Skip to content

Tech Tinker Lab

Journal for tech tinkering experiments

  • Home
  • Basics
    • Coding
  • Game Development
    • Unity
    • Blender
  • Web Development
    • WordPress
    • Shopify
    • Low/No Code
      • Microsoft Power Pages
  • Automation
    • Testing Automation
      • Unit Testing
      • End to End Testing
        • Nightwatch
  • Productivity
    • GSuite
      • GSheets
  • Toggle search form
basic auth nightwatch

Basic Authentication Nightwatch

Posted on June 14, 2022June 14, 2022 By techtinkerer No Comments on Basic Authentication Nightwatch

In this article we will learn how to to basic authentication using Nightwatch.

We will use the demo app called “The Internet” that has a use case around basic authentication

The demo app can be found here.

Use Case – Basic Authentication Nightwatch

Let us try to understand the use case. This case will be very common when testing websites that contain a basic authentication mechanism.

Before we do the automation setup, let us implement the use case manually.

Steps

  • Go to “The Internet” website
  • Click on “Basic Authentication” (Note that the username and password are mentioned as admin)
  • Enter admin in the username field
  • Enter admin in the password field
  • Click on Sign In

You have successfully logged in using to a website using basic authentication manually. Now let’s proceed towards setting this up via an automation script using Nightwatch.

Script – Basic Authentication Nightwatch

If you directly open the URL using the navigateTo command, the script will get stuck where the basic authentication alert is shown. It will not enter the user name and password and you will not be able to continue your testing.

In order to avoid this issue, you have to pass the credentials in the http request headers. This can be done by using a command called registerBasicAuth(‘username’,’password’). Reference

You will have to register the basic authentication before opening the URL.

describe('basic authentication', function() {
    it('Open URL and do Basic Authentication', function(browser) {
      browser.registerBasicAuth('admin', 'admin');

      browser
        .navigateTo('http://the-internet.herokuapp.com/basic_auth')
        .assert.textContains('#content .example h3','Basic Auth')
        .assert.textContains('#content .example p','Congratulations');
    }); 
});

Before opening the URL that contains basic authentication, we registered the basic authentication credentials as

  • username – admin
  • password – admin

Once this is done, in the subsequent requests, the basic authentication will be passed in the header requests and hence the test will not be stuck at the point where credentials are asked.

Conclusion

Basic authentication is a breeze with Nightwatch. You simply have to register the credentials and proceed with the testing of the website. The framework will handle the rest.

End to End Testing, Nightwatch, Testing Automation

Post navigation

Previous Post: Power Pages Tutorial – Building Pages
Next Post: Nightwatch Shadow DOM

Related Posts

nightwatch shadow dom Nightwatch Shadow DOM End to End Testing
test doubles Test Doubles – Dummies, Fakes, Stubs & Spies Testing Automation

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • January 2023
  • September 2022
  • June 2022
  • May 2022

Categories

  • Basics
  • Blender
  • Coding
  • End to End Testing
  • Game Development
  • GSheets
  • GSuite
  • Nightwatch
  • No/Low Code
  • Power Pages
  • Productivity
  • Testing Automation
  • Unit Testing
  • Web Development
  • Wordpress
  • How to get Indian Mutual Fund Portfolio in Google Sheets in 2023?
  • Learn Blender from scratch with Imphenzia
  • Power Pages Form
  • Styling with Power Pages
  • Nightwatch Shadow DOM

Copyright © 2023 Tech Tinker Lab.

Powered by PressBook Masonry Blogs