1
1
const puppeteer = require ( 'puppeteer' ) ;
2
2
3
- ( async ( ) => {
4
- // Launch the browser and open a new blank page
5
- const browser = await puppeteer . launch ( ) ;
6
- const page = await browser . newPage ( ) ;
3
+ describe ( 'Puppeteer lab test' , ( ) => {
4
+ it ( 'should search for chipi chipi chapa chapa and print the title of the first result in Docs section' , async ( ) => {
5
+ const browser = await puppeteer . launch ( ) ;
6
+ const page = await browser . newPage ( ) ;
7
+ await page . goto ( 'https://pptr.dev/' ) ;
7
8
8
- // Navigate the page to a URL
9
- await page . goto ( 'https://pptr.dev/ ') ;
9
+ // 在搜索框中輸入 "chipi chipi chapa chapa"
10
+ await page . type ( 'input[name="search"]' , 'chipi chipi chapa chapa ') ;
10
11
11
- // Hints:
12
- // Click search button
13
- // Type into search box
14
- // Wait for search result
15
- // Get the `Docs` result section
16
- // Click on first result in `Docs` section
17
- // Locate the title
18
- // Print the title
12
+ // 等待搜索結果加載並點擊 Docs 部分的第一個結果
13
+ await page . waitForSelector ( '.algolia-docsearch-suggestion--title' ) ;
14
+ const firstResult = await page . $ ( '.algolia-docsearch-suggestion--title' ) ;
15
+ await firstResult . click ( ) ;
19
16
20
- // Close the browser
21
- await browser . close ( ) ;
22
- } ) ( ) ;
17
+ // 等待頁面加載並打印標題
18
+ await page . waitForSelector ( 'h1' ) ;
19
+ const title = await page . $eval ( 'h1' , element => element . textContent ) ;
20
+ console . log ( 'Title:' , title ) ;
21
+
22
+ await browser . close ( ) ;
23
+ } ) ;
24
+ } ) ;
0 commit comments