Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip login on each nightwatch test

Each of my Nightwatch.js tests requires authentication in order to run and for the time being I can't make authentication to be done via cookies (of course if test fill in a username and password it logs in, but it takes time this way)

I have a working PHPSESSID (tested in Fiddler) cookie and trying to set it via nightwatch setCookie function like this:

browser
    .setCookie({
        name     : "PHPSESSID",
        value    : "gfnpqlflvlrkd2asj18ja2ewrt",
        path     : "/admin", //(Optional)
        domain   : "example.com", //(Optional)
        secure   : true, //(Optional)
        httpOnly : false // (Optional)           
    })        
    .url("www.example.com/admin")

however www.example.com/admin redirects me back to www.example.com/login meaning authentication didn't pass.

Is there any solution?

like image 834
Todo Avatar asked Oct 08 '15 12:10

Todo


1 Answers

I had this issue as well and just ended up creating a custom command. You'll need to create a new folder called custom-commands within your nightwatch test folder. In your nightwatch.JSON file you will need to specify the path to this folder like so:

 "custom_commands_path" : "./nightwatch-tests/custom-commands"

Create a new test filed in this new folder with the login steps and use the name as your new command at the beginning of each test. For example, my file is called "login.js" and it looks like this:

var load_speed = 5000,
local_login_url = "https://";

exports.command = function(username, password) {

  this
    .url(local_login_url)
    .waitForElementVisible('.login', load_speed)
    .setValue('input[type=text]', username)
    .setValue('input[type=password]', password)
    .click('input[type=submit]')
    .pause(load_speed)
    .assert.elementPresent('.dashboard')

  return this;
};

When I call the custom command in my test, it looks like this:

.login(username, password)
like image 172
tracyak13 Avatar answered Oct 05 '22 01:10

tracyak13