I am using Selenium Webdriver on Node.js with Cucumber.js.
I want to run the same test on multiple pages. In this case just checking for 404s in my footer.
My Cucumber .feature file looks like:
Feature: Check footer links
Scenario: Check for broken links in the footer section
Given I am checking the footer on the ‘<page>’ page
Then there should be no broken links in the footer on ‘<page>’ page
Examples:
| page |
| about-us |
| contact-us |
| products |
And in my steps.js file I have:
const { When, Then, Given, AfterAll } = require(‘@cucumber/cucumber’);
const assert = require(‘assert’);
const { Builder, By, until, Key, http } = require(‘selenium-webdriver’);
const firefox = require(‘selenium-webdriver/firefox’);
const XMLHttpRequest = require(‘xhr2’);
var {setDefaultTimeout} = require(‘@cucumber/cucumber’);
setDefaultTimeout(60 * 1000);
Given(‘I am checking the footers on the {string} page’, async function (string) {
this.driver = new Builder()
.forBrowser(‘firefox’)
.build();
this.driver.wait(until.elementLocated(By.className(‘logo-image’)));
await this.driver.get(‘https://www.some-site.com/’ + string);
});
Then(‘there should be no broken links in the footer on {string} page’, async function(string) {
var urlArr = [];
var footerLinks = await this.driver.findElements(By.css(‘.footer a’));
for (let i = 0; i < footerLinks.length; i++) {
var url = await footerLinks[i].getAttribute(“href”);
urlArr.push(url);
}
if (urlArr.length < 1) {
console.log(`Could not find any footer links on ${string} page`);
}
else {
for (let i = 0; i < urlArr.length; i++) {
var respStatus = await checkLink(urlArr[i]);
assert.ok(respStatus==200);
}
}
});
function checkLink(url) {
//Check link function returns status code….
}
This works fine, but for one thing: For each page that is tested a new instance of FireFox opens up.
I tried adding
After(async function() {
this.driver.quit();
});
To the end but this closes the session completely and the other tests fail after the first initial one is done.
Would anyone know how I could repeat the tests in the same browser instance?