fbpx
Uncategorized

How To Wait For Page Load In Selenium And Use Runtime Assertions In Javascript With Nightwatch

Today we will take a look at runtime assertions while using the Javascript programming language with Nightwatch framework. This feature is very useful in various applications, but, for example, we will take a look at one of the most popular use cases for it in Loadero – using runtime assertion to wait for page load in Selenium and execute an assert after. By using runtime assertions, you minimize dependence on post-run asserts and manual log analysis. This can save you a great amount of time working with the results of your test run. We will take a look at what runtime assertion is and how to start using it to your benefit.

Runtime assertion is similar to regular asserts which validate data, but what sets them apart is the ability to assert data values during real-time script execution. Meaning we can validate specific data points at different parts of execution flow and once you get the results of the test run, you immediately see where the asserts you have set failed. Runtime assertions are extremely useful when checking if web elements are loaded. For example, a common use case could be some element not appearing on the screen under high load, or to check if some web element has loaded, to verify that the app has reached a specific state. With a runtime assertion in your script, you can immediately see which participants failed to get the element visible.

Prerequisites:

  • Active Loadero account (you can create one here, it’s free).
  • Logged in Loadero.com with your active account.
  • A project, a test with all of the necessary structure and at least one participant created for a test run, as we will be just working with script and results today.

So let’s get the party started. There are two distinct ways of using Nightwatch to do runtime assertions:

  • .assert – when this assertion fails, test ends while skipping all other assertions
  • .verify – when this assertion fails, test logs the failure and continues with test execution

To learn more about assertions check out Nightwatch documentation. A lot of methods from Node.js assert module are also available using Nightwatch runtime asserts.

Let’s move on to how we can actually use runtime asserts in Loadero. We just need to call the appropriate runtime assert in our test script. So for today’s examples we’ll be using a simple test script, which will open a page, wait for it to load and then assert the active page URL to our provided value and wait 10 seconds after assertion. The particular values for test, group and participant are not that important in this example, except the test script.

Assert use examples

Let’s take a look at four different scenarios and their output in participant Selenium log. These examples will open a page and wait until page body is visible then execute the runtime assertion and if possible pause for 10 seconds:

  1. .assert positive – will output a successful assertion message and continue test execution without any problems.

Code:

function(client) {
  client
    .url('https://www.google.com/')
    .waitForElementVisible('body', 10 * 1000)
    .assert.urlEquals('https://www.google.com/')
    .pause(10 * 1000);
}
Selenium log:
Running:  client test
→ Running [beforeEach]:
→ Completed [beforeEach].
→ Running command: url ('https://www.google.com/')
  Request POST  /wd/hub/session/29a2347148fdd7770497c0117b938a87/url 
  { url: 'https://www.google.com/' }
  Response 200 POST /wd/hub/session/29a2347148fdd7770497c0117b938a87/url (248ms)
  { sessionId: '29a2347148fdd7770497c0117b938a87',
    status: 0,
    value: null }
→ Completed command url ('https://www.google.com/') (249ms)
→ Running command: waitForElementVisible ('body', 10000)
  Request POST  /wd/hub/session/29a2347148fdd7770497c0117b938a87/elements 
  { using: 'css selector', value: 'body' }
  Response 200 POST /wd/hub/session/29a2347148fdd7770497c0117b938a87/elements (13ms)
  { sessionId: '29a2347148fdd7770497c0117b938a87',
    status: 0,
    value: [ { ELEMENT: '0.6675568316887011-1' } ] }
  Request GET  /wd/hub/session/29a2347148fdd7770497c0117b938a87/element/0.6675568316887011-1/displayed 
  Response 200 GET /wd/hub/session/29a2347148fdd7770497c0117b938a87/element/0.6675568316887011-1/displayed (10ms)
  { sessionId: '29a2347148fdd7770497c0117b938a87',
    status: 0,
    value: true }
✔ Element  was visible after 25 milliseconds.
→ Completed command waitForElementVisible ('body', 10000) (28ms)
→ Running command: assert.urlEquals ('https://www.google.com/')
→ Running command: url ([Function])
  Request GET  /wd/hub/session/29a2347148fdd7770497c0117b938a87/url 
  Response 200 GET /wd/hub/session/29a2347148fdd7770497c0117b938a87/url (5ms)
  { sessionId: '29a2347148fdd7770497c0117b938a87',
    status: 0,
    value: 'https://www.google.com/' }
✔ Testing if the URL equals "https://www.google.com/"  - 6 ms.
→ Completed command url ([Function]) (5ms)
→ Completed command assert.urlEquals ('https://www.google.com/') (8ms)
→ Running command: pause (10000)
→ Completed command pause (10000) (10001ms)
→ Running [afterEach]:
→ Completed [afterEach].
OK. 2 assertions passed. (10.287s)
  1. .assert negative – will output failure messages, and stop test execution immediately. This will result for Loadero participants to display that test failed because of Selenium failure.

