serkan
serkan

Reputation: 7141

nightwatchjs, run same test on multiple pages

I have written some tests for my homepage but the tests are very generic, like footer, header checking.

My test structure is like:

const footerCheck = function(browser){ 
    browser.url("example.com");
    browser.verify.elementPresent(".footer-top", "Footer-top is present.")
    browser.verify.elementPresent(".footer-middle", "Legal notice bar is present")
    browser.verify.elementPresent(".footer-bottom", "Copyright bar is present")
    }

export.module = {
"Footer Check" : footerCheck
}

Lets say I have 100 pages. I would like to run footerCheck function run on all hundred pages.

URLs like example.com/page1 , example.com/page2 , example.com/page3...

Since all the tests are valid for other pages I would like to loop all pages for the same test cases. Somehow could not get my head around it.

How is that possible, any help would be appreciated.

Thanks

Upvotes: 0

Views: 781

Answers (1)

Frankusky
Frankusky

Reputation: 1083

In my personal experience, the best way to do BDD is adding cucumber that uses gherkin syntax. It is clearer and helps a lot to reduce redundant code if you know to use it well. There is a Nightwatch npm plugin to add cucumber, once you have added it you have to create your .feature file like the following

Feature: Check elements are present
Scenario Outline:
Given the user enters on a <page>
Then .footer-top, .footer-middle and .footer-bottom class should be enabled

Examples:
|page|
|page.com/page1|
|page.com/page2|
|page.com/page3|

And your step definitions (where you declare what will do each step) it automatically will run each step for each url provided in the examples (note the <page> flag that will be replaced on the example, first row is the name of the tag).

Take a look to the examples

Upvotes: 1

Related Questions