From 058cec21b7bf5f845ddefbb5aea0c3185159190b Mon Sep 17 00:00:00 2001 From: Rongrong Date: Thu, 24 Nov 2022 06:32:15 +0800 Subject: [PATCH] test(puppeteer): wait for bot test result (#11322) Signed-off-by: Rongrong Signed-off-by: Rongrong --- .github/workflows/test.yml | 7 ++++--- test/utils/puppeteer.js | 29 ++++++++--------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d082ac3488..577eb29859 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -93,9 +93,10 @@ jobs: run: yarn - name: Install Chromium if: ${{ matrix.chromium.dependency != '' }} - # Chromium from Ubuntu is too old (85), but can still pass the tests - # That's not really a problem since Chromium-bundled Docker image is based on Debian bullseye, - # which updates Chromium frequently, and only on arm/arm64 the image needs Chromium from Debian. + # 'chromium-browser' from Ubuntu APT repo is a dummy package. Its version (85.0.4183.83) means + # nothing since it calls Snap (disgusting!) to install Chromium, which should be up-to-date. + # That's not really a problem since the Chromium-bundled Docker image is based on Debian bullseye, + # which provides up-to-date native packages. run: | set -ex curl -s "https://dl.google.com/linux/linux_signing_key.pub" | gpg --dearmor | diff --git a/test/utils/puppeteer.js b/test/utils/puppeteer.js index ecdc8f8336..c1ad95e3b2 100644 --- a/test/utils/puppeteer.js +++ b/test/utils/puppeteer.js @@ -1,6 +1,5 @@ let puppeteer; const wait = require('../../lib/utils/wait'); -const cheerio = require('cheerio'); let browser = null; @@ -47,38 +46,26 @@ describe('puppeteer', () => { puppeteer = require('../../lib/utils/puppeteer'); browser = await puppeteer({ stealth: false }); const page = await browser.newPage(); - await page.goto('https://bot.sannysoft.com'); - - const html = await page.evaluate(() => document.body.innerHTML); - const $ = cheerio.load(html); - browser.close(); - browser = null; - - const webDriverTest = $('tbody tr').eq(2).find('td').eq(1).text().trim(); - const chromeTest = $('tbody tr').eq(4).find('td').eq(1).text().trim(); + await page.goto('https://bot.sannysoft.com', { waitUntil: 'networkidle0' }); + // page rendering is not instant, wait for expected elements to appear + const [webDriverTest, chromeTest] = await Promise.all(['webdriver', 'chrome'].map((t) => page.waitForSelector(`td#${t}-result.result.failed`).then((hd) => hd.evaluate((e) => e.textContent)))); // the website return empty string from time to time for no reason // since we don't really care whether puppeteer without stealth passes the bot test, just let it go expect(['present (failed)', '']).toContain(webDriverTest); expect(['missing (failed)', '']).toContain(chromeTest); - }, 10000); + }, 15000); it('puppeteer with stealth', async () => { puppeteer = require('../../lib/utils/puppeteer'); browser = await puppeteer({ stealth: true }); const page = await browser.newPage(); - await page.goto('https://bot.sannysoft.com'); - - const html = await page.evaluate(() => document.body.innerHTML); - const $ = cheerio.load(html); - browser.close(); - browser = null; - - const webDriverTest = $('tbody tr').eq(2).find('td').eq(1).text().trim(); - const chromeTest = $('tbody tr').eq(4).find('td').eq(1).text().trim(); + await page.goto('https://bot.sannysoft.com', { waitUntil: 'networkidle0' }); + // page rendering is not instant, wait for expected elements to appear + const [webDriverTest, chromeTest] = await Promise.all(['webdriver', 'chrome'].map((t) => page.waitForSelector(`td#${t}-result.result.passed`).then((hd) => hd.evaluate((e) => e.textContent)))); // these are something we really care about expect(webDriverTest).toBe('missing (passed)'); expect(chromeTest).toBe('present (passed)'); - }, 10000); + }, 15000); it('puppeteer accept proxy uri', async () => { process.env.PROXY_URI = 'http://user:pass@rsshub.proxy:2333';