diff --git a/src/index.ts b/src/index.ts index 5576e3a..3caa856 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,18 +41,18 @@ async function visitCityPages(): Promise { 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 { 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 { // 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 { // 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 { 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 { if (page > 10) break; UIActions.clickPagination(driver, page); - await WebDriverUtils.wait(); + await WebDriverUtils.wait(driver); } diff --git a/src/lib/UIActions.ts b/src/lib/UIActions.ts index 765d9bf..5c16bcc 100644 --- a/src/lib/UIActions.ts +++ b/src/lib/UIActions.ts @@ -21,9 +21,9 @@ export async function execSearch(driver: WebDriver, city: string): Promise { - 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 { + 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)); + } } /**