auto scroll

This commit is contained in:
Ken Yasue
2025-03-24 06:57:25 +01:00
parent b6cad2a241
commit 1ffea0f61f
3 changed files with 42 additions and 16 deletions

View File

@ -41,18 +41,18 @@ async function visitCityPages(): Promise<void> {
console.log("Logo click")
if (!await UIActions.gotoHome(driver)) throw `${city} failed`;
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
console.log("Exec Search")
if (!await UIActions.execSearch(driver, city)) throw `${city} failed`;
await WebDriverUtils.wait(5);
await WebDriverUtils.wait(driver);
console.log("Click See all")
if (!await UIActions.clickSeeAll(driver)) {
if (!await UIActions.clickTourismLink(driver)) throw `${city} failed`;
if (!await UIActions.clickSeeAll(driver)) throw `${city} failed`;
}
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
console.log("Switch tab")
let windows = await driver.getAllWindowHandles();
@ -66,7 +66,7 @@ async function visitCityPages(): Promise<void> {
console.log("Click See all attractions")
if (!await UIActions.clickSeeAllAttractions(driver)) throw `${city} failed`;
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
console.log("Switch tab to Attraction")
windows = await driver.getAllWindowHandles();
@ -81,7 +81,7 @@ async function visitCityPages(): Promise<void> {
// click museum
console.log("Click Museum link");
if (!await UIActions.clickMuseumsLink(driver)) throw `${city} failed`;
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
let page = 1;
while (1) {
@ -89,12 +89,12 @@ async function visitCityPages(): Promise<void> {
// get list of museums
console.log("Get list of museums");
const museumElms = await UIActions.getMusiums(driver);
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
for (const listItem of museumElms) {
await listItem.click();
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
windows = await driver.getAllWindowHandles();
for (const handle of windows) {
@ -111,10 +111,10 @@ async function visitCityPages(): Promise<void> {
museumWindow && await driver.switchTo().window(museumWindow);
await driver.close();
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
attactionsWindow && await driver.switchTo().window(attactionsWindow);
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
}
@ -123,7 +123,7 @@ async function visitCityPages(): Promise<void> {
if (page > 10) break;
UIActions.clickPagination(driver, page);
await WebDriverUtils.wait();
await WebDriverUtils.wait(driver);
}

View File

@ -21,9 +21,9 @@ export async function execSearch(driver: WebDriver, city: string): Promise<boole
await searchInput.sendKeys(city);
// Submit the search (press Enter)
await WebDriverUtils.wait(2);
await WebDriverUtils.wait(driver);
await searchInput.sendKeys('\uE007'); // Unicode for Enter key
await WebDriverUtils.wait(5); // Wait 5 seconds before next city
await WebDriverUtils.wait(driver); // Wait 5 seconds before next city
return true;
} catch (e) {

View File

@ -14,10 +14,36 @@ export class WebDriverUtils {
* @param seconds Number of seconds to wait
* @returns Promise that resolves after the specified time
*/
static async wait(seconds: number = 3): Promise<void> {
seconds = Math.floor(Math.random() * 1000) % 3 + 3;
console.log(`Waiting for ${seconds} seconds...`);
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
static async wait(driver?: WebDriver): Promise<void> {
const seconds = Math.floor(Math.random() * 1000) % 3 + 3;
console.log(`Scrolling to bottom for ${seconds} seconds...`);
const endTime = Date.now() + seconds * 1000;
let scrollCounter = 0;
while (Date.now() < endTime) {
try {
if(driver){
if(scrollCounter < 4){
await driver.executeScript(`
window.scrollBy(0, window.innerHeight);
`);
}else{
await driver.executeScript(`
window.scrollTo(0, 0);
`);
}
scrollCounter++;
}
} catch (error) {
console.warn('Scroll failed:', error);
}
// Wait a little between scrolls
await new Promise(resolve => setTimeout(resolve, 500));
}
}
/**