[Vue-Nightwatch] Using Nightwatch to implement unit testing of Vue projects

unit test:

Personal understanding is that the code simulates the user to test the use case, the case wizard is similar to this

 installation

First we create a project and then enter the project:

cd nightwatch-guide

Then initialize the project's package.json

npm init -y

Then install dependencies:

npm install nightwatch

Since it is based on Selenium Server, you have to install this:

npm install selenium-server

We hope chrome-driver to test, so install Google driver again, of course, you can also choose Firefox or other drivers

npm install chromedriver

Install another driver

npm install geckodriver

prompt

  1. If the above is not successful, please use the cnpmalternative
  2. The introduction on the official website says that I want to install jdk. I have not tried it without jdk. I had a jdk environment on my computer at the beginning, so I did not uninstall it. If you reported a java-like error during installation, such as what package or module could not be found, please install jdk before installing dependencies.
  3. This part of the official website has an introduction: http://nightwatchjs.org/gettingstarted#selenium-server-setup
  • Configuration

  • Create a new nightwatch.conf.jsfile in the root directory of the project , and then copy the following code into it
  • module.exports = {
      src_folders: ['examples'],
      output_folder: 'output',
      custom_assertions_path: [],
      page_objects_path: '',
      globals_path: '',
    
      selenium: {
        start_process: true,
        server_path: require('selenium-server').path,
        host: '127.0.0.1',
        port: 5555,
        cli_args: {
          'webdriver.chrome.driver': require('chromedriver').path
        }
      },
    
      test_settings: {
        default: {
          selenium_port: 5555,
          selenium_host: 'localhost',
          silent: true,
          globals: {
            devServerURL: 'http://localhost:' + (process.env.PORT || 1111)
          }
        },
    
        chrome: {
          desiredCapabilities: {
            browserName: 'chrome',
            javascriptEnabled: true,
            acceptSslCerts: true
          }
        },
    
        firefox: {
          desiredCapabilities: {
            browserName: 'firefox',
            javascriptEnabled: true,
            acceptSslCerts: true
          }
        }
      }
    }
    
    

    The first test case

     Coding test code

We do not need to use src_foldersthe directory on the configuration as a test directory.

Create a new examples folder in the root directory of the project to store our test script. Then create a new js file as a test file.
As shown:

examples
  |---01-hello-nightwatch.js

Then write our test script:

module.exports = {
  'search nightwatch on baidu': function (browser) {
    browser
      .url('http://www.baidu.com')
      .waitForElementVisible('body', 1000)
      .setValue('#kw', 'nightwatch')
      .click('#su')
      .pause(3000)
      .waitForElementVisible('#content_left', 3000)
      .end();
  }
};

The above use case simulates the process of users searching for nightwatch keywords in Baidu.

Run the use case test

We add the running script to the scripts of package.json:

"e2e": "nightwatch --env chrome"

Then run

npm run e2e

Thanks to https://blog.csdn.net/weixin_42941619/article/details/103541140   for the inspiration

Thanks to https://blog.csdn.net/qq_25324335/article/details/81990022  for the tutorial

Published 248 original articles · Like 602 · Visit 1.08 million +

Guess you like

Origin blog.csdn.net/qq_32963841/article/details/103901715