initial commit
This commit is contained in:
45
src/lib/utils.ts
Normal file
45
src/lib/utils.ts
Normal 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}`);
|
||||
}
|
||||
Reference in New Issue
Block a user