Search code examples
node.jsintegration-testingnightwatch.js

nightwatch - check for popup window, after each click event


Is there a way in nightwatch to check whether a popup window appears after each click event?

I have a problem that randomly an error message appear and I don't want to write for each click event the same callback function.

I have already tried out the after and afterEach commands in the Global.js but then the commands will only run after the whole test suite.

I also have tried it local within a test file, although it also does not cover all single click events, even though the official website writes "... while beforeEach and afterEach are ran before and after each testcase (test step)"?

Solution I'm looking for:

.waitForElementVisible('selector')
    .click('selector')  
    .click('selector')  
 

Solution I have come up with so far:

.waitForElementVisible('selector')
    .click('selector', isPresent)  
    .click('selector', isPresent)  

isPresent as a callback function, which does the checking and close the popup window if it appears.

Is there another way to write a function (with or without after and/or forEach), so that it will be called after each click event or after each command. Thus, I don't have to write the isPresent repetitive code?


Solution

  • You can insert something like this in your page object file:

    var popupCommand = {
     popupCheck:function(){
    return this.waitForElementVisible('selector', 5000)
      .click('selector', isPresent)
      .click('selector', isPresent)
    
    },
    
    module.exports = {
    commands:[popupCommand],
    elements:{
    firstElement: {selector: 'xpath',locateStrategy: 'xpath'},
    secondElement: {selector: 'css'},
    
    
     }
    }
    

    Where 'popupCommand' will be the name of your page object file, for example 'Popup'. And also you will have to insert your isPresent callback function here so you can use it. I did my best to explain you as much as possible what and how to do that :)