Using Nightwatch.js as a browser-based Web application automated test

Source: Internet
Author: User
Tags browserstack

1 installation

1.1 Installing node. js

Install the installation package that is suitable for the native system on the http://nodejs.org/, and note that the NPM tool is selected in the installation options for subsequent dependent package installations.

1.2 Installing Nightwatch with NPM tools

The command line runs "NPM install Nightwatch", and the following prompts indicate that the installation was successful.

1.3 NPM Related directory structure

All NPM installed modules are based on the directory of the current CMD window, that is, if the working directory of CMD is in the C # root directory, the Node_modules folder will be created under that directory and the installed modules will be placed in that directory if the Windows accessory program or win+ R starts, the working directory is under "%userprofile%\".

The temporary files downloaded by the NPM installation are saved under "%appdata%\npm-cache".

1.4 Download Selenium Webdriver server

Http://selenium-release.storage.googleapis.com/index.html downloads the latest version of the jar package and puts it in the Nightwatch bin directory.

2 Instance Usage

Add references in 2.1 nightwatch.js

In "\node_modules\nightwatch\examples\tests\nightwatch.js", add the Reference "require ('. /.. /bin/runner.js '); "

2.2 Run Selenium Webdriver server (enter the directory where the jar is located, my directory is D:\nodejs\node_modules\nightwatch\bin, Run command "Java-jar 2.53.1- Server.jar "

2.3 Running Nightwatch.js

command line, CD to Nightwatch directory (my directory is D:\nodejs\node_modules\nightwatch), and then run "node./examples/tests/nightwatch.js"

I use the Chrome browser, I put Chromedriver.exe under directory D:\nodejs\node_modules\nightwatch\bin, Nightwatch.json configuration file as follows:

{"Src_folders": ["./examples/tests"], "Output_folder": "./examples/reports", "Custom_commands_path": "./examples/cu Stom-commands "," Page_objects_path ":"./examples/pages "," Custom_assertions_path ":" "," Globals_path ":" "," Live_ou Tput ": false," Parallel_process_delay ": Ten," disable_colors ": false," test_workers ": false," Selenium ": {" star T_process ": false," Server_path ":" "," Log_path ":" "," host ":" 127.0.0.1 "," Port ": 4444," Cli_args ":  {"Webdriver.chrome.driver": "./", "Webdriver.ie.driver": "", "Webdriver.firefox.profile": ""}}, "Test_settings": {"default": {"Launch_url": "http://localhost", "Selenium_host": "127.0.0.1", "sel        Enium_port ": 4444," silent ": true," disable_colors ": false," screenshots ": {" Enabled ": false,       "Path": ""}, "desiredcapabilities": {"Browsername": "Chrome", "javascriptenabled": true, "Acceptsslcerts": true}, "Chrome": {"desiredcapabilities": {"Browsername": "Chrome"       }}, "Saucelabs": {"Selenium_host": "Ondemand.saucelabs.com", "Selenium_port": "Usernam" E ":" ${sauce_username} "," Access_key ":" ${sauce_access_key} "," Use_ssl ": false," silent ": true," O      Utput ": True," screenshots ": {" Enabled ": false," on_failure ": True," path ":" "},        "Desiredcapabilities": {"name": "Test-example", "Browsername": "Firefox"}, "Globals": {  "Myglobal": "Some_sauce_global"}, "Selenium": {"start_process": false}}, "Phantomjs" : {"desiredcapabilities": {"Browsername": "Phantomjs", "javascriptenabled": true, "accepts Slcerts ": True," Phantomjs.binary.path ":"/path/to/phantomjs "}}," Browserstack ": {" Selenium "      : {"start_process": false}, "Selenium_host": "Hub.browserstack.com", "Selenium_port": 80, "Silent": true, "DEsiredcapabilities ": {" name ":" Test-example "," Browsername ":" Firefox "," Browserstack.user ":" ... "      , "Browserstack.key": "..."}, "Testingbot": {"Selenium_host": "Hub.testingbot.com",  "Selenium_port": "ApiKey": "${tb_key}", "Apisecret": "${tb_secret}", "silent": true, "output" : True, "screenshots": {"Enabled": false, "on_failure": True, "path": ""}, "des Iredcapabilities ": {" name ":" Test-example "," Browsername ":" Firefox "}," Selenium ": {" s Tart_process ": False}}}}

  

2.3 Exception Handling

If no accident, the implementation of the above JS will be thrown like the following exception, do not panic, according to the exception prompt, install the required module, installation method "NPM Install XXX".

3 Fundamentals

4 Test Kit

Nightwatch.js makes it possible to organizedyour test scripts into groups and run them as needed. To group tests Togetherjust place them in the same sub-folder. The folder name is the name of Thegroup. For example, the following directory structure.

5 of your own scripts

Build a file named Test.js in the Nightwatch root directory:

Require ('./bin/runner.js ');

var Nightwatch = require ('./index.js ');

Module.exports = {

"Step One": function (browser) {

Browser

. URL ("http://www.google.com.hk")

. waitforelementvisible (' body ', 1000)

. SetValue (' input[type=text] ', ' Nightwatch ')

. Waitforelementvisible (' button[name=btng] ', 1000)

},

"Step": function (browser) {

Browser

. Click (' button[name=btng] ')

. Pause (1000)

. Assert.containstext (' #main ', ' The Night Watch ')

. end ();

}

};

Then "node./test.js" Runs:

For more use see its API documentation: HTTP://NIGHTWATCHJS.ORG/API

6 Product Features

? Simple but powerful syntax which enables the write tests veryquickly, using only JavaScript and CSS selectors. No need to initialize otherobjects and classes, you have need to write the test specs.

? Built-in command-line test runner which enables you to run the Testseither altogether, by group or single.

? Manages the Selenium server automatically; Can is disabled Ifselenium runs on another machine.

? Continous integration Support:junit XML reporting is built-in soyou can integrate your tests in your build process with S Ystems Suchs as Hudsonor Teamcity.

? Use CSS selectors or Xpath to locate and verify elements on the Pageor execute commands.

? Easy to extend if you need to implement your own commands SpecificTo your application.

Using Nightwatch.js as a browser-based Web application automated test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.