Closed hjyssg closed 3 years ago
// https://github.com/mochajs/mocha // npm i mocha chai puppeteer // "scripts": { // "test": "mocha test.js" // }, const puppeteer = require('puppeteer'); https://stackoverflow.com/questions/25678063/whats-the-difference-between-assertion-library-testing-framework-and-testing-e#:~:text=Assertion%20libraries%20are%20tools%20to,do%20thousands%20of%20if%20statements. const { expect } = require('chai'); describe('Duck Duck Go search using basic Puppeteer', function(){ let browser; let page; //https://github.com/mochajs/mocha/issues/2586 //不用arrow function // Passing arrow functions (aka “lambdas”) to Mocha is discouraged. Lambdas lexically bind this and cannot access the Mocha context. this.timeout(30*1000); before(async function() { // runs once before the first test in this block browser = await puppeteer.launch({ headless: false }); }); after(async function() { // runs once after the last test in this block await browser.close(); }); beforeEach(async function() { page = await browser.newPage(); await page.goto('https://duckduckgo.com'); }); afterEach(async function() { await page.close(); }); it('should have the correct page title', async function() { const title = await page.title(); expect(title).to.eql('DuckDuckGo — Privacy, simplified.'); }); // it('should show a list of results when searching actual word', async () => { // await page.type('input[id=search_form_input_homepage]', 'puppeteer'); // await page.click('input[type="submit"]'); // await page.waitForSelector('h2 a'); // const links = await page.evaluate(() => { // return Array.from(document.querySelectorAll('h2 a')); // }); // expect(links.length).to.be.greaterThan(0); // }); // it('should show a warning when searching fake word', async () => { // await page.type('input[id=search_form_input_homepage]', 'pupuppeppeteerteer'); // await page.click('input[type="submit"]'); // await page.waitForSelector('div[class=msg__wrap]'); // const text = await page.evaluate(() => { // return document.querySelector('div[class=msg__wrap]').textContent; // }); // expect(text).to.contain('Not many results contain'); // }); });