From 51b47f142cbfbbe6f0f48c138f4548302cf3a0a8 Mon Sep 17 00:00:00 2001 From: Chen Chun-Hsuan Date: Wed, 2 Apr 2025 03:56:12 +0800 Subject: [PATCH] test: add test for retrieve page title --- lab4/main_test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a..1cf4c0d 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -2,7 +2,12 @@ const puppeteer = require('puppeteer'); (async () => { // Launch the browser and open a new blank page - const browser = await puppeteer.launch(); + // const browser = await puppeteer.launch(); + const browser = await puppeteer.launch({ + headless: true, // 關閉無頭模式 + slowMo: 50, // 設定操作延遲,單位為毫秒 + args: ['--start-maximized'] // 啟動時最大化視窗 + }); const page = await browser.newPage(); // Navigate the page to a URL @@ -10,12 +15,31 @@ const puppeteer = require('puppeteer'); // Hints: // Click search button + + function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + + const searchButtonSelector = '#__docusaurus > nav > div.navbar__inner > div.navbar__items.navbar__items--right > div.navbarSearchContainer_IP3a > button'; + await page.waitForSelector(searchButtonSelector); + await page.click(searchButtonSelector); // Type into search box + const searchBoxSelector = '#docsearch-input'; + await page.waitForSelector(searchBoxSelector); + await page.type(searchBoxSelector, 'andy popoo'); // Wait for search result + const searchResultSelector = '#docsearch-hits1-item-4 > a > div'; + await page.waitForSelector(searchResultSelector, { visible: true }); // Get the `Docs` result section // Click on first result in `Docs` section + await page.click(searchResultSelector); // Locate the title + const textSelector = await page.waitForSelector( + '#__docusaurus_skipToContent_fallback > div > div > main > div > div > div > div > article > div.theme-doc-markdown.markdown > header > h1' + ); + const fullTitle = await textSelector?.evaluate(el => el.textContent); // Print the title + console.log(fullTitle); // Close the browser await browser.close();