Web automation test of cuketest + puppeter

In the last article, we talked about the related theoretical knowledge of CukeTest + puppeter, and we were familiar with how to run CukeTest and how to write a script. The general theoretical system of puppeter and how to use it in combination, but we haven't given you hands-on practical operation. This one, I'll take you all together to practice~~~

The test page takes Baidu home page as an example. We use cuketest + puppeter to write a function test Demo, which combines the relevant knowledge points mentioned in the previous article.

Official documents of CukeTest: http://www.cuketest.com/zh-cn/

Official document of puppeter: https://zhaoqize.github.io/puppeteer-api-zh_CN/

1, Example 1

Function test: open multiple web pages in parametric form

1. Open CukeTest to create a new empty project, install Node and puppeter. Note (compatibility between the two versions). As mentioned above.

2. Edit script related parameters

3. Write the script corresponding to the script

4. Operation

As shown below

The text of the play is as follows

# language: zh-CN
 Function: Baidu Homepage
 Open Baidu Homepage

  Scene Outline: page open
    If you open Baidu homepage "< Param1 >"
      | param1                  |
      | https://www.baidu.com/  |
      | https://www.runoob.com/ |
      | param1                   |
      | https://www.csdn.net/    |
      | https://www.cnblogs.com/ |

  Scene: Baidu homepage search
  Open Baidu homepage, search 'puppeter', baidu queries and screenshots to save results
    If open Baidu home page“ https://www.baidu.com/ "
    When inputting "puppeter", click Baidu
    Then the screenshot saves the search results

Script corresponding to the script

 1 //quote cucumber and puppeteer,Use the respective API
 2 const {
 3     Given,
 4     When,
 5     Then
 6 } = require('cucumber');
 7 const puppeteer = require('puppeteer');
 8 //Asynchronous by default hook And steps timeout after 5000 milliseconds, which can be modified globally
 9 const {
10     setDefaultTimeout
11 } = require('cucumber');
12 setDefaultTimeout(60 * 1000);
13 //Variable definition
14 let browser = null;
15 let page = null;
17 //// Your step definition/////
19 Given("Open Baidu Homepage{string}", async function (arg1) {
20     /*
21     When the puppeter is connected to a Chromium instance, the puppeteer.launch  Create a Browser object.
22     Create an instance object of Browser class
23     executablePath:Path to run Chrome or Chrome executable
24     headless: Whether to run the browser in headless mode
25     defaultViewport: null, args: ['--start-maximized']:Maximize view window
26     defaultViewport Option set to null to disable 800x600 resolution. It needs the maximum resolution
27     ignoreDefaultArgs: ['--enable-automation']:
28     It is forbidden to show that there is a chrome in the upper left corner of chrome that is under the control of automatic software to avoid the puppeter being detected by the front JS
29     */
30     browser = await puppeteer.launch({
31         executablePath: 'D:\\Demo\\node_modules\\puppeteer\\.local-chromium\\win64-722234\\chrome-win\\chrome.exe',
32         headless: false,
33         defaultViewport: null,
34         args: ['--start-maximized'],
35         ignoreDefaultArgs: ['--enable-automation']
36     });
37     //Create a new Page object
38     page = await browser.newPage();
39     //Open link
40     await page.goto(arg1);
41     //Wait 3 s
42     await page.waitFor(3000);
43     //close chromium
44     await browser.close();
45 });

Run: the browser opens the four pages edited in the script in sequence, opens one page, closes the browser, and then opens the next page.

2, Example 2

Function: open Baidu homepage search and save screenshot.

Based on the above example 1, then write scenario 2, as shown below

Script corresponding to the scene:

 1 //Scene 2: Baidu homepage search
 2 When("input{string},Click Baidu", async function (arg1) {
 3     //Match positioning elements, positioning input box elements,And input'puppeteer'
 4     await page.type('#kw', arg1);
 5     //Wait 2 s
 6     await page.waitFor(2000);
 7     //Click "Baidu below" button
 8     await page.click('#su');
 9     //Wait 3 s
10     await page.waitFor(3000);
11 });
13 Then("Screenshots saving search results", async function () {
14     //Screenshot and save. The screenshot path is path,The screenshot type can be jpeg perhaps png,default 'png'.
15     await page.screenshot({ path: 'baiduHomeSearch.png' });
16     //close chromium
17     await browser.close();
18 });

Tags: github

Posted on Mon, 25 May 2020 00:49:31 -0700 by belphegor