Skip to content

Commit

Permalink
#62: Check for a warning modal after making picks
Browse files Browse the repository at this point in the history
  • Loading branch information
blchelle committed Sep 3, 2023
1 parent be3c26c commit 8eb24d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
name: Install
with:
cmd: install
env:
PUPPETEER_SKIP_DOWNLOAD: true
- uses: borales/actions-yarn@v3.0.0
name: Lint
with:
Expand Down
48 changes: 35 additions & 13 deletions src/webscraper/makePicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,8 @@ const pickGame = async (page: Page, gameInputs: GameInputs, pick: number[]): Pro
// Only click the button if it hasn't been previously selected from a prior session
if (!await elementHandleHasClass(buttons[teamPicked], 'btn-pickable-saved')) await simulateClick(buttons[teamPicked], 'Space')

// Check if the "Underdog Warning" modal popped up. Close it if it did.
try {
const closeModalButton = await page.waitForSelector('#alertModal .modal-footer button', { timeout: 3000 })

// Clicks the button to close the modal and waits for the modal to dissapear
if (closeModalButton !== null) await simulateClick(closeModalButton, 'Enter')
await page.waitForFunction(() => document.querySelector('#alertModal') === null)
} catch (err) {
if (!(err instanceof TimeoutError)) {
console.log('An non-timeout error occurred: ', err)
throw err
}
}
// Closes the "Underdog Warning" modal, if it appears
await closeWarningModal(page)

// Clicking the rank picker will show a dropdown
await page.waitForFunction((rankPickerEl) => !Array.from(rankPickerEl.classList).includes('btn-locked'), {}, rankPicker)
Expand All @@ -103,6 +92,39 @@ const pickGame = async (page: Page, gameInputs: GameInputs, pick: number[]): Pro
await simulateClick(rankButton, 'Space')
}

/**
* Checks to see if a warning modal pops up after making a pick and closes it if it does necessary
*
* @param page The puppeteer.Page instance
*/
const closeWarningModal = async (page: Page): Promise<void> => {
const alertModalSelector = '#alertModal[style="display: block;"]'

try {
// Wait for the alertModal to load, most of the time this should throw a TimeoutError
const alertModal = await page.waitForSelector(alertModalSelector, { timeout: 1000 })
if (alertModal == null) {
throw new Error('anomaly: alert modal is null, it should either be found or throw instead')
}

// Get the button to close the modal
const closeButton = await alertModal.waitForSelector('.modal-footer button')
if (closeButton == null) {
throw new Error('anomaly: alert modal was found but close button is null, it should be found')
}

// Clicks the button to close the modal and waits for the modal to dissapear
await simulateClick(closeButton, 'Space')
await page.waitForSelector('#alertModal', { hidden: true })
} catch (err) {
// A timeout error is expected, anything else is an actual error we should throw
if (!(err instanceof TimeoutError)) {
console.log('An non-timeout error occurred: ', err)
throw err
}
}
}

/**
* Clicks the submit button and waits to navigate to the confirmation page
* @param page the puppeteer.Page instance
Expand Down

0 comments on commit 8eb24d7

Please sign in to comment.