Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create reusable test steps in nightwatch.js?

I am looking to create reusable components within my nightwatch.js tests.

ie. login to the web app, logout of the web app

What is the best method / pattern for creating these steps in a reusable way?

like image 661
chrisphilton Avatar asked Jul 13 '15 16:07

chrisphilton


People also ask

How do you run multiple test cases on Nightwatch?

To execute tests in multiple browsers, you need to add the desired capabilities of the browsers and Test_worker configurations in nightwatch. json file. For example if you want to execute tests in three browsers parallely - Chrome, Firefox and Opera, your nightwatch. json should something like this.

How do you run a single test on Nightwatch?

A new parameter --testcase has been added to run a specified testcase. Show activity on this post. Test 2 and Test 3 will be executed. Separate each test function is mandatory because Nightwatch handled with filematcher each file.

What is Nightwatch JS used for?

js, Nightwatch. js is an open-source automated testing framework that aims at providing complete E2E (end to end) solutions to automate testing with Selenium Javascript for web-based applications, browser applications, and websites. Nightwatch.

Is Nightwatch Selenium based?

Nightwatch. js framework is a Selenium-based test automation framework, written in Node. js and uses the W3C WebDriver API (formerly Selenium WebDriver).


2 Answers

You can create custom commands for that: http://nightwatchjs.org/guide#writing-custom-commands

  1. in nightwatch.json specify the path to the folder that will contain your custom command file
  2. create a js file and name it how your custom command should be names (ie login.js)
  3. write the code you need:

exports.command = function(username, password) {
    
    this
        .waitForElementVisible('#password', 4000)
        .setValue('#password', password)
        .waitForElementVisible('#username', 1000)
        .setValue('#username', username)
        .waitForElementVisible('#sign_in', 1000)
        .click('#sign_in')
        .waitForElementVisible('h1.folder-title', 10000)
        
        return this;
};
  1. use the custom command in your test:

.login("your_username", "your_password")
like image 128
Corrinna Rainwater Avatar answered Oct 17 '22 21:10

Corrinna Rainwater


This is typically done with page objects. http://nightwatchjs.org/guide#page-objects

Then you can just

var myPage = client.page.myPage();

myPage.navigate()
  .assert.title('MyPage')
  .login('foo', 'bar)
  .assert.displayName('foo');
like image 36
Jkarttunen Avatar answered Oct 17 '22 19:10

Jkarttunen