initial commit

This commit is contained in:
Ken Yasue
2025-03-23 20:32:57 +01:00
parent c2e18462be
commit 4b42f7bf3a
14 changed files with 1884 additions and 132 deletions

45
src/lib/utils.ts Normal file
View File

@ -0,0 +1,45 @@
/**
* Utility class for common WebDriver operations
*/
import { WebDriver, By, until } from 'selenium-webdriver';
import { writeFileSync, existsSync, appendFileSync } from 'fs';
import * as path from 'path';
import { ContactInfo } from './types';
export class WebDriverUtils {
/**
* Wait for a specified number of seconds
* @param seconds Number of seconds to wait
* @returns Promise that resolves after the specified time
*/
static async wait(seconds: number): Promise<void> {
console.log(`Waiting for ${seconds} seconds...`);
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
/**
* Wait for an element to be located on the page
* @param driver WebDriver instance
* @param selector CSS selector for the element
* @param timeoutMs Timeout in milliseconds (default: 10000)
* @returns Promise that resolves when the element is found
*/
static async waitForElement(driver: WebDriver, selector: string, timeoutMs: number = 10000) {
console.log(`Waiting for element: ${selector}`);
await driver.wait(until.elementLocated(By.css(selector)), timeoutMs);
}
}
export function saveContactInfoToCSV(city: string, contactInfo: ContactInfo, filePath: string): void {
const headers = 'City,Website URL,Email\n';
const line = `"${city},"${contactInfo.websiteUrl}","${contactInfo.email}"\n`;
if (!existsSync(filePath)) {
writeFileSync(filePath, headers + line);
} else {
appendFileSync(filePath, line);
}
console.log(`Contact info saved to ${filePath}`);
}