I'm currently trying to perform parameterised testing using Mocha in Selenium.
I'm passing multiple values into a text-box(s). When using simple arrays, I'm able to pass the array data in sendKeys()
function and successfully complete the test (refer below code):
const {By,Key,Builder} = require("selenium-webdriver");
require("edgedriver");
const assert = require("assert");
describe("dyndataTest1", function() {
const URL = "https://www.google.com/";
const colors = ["blue", "green","red"];
colors.forEach((googlesearch) => {
it(`googlesearch1 for search key - ${googlesearch}`, async function() {
let driver = await new Builder().forBrowser('MicrosoftEdge').build();
await driver.manage().setTimeouts({implicit:10000});
await driver.get(URL);
await driver.findElement(By.name("q")).sendKeys(googlesearch, Key.RETURN);
await driver.findElement(By.id("APjFqb")).clear();
await driver.findElement(By.id("APjFqb")).sendKeys(googlesearch, Key.RETURN);
let verifyText = await driver.getTitle().then (function (value) {
return value;
});
console.log(verifyText);
assert.strictEqual(verifyText, googlesearch + " - Google Search");
await driver.quit();
});
});
});
I tried the same again, but this time passed an array of objects instead of a simple array, using similar code below:
const {By,Key,Builder} = require("selenium-webdriver");
require("edgedriver");
const assert = require("assert");
const colors = [
{ googlesearch1:"blue", googlesearch2:"green", googlesearch3:"red" },
{ googlesearch1:"sky_blue", googlesearch2:"forest_green", googlesearch3:"brick_red" },
];
describe("dyndataTest2", function() {
const URL = "https://www.google.com/";
colors.forEach(({googlesearch1, googlesearch2, googlesearch3}) => {
it(`googlesearch for search key - ${googlesearch1},${googlesearch2},${googlesearch3}`, async function() {
let driver = await new Builder().forBrowser('MicrosoftEdge').build();
await driver.manage().setTimeouts({implicit:10000});
await driver.get(URL);
await driver.findElement(By.name("q")).sendKeys(googlesearch1, Key.RETURN);
await driver.findElement(By.id("APjFqb")).clear();
await driver.findElement(By.id("APjFqb")).sendKeys(googlesearch2, Key.RETURN);
await driver.findElement(By.id("APjFqb")).clear();
await driver.findElement(By.id("APjFqb")).sendKeys(googlesearch3, Key.RETURN)
let verifyText = driver.getTitle().then (function (value) {
return value;
});
console.log(verifyText);
assert.strictEqual(verifyText, googlesearch3 + " - Google Search");
await driver.quit();
});
});
});
However, on running the 2nd code block, I'm getting the output in console as Promise { <pending> }
, which means I have to return the promise to Mocha, which I'm unsure of how to insert in the code when using forEach()
loop.
Here is the console output:
dyndataTest2
Promise { <pending> }
1) googlesearch for search key - blue,green,red
Promise { <pending> }
2) googlesearch for search key - sky_blue,forest_green,brick_red
0 passing (34s)
2 failing
1) dyndataTest2
googlesearch for search key - blue,green,red:
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected
+ Promise {
+ <pending>
+ }
- 'red - Google Search'
at Context.<anonymous> (Test Scripts/dyndataTest2.js:33:20)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2) dyndataTest2
googlesearch for search key - sky_blue,forest_green,brick_red:
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected
+ Promise {
+ <pending>
+ }
- 'brick_red - Google Search'
at Context.<anonymous> (Test Scripts/dyndataTest2.js:33:20)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Can someone tell me where and how I should insert the promise callback to fix this code?
What I require is to perform the following set of identical tests in succession using one loop:
- Open browser | launch google | search: blue (clear search) , green (clear search) , red | Close browser
- Open browser | launch google | search: sky_blue (clear search) , forest_green (clear search) , brick_red | Close browser