Code:

function(client) {
  client
    .url('https://www.google.com/')
    .waitForElementVisible('body', 10 * 1000)
    .assert.urlEquals('https://www.bing.com/')
    .pause(10 * 1000);
}
Selenium log:
Running:  client test
 → Running [beforeEach]:
 → Completed [beforeEach].
 → Running command: url ('https://www.google.com/')
   Request POST  /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/url  
   { url: 'https://www.google.com/' }
   Response 200 POST /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/url (291ms)
   { sessionId: '6de5de11548da789c80be9ad9ed71bc0',
     status: 0,
     value: null }
 → Completed command url ('https://www.google.com/') (292ms)
 → Running command: waitForElementVisible ('body', 10000)
   Request POST  /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/elements  
   { using: 'css selector', value: 'body' }
   Response 200 POST /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/elements (12ms)
   { sessionId: '6de5de11548da789c80be9ad9ed71bc0',
     status: 0,
     value: [ { ELEMENT: '0.28723508654107777-1' } ] }
   Request GET  /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/element/0.28723508654107777-1/displayed  
   Response 200 GET /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/element/0.28723508654107777-1/displayed (37ms)
   { sessionId: '6de5de11548da789c80be9ad9ed71bc0',
     status: 0,
     value: true }
✔ Element  was visible after 51 milliseconds.
 → Completed command waitForElementVisible ('body', 10000) (53ms)
 → Running command: assert.urlEquals ('https://www.testdevlab.com/')
 → Running command: url ([Function])
   Request GET  /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/url  
   Response 200 GET /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/url (4ms)
   { sessionId: '6de5de11548da789c80be9ad9ed71bc0',
     status: 0,
     value: 'https://www.google.com/' }
 → Completed command url ([Function]) (5ms)
 → Running command: url ([Function])
   Request GET  /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/url  
   Response 200 GET /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/url (5ms)
   { sessionId: '6de5de11548da789c80be9ad9ed71bc0',
     status: 0,
     value: 'https://www.google.com/' }
 → Completed command url ([Function]) (5ms)
 → Running command: url ([Function])
   Request GET  /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/url  
   Response 200 GET /wd/hub/session/6de5de11548da789c80be9ad9ed71bc0/url (5ms)
   { sessionId: '6de5de11548da789c80be9ad9ed71bc0',
     status: 0,
     value: 'https://www.google.com/' }
✖ Testing if the URL equals "https://www.testdevlab.com/" in 1000 ms. - expected "https://www.testdevlab.com/" but got: "https://www.google.com/"
    at Object.client test (/opt/loadero/static/resources/4788/script.js:157:13)
    at process._tickCallback (internal/process/next_tick.js:68:7) 
 → Completed command url ([Function]) (7ms)
 → Completed command assert.urlEquals ('https://www.testdevlab.com/') (1023ms)
 → Running [afterEach]:
 → Completed [afterEach].
FAILED: 1 assertions failed and  1 passed (1.37s)
  1. .verify positive – will output a successful assertion message and continue test execution without any problems.

Code:

function(client) {
  client
    .url('https://www.google.com/')
    .waitForElementVisible('body', 10 * 1000)
    .verify.urlEquals('https://www.google.com/')
    .pause(10 * 1000);
}
Selenium log:
Running:  client test
→ Running [beforeEach]:
→ Completed [beforeEach].
→ Running command: url ('https://www.google.com/')
  Request POST  /wd/hub/session/06b00507cbc8f5416628326897edf871/url 
  { url: 'https://www.google.com/' }
  Response 200 POST /wd/hub/session/06b00507cbc8f5416628326897edf871/url (242ms)
  { sessionId: '06b00507cbc8f5416628326897edf871',
    status: 0,
    value: null }
→ Completed command url ('https://www.google.com/') (242ms)
→ Running command: waitForElementVisible ('body', 10000)
  Request POST  /wd/hub/session/06b00507cbc8f5416628326897edf871/elements 
  { using: 'css selector', value: 'body' }
  Response 200 POST /wd/hub/session/06b00507cbc8f5416628326897edf871/elements (13ms)
  { sessionId: '06b00507cbc8f5416628326897edf871',
    status: 0,
    value: [ { ELEMENT: '0.803859889153755-1' } ] }
  Request GET  /wd/hub/session/06b00507cbc8f5416628326897edf871/element/0.803859889153755-1/displayed 
  Response 200 GET /wd/hub/session/06b00507cbc8f5416628326897edf871/element/0.803859889153755-1/displayed (11ms)
  { sessionId: '06b00507cbc8f5416628326897edf871',
    status: 0,
    value: true }
✔ Element  was visible after 26 milliseconds.
→ Completed command waitForElementVisible ('body', 10000) (28ms)
→ Running command: verify.urlEquals ('https://www.google.com/')
→ Running command: url ([Function])
  Request GET  /wd/hub/session/06b00507cbc8f5416628326897edf871/url 
  Response 200 GET /wd/hub/session/06b00507cbc8f5416628326897edf871/url (48ms)
  { sessionId: '06b00507cbc8f5416628326897edf871',
    status: 0,
    value: 'https://www.google.com/' }
✔ Testing if the URL equals "https://www.google.com/"  - 48 ms.
→ Completed command url ([Function]) (48ms)
→ Completed command verify.urlEquals ('https://www.google.com/') (49ms)
→ Running command: pause (10000)
→ Completed command pause (10000) (10000ms)
→ Running [afterEach]:
→ Completed [afterEach].
OK. 2 assertions passed. (10.322s)
  1. .verify negative – will output a failure message, but instead of stopping test it will continue test execution, but the end result for Loadero participants will be Selenium failure.

Code:

function(client) {
  client
    .url('https://www.google.com/')
    .waitForElementVisible('body', 10 * 1000)
    .verify.urlEquals('https://www.bing.com/')
    .pause(10 * 1000);
}
Selenium log:
Running:  client test
→ Running [beforeEach]:
→ Completed [beforeEach].
→ Running command: url ('https://www.google.com/')
  Request POST  /wd/hub/session/084ca84d6a42e63236e010f13221dfda/url 
  { url: 'https://www.google.com/' }
  Response 200 POST /wd/hub/session/084ca84d6a42e63236e010f13221dfda/url (241ms)
  { sessionId: '084ca84d6a42e63236e010f13221dfda',
    status: 0,
    value: null }
→ Completed command url ('https://www.google.com/') (241ms)
→ Running command: waitForElementVisible ('body', 10000)
  Request POST  /wd/hub/session/084ca84d6a42e63236e010f13221dfda/elements 
  { using: 'css selector', value: 'body' }
  Response 200 POST /wd/hub/session/084ca84d6a42e63236e010f13221dfda/elements (12ms)
  { sessionId: '084ca84d6a42e63236e010f13221dfda',
    status: 0,
    value: [ { ELEMENT: '0.006930492376904018-1' } ] }
  Request GET  /wd/hub/session/084ca84d6a42e63236e010f13221dfda/element/0.006930492376904018-1/displayed 
  Response 200 GET /wd/hub/session/084ca84d6a42e63236e010f13221dfda/element/0.006930492376904018-1/displayed (10ms)
  { sessionId: '084ca84d6a42e63236e010f13221dfda',
    status: 0,
    value: true }
✔ Element  was visible after 24 milliseconds.
→ Completed command waitForElementVisible ('body', 10000) (26ms)
→ Running command: verify.urlEquals ('https://www.testdevlab.com/')
→ Running command: url ([Function])
  Request GET  /wd/hub/session/084ca84d6a42e63236e010f13221dfda/url 
  Response 200 GET /wd/hub/session/084ca84d6a42e63236e010f13221dfda/url (6ms)
  { sessionId: '084ca84d6a42e63236e010f13221dfda',
    status: 0,
    value: 'https://www.google.com/' }
→ Completed command url ([Function]) (7ms)
→ Running command: url ([Function])
  Request GET  /wd/hub/session/084ca84d6a42e63236e010f13221dfda/url 
  Response 200 GET /wd/hub/session/084ca84d6a42e63236e010f13221dfda/url (5ms)
  { sessionId: '084ca84d6a42e63236e010f13221dfda',
    status: 0,
    value: 'https://www.google.com/' }
→ Completed command url ([Function]) (5ms)
→ Running command: url ([Function])
  Request GET  /wd/hub/session/084ca84d6a42e63236e010f13221dfda/url 
  Response 200 GET /wd/hub/session/084ca84d6a42e63236e010f13221dfda/url (5ms)
  { sessionId: '084ca84d6a42e63236e010f13221dfda',
    status: 0,
    value: 'https://www.google.com/' }
✖ Testing if the URL equals "https://www.testdevlab.com/" in 1000 ms. - expected "https://www.testdevlab.com/" but got: "https://www.google.com/"
    at Object.client test (/opt/loadero/static/resources/4787/script.js:157:13)
    at process._tickCallback (internal/process/next_tick.js:68:7)
→ Completed command url ([Function]) (7ms)
→ Completed command verify.urlEquals ('https://www.testdevlab.com/') (1025ms)
→ Running command: pause (10000)
→ Completed command pause (10000) (10010ms)
→ Running [afterEach]:
→ Completed [afterEach].
FAILED: 1 assertions failed and  1 passed (11.305s)

The example we looked at today was a basic runtime assertion use case but a popular one. You might need to wait for page load in Selenium, make an assertion and continue on with the script execution in your load, performance or session record tests. But there are also other more advanced use cases, where runtime asserts can save your time. Learn to use assert preconditions and post-run assertions as well to get the best out of Loadero use for your needs. If you have any difficulties understanding or using these features, you can always contact our support for help. Try using Loadero for free, runtime assertions are included in our free plan along with all other Loadero features.

Write A Comment