Files
SERVtomaste/temp/test-session.js

160 lines
6.1 KiB
JavaScript

import auth from '../tools/siemens-auth.js';
console.log('=== Testing NX Open Session Class Access ===\n');
const browser = await auth.getBrowser();
const page = await browser.newPage();
await page.setViewport({ width: 1400, height: 900 });
await auth.loadCookies(page);
const baseUrl = 'https://docs.sw.siemens.com/documentation/external/PL20200522120320484/en-US/nx_open_python_ref/nx/1980/nx_open_python_ref/en-US/nxopen_python_ref/';
// First navigate to the index page to establish session
console.log('Loading index page first...');
await page.goto(baseUrl + 'index.html', { waitUntil: 'networkidle2', timeout: 60000 });
await new Promise(r => setTimeout(r, 2000));
// Handle cookie popup
const cookieBtn = await page.evaluateHandle(() => {
const btns = Array.from(document.querySelectorAll('button'));
return btns.find(b => b.innerText && b.innerText.includes('Accept All Cookies'));
});
if (cookieBtn && cookieBtn.asElement()) {
await cookieBtn.asElement().click();
await new Promise(r => setTimeout(r, 2000));
}
// Try to navigate to Class Index and find Session
console.log('\n=== Looking for NXOpen.Session class ===');
const classIndexUrl = baseUrl + 'classes.html';
await page.goto(classIndexUrl, { waitUntil: 'networkidle2', timeout: 60000 });
await new Promise(r => setTimeout(r, 3000));
// Get HTML to find the actual Session link
const sessionInfo = await page.evaluate(() => {
const html = document.body.innerHTML;
const text = document.body.innerText;
// Look for Session (NXOpen) in the class index
const sessionMatch = text.match(/Session\s*\(NXOpen\)/);
// Look for all links containing Session
const links = Array.from(document.querySelectorAll('a'));
const sessionLinks = links
.filter(a => a.innerText && a.innerText.includes('Session') && a.href)
.map(a => ({ text: a.innerText.trim(), href: a.href }));
return {
hasSessionInText: sessionMatch ? sessionMatch[0] : null,
sessionLinks: sessionLinks,
// Look for the specific pattern in text
sessionSection: text.split('\n').filter(l => l.includes('Session') && l.includes('NXOpen')).slice(0, 10)
};
});
console.log('Session in text:', sessionInfo.hasSessionInText);
console.log('Session-related lines:', sessionInfo.sessionSection);
console.log('Session links found:', sessionInfo.sessionLinks.length);
sessionInfo.sessionLinks.slice(0, 10).forEach(l => console.log(` - ${l.text}: ${l.href.split('/').pop()}`));
// Navigate to the S section of the class index
console.log('\n=== Checking S section of class index ===');
const sSectionUrl = baseUrl + 'classes.html#letter_s';
await page.goto(sSectionUrl, { waitUntil: 'networkidle2', timeout: 60000 });
await new Promise(r => setTimeout(r, 2000));
const sSectionContent = await page.evaluate(() => {
const text = document.body.innerText;
// Find the section starting from S
const sIndex = text.indexOf('\nS\n');
if (sIndex > -1) {
return text.substring(sIndex, sIndex + 3000);
}
// Try to find Session
const sessionIdx = text.indexOf('Session');
if (sessionIdx > -1) {
return text.substring(Math.max(0, sessionIdx - 100), sessionIdx + 500);
}
return text.substring(0, 2000);
});
console.log('S section content:');
console.log(sSectionContent);
// Try to click on Session class from classes page
const sessionClassLinks = await page.evaluate(() => {
const links = Array.from(document.querySelectorAll('a'));
return links
.filter(a => {
const text = a.innerText.trim();
// Look for Session class that's under NXOpen namespace
return text === 'Session' || (text.includes('Session') && !text.includes('Builder') && !text.includes('Collection') && text.length < 30);
})
.map(a => ({ text: a.innerText.trim(), href: a.href, parent: a.parentElement?.innerText?.substring(0, 50) || '' }))
.slice(0, 10);
});
console.log('\nSession class candidates:', sessionClassLinks);
// Try direct access to NXOpen.Session class page using Doxygen naming convention
// Doxygen uses classNXOpen_1_1Session.html format
console.log('\n=== Trying direct Session class URL ===');
const sessionDirectUrl = baseUrl + 'a55186.html'; // This is likely the Session page based on pattern
await page.goto(sessionDirectUrl, { waitUntil: 'networkidle2', timeout: 60000 });
await new Promise(r => setTimeout(r, 3000));
let sessionPageResult = await page.evaluate(() => {
return {
title: document.title,
url: window.location.href,
hasError: document.body.innerText.includes('Access Denied'),
content: document.body.innerText.substring(0, 3000)
};
});
console.log('Direct URL result:');
console.log('Title:', sessionPageResult.title);
console.log('Has error:', sessionPageResult.hasError);
console.log('Content:', sessionPageResult.content.substring(0, 500));
// If error, try to find Session by navigating from hierarchy
if (sessionPageResult.hasError) {
console.log('\n=== Trying via hierarchy.html ===');
await page.goto(baseUrl + 'hierarchy.html', { waitUntil: 'networkidle2', timeout: 60000 });
await new Promise(r => setTimeout(r, 3000));
// Find Session link
const hierarchySession = await page.evaluate(() => {
const links = Array.from(document.querySelectorAll('a'));
const sessionLink = links.find(a => a.innerText.trim() === 'Session');
if (sessionLink) {
return { text: sessionLink.innerText, href: sessionLink.href };
}
return null;
});
if (hierarchySession) {
console.log('Found Session in hierarchy:', hierarchySession);
await page.goto(hierarchySession.href, { waitUntil: 'networkidle2', timeout: 60000 });
await new Promise(r => setTimeout(r, 3000));
sessionPageResult = await page.evaluate(() => {
return {
title: document.title,
url: window.location.href,
hasError: document.body.innerText.includes('Access Denied'),
content: document.body.innerText.substring(0, 3000)
};
});
console.log('\nSession page via hierarchy:');
console.log('Title:', sessionPageResult.title);
console.log('Has error:', sessionPageResult.hasError);
console.log('Content:', sessionPageResult.content);
}
}
await page.screenshot({ path: '/tmp/session-final.png' });
await auth.closeBrowser();
console.log('\n=== Test Complete ===');