213 lines
7.0 KiB
JavaScript
213 lines
7.0 KiB
JavaScript
const { Builder, By, until, Keys } = require('selenium-webdriver');
|
|
const chrome = require('selenium-webdriver/chrome');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Test CSV data
|
|
const STARK_CSV_DATA = `ProduktNr;Produktnavn;Kategori;Enhed;Pris;Lager
|
|
STARK-280;B7 Tagplader gul;Tagmaterialer;m2;245.50;100
|
|
STARK-1001;Regugle 38x73;Materialer;meter;12.75;500
|
|
STARK-1500;Tagskrue 4.8x35;Beslag;kg;89.50;50
|
|
STARK-2000;Isolering 150mm;Isolering;m2;125.00;200
|
|
STARK-2500;Dampspørre;Isolering;m2;45.00;150`;
|
|
|
|
const BASE_URL = 'http://localhost:3000';
|
|
const TIMEOUT = 10000;
|
|
|
|
async function starkImportTests() {
|
|
let driver;
|
|
let testFilePath;
|
|
|
|
try {
|
|
// Setup
|
|
console.log('🚀 Starting Selenium Stark Import Tests...\n');
|
|
|
|
// Create test CSV
|
|
const testDir = path.join(__dirname, '../_temp');
|
|
if (!fs.existsSync(testDir)) {
|
|
fs.mkdirSync(testDir, { recursive: true });
|
|
}
|
|
testFilePath = path.join(testDir, 'stark_selenium_test.csv');
|
|
fs.writeFileSync(testFilePath, STARK_CSV_DATA);
|
|
console.log(`✅ Test CSV created: ${testFilePath}\n`);
|
|
|
|
// Create Chrome driver
|
|
const options = new chrome.Options();
|
|
// options.addArguments('--headless'); // Uncomment for headless mode
|
|
driver = await new Builder()
|
|
.forBrowser('chrome')
|
|
.setChromeOptions(options)
|
|
.build();
|
|
|
|
// Test 1: Navigate to application
|
|
console.log('Test 1: Navigate to application');
|
|
await driver.get(BASE_URL);
|
|
await driver.wait(until.elementLocated(By.css('body')), TIMEOUT);
|
|
console.log('✅ Application loaded successfully\n');
|
|
|
|
// Test 2: Find and click Stark import button
|
|
console.log('Test 2: Find Stark import button');
|
|
try {
|
|
const starkButton = await driver.wait(
|
|
until.elementLocated(By.xpath("//button[contains(text(), 'Importer Stark')]")),
|
|
TIMEOUT
|
|
);
|
|
console.log('✅ Stark import button found');
|
|
await starkButton.click();
|
|
console.log('✅ Button clicked\n');
|
|
} catch (e) {
|
|
console.log('⚠️ Stark button not found - may not be on materials page');
|
|
}
|
|
|
|
// Test 3: Check for modal
|
|
console.log('Test 3: Verify modal appears');
|
|
try {
|
|
const modal = await driver.wait(
|
|
until.elementLocated(By.xpath("//*[contains(text(), 'Importer Stark')]")),
|
|
TIMEOUT
|
|
);
|
|
console.log('✅ Modal found\n');
|
|
} catch (e) {
|
|
console.log('⚠️ Modal not visible\n');
|
|
}
|
|
|
|
// Test 4: Upload file
|
|
console.log('Test 4: Upload Stark CSV file');
|
|
try {
|
|
const fileInput = await driver.findElement(By.css('input[type="file"]'));
|
|
await fileInput.sendKeys(testFilePath);
|
|
console.log(`✅ File uploaded: ${path.basename(testFilePath)}\n`);
|
|
} catch (e) {
|
|
console.log(`⚠️ File upload failed: ${e.message}\n`);
|
|
}
|
|
|
|
// Test 5: Wait for processing
|
|
console.log('Test 5: Wait for upload processing');
|
|
await driver.wait(async () => {
|
|
try {
|
|
const statusEl = await driver.findElement(By.xpath("//*[contains(@class, 'status')]"));
|
|
const text = await statusEl.getText();
|
|
return text.includes('Success') || text.includes('Error') || text.includes('uploaderet');
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}, 5000).catch(() => {
|
|
console.log('⚠️ Processing status not found (may have completed)');
|
|
return true;
|
|
});
|
|
console.log('✅ Processing complete\n');
|
|
|
|
// Test 6: API Status Check
|
|
console.log('Test 6: Check API status endpoint');
|
|
try {
|
|
await driver.get(`${BASE_URL}/api/stark/status`);
|
|
const pageSource = await driver.getPageSource();
|
|
|
|
if (pageSource.includes('stark_materials_cache')) {
|
|
console.log('✅ API status endpoint working\n');
|
|
} else {
|
|
console.log('⚠️ API response unexpected\n');
|
|
}
|
|
} catch (e) {
|
|
console.log(`⚠️ API status check failed: ${e.message}\n`);
|
|
}
|
|
|
|
// Test 7: Navigate to materials page
|
|
console.log('Test 7: Navigate to materials list');
|
|
try {
|
|
await driver.get(`${BASE_URL}/materials`);
|
|
await driver.wait(until.elementLocated(By.css('body')), TIMEOUT);
|
|
console.log('✅ Materials page loaded\n');
|
|
} catch (e) {
|
|
console.log(`⚠️ Materials page not found: ${e.message}\n`);
|
|
}
|
|
|
|
// Test 8: Check for Stark materials
|
|
console.log('Test 8: Search for Stark materials');
|
|
try {
|
|
const starkElements = await driver.findElements(By.xpath("//*[contains(text(), 'STARK-')]"));
|
|
console.log(`✅ Found ${starkElements.length} Stark product references\n`);
|
|
} catch (e) {
|
|
console.log(`⚠️ No Stark materials visible on page\n`);
|
|
}
|
|
|
|
// Test 9: Project creation
|
|
console.log('Test 9: Create project with Stark materials');
|
|
try {
|
|
await driver.get(`${BASE_URL}/projects`);
|
|
await driver.wait(until.elementLocated(By.css('body')), TIMEOUT);
|
|
|
|
const newProjectBtn = await driver.wait(
|
|
until.elementLocated(By.xpath("//button[contains(text(), 'Nyt')]")),
|
|
TIMEOUT
|
|
).catch(() => null);
|
|
|
|
if (newProjectBtn) {
|
|
await newProjectBtn.click();
|
|
console.log('✅ New project dialog opened');
|
|
|
|
// Fill project name
|
|
const nameInput = await driver.wait(
|
|
until.elementLocated(By.css('input[placeholder*="navn"]')),
|
|
TIMEOUT
|
|
).catch(() => null);
|
|
|
|
if (nameInput) {
|
|
await nameInput.sendKeys('Test Stark Project');
|
|
console.log('✅ Project name entered');
|
|
}
|
|
}
|
|
console.log('✅ Project creation flow accessible\n');
|
|
} catch (e) {
|
|
console.log(`⚠️ Project creation test skipped: ${e.message}\n`);
|
|
}
|
|
|
|
// Test 10: Database verification
|
|
console.log('Test 10: Verify test data in database');
|
|
const sql = require('mysql2/promise');
|
|
try {
|
|
const connection = await sql.createConnection({
|
|
host: 'localhost',
|
|
user: 'tilbuduser',
|
|
password: process.env.DB_PASSWORD,
|
|
database: 'tilbudgivern'
|
|
});
|
|
|
|
const [rows] = await connection.execute(
|
|
'SELECT COUNT(*) as count FROM stark_materials_cache'
|
|
);
|
|
const count = rows[0].count;
|
|
console.log(`✅ Database check: ${count} products in stark_materials_cache\n`);
|
|
|
|
const [materials] = await connection.execute(
|
|
"SELECT COUNT(*) as count FROM materials WHERE sku LIKE 'STARK-%'"
|
|
);
|
|
const materialCount = materials[0].count;
|
|
console.log(`✅ Database check: ${materialCount} STARK materials in materials table\n`);
|
|
|
|
await connection.end();
|
|
} catch (e) {
|
|
console.log(`⚠️ Database verification failed: ${e.message}\n`);
|
|
}
|
|
|
|
// Final report
|
|
console.log('━'.repeat(50));
|
|
console.log('🎉 Selenium Stark Import Tests Completed!');
|
|
console.log('━'.repeat(50));
|
|
|
|
} catch (error) {
|
|
console.error('❌ Test Error:', error);
|
|
} finally {
|
|
if (driver) {
|
|
await driver.quit();
|
|
}
|
|
// Cleanup
|
|
if (testFilePath && fs.existsSync(testFilePath)) {
|
|
fs.unlinkSync(testFilePath);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run tests
|
|
starkImportTests().catch(console.error);
|