diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a5..724e68f4 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -1,22 +1,24 @@ const puppeteer = require('puppeteer'); -(async () => { - // Launch the browser and open a new blank page - const browser = await puppeteer.launch(); - const page = await browser.newPage(); +describe('Puppeteer lab test', () => { + it('should search for chipi chipi chapa chapa and print the title of the first result in Docs section', async () => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.goto('https://pptr.dev/'); - // Navigate the page to a URL - await page.goto('https://pptr.dev/'); + // 在搜索框中輸入 "chipi chipi chapa chapa" + await page.type('input[name="search"]', 'chipi chipi chapa chapa'); - // Hints: - // Click search button - // Type into search box - // Wait for search result - // Get the `Docs` result section - // Click on first result in `Docs` section - // Locate the title - // Print the title + // 等待搜索結果加載並點擊 Docs 部分的第一個結果 + await page.waitForSelector('.algolia-docsearch-suggestion--title'); + const firstResult = await page.$('.algolia-docsearch-suggestion--title'); + await firstResult.click(); - // Close the browser - await browser.close(); -})(); \ No newline at end of file + // 等待頁面加載並打印標題 + await page.waitForSelector('h1'); + const title = await page.$eval('h1', element => element.textContent); + console.log('Title:', title); + + await browser.close(); + }); +});