Skip to content

Commit

Permalink
Merge pull request #36 from softrams/new-steps
Browse files Browse the repository at this point in the history
new-steps: added new steps for web table cell element and file uploads
  • Loading branch information
mkmurali authored Sep 1, 2023
2 parents e57f632 + 338b66d commit 06a9db5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
44 changes: 38 additions & 6 deletions lib/_pageElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,29 @@ async function getFileContent(path) {
return fs.existsSync(path) ? await readFile(path, 'utf8') : null;
}

exports.getPageElement = async (taiko, element) => {
const _element = await getHelper(taiko, element);
if (_element !== element) {
return await _element;
async function tableCellElement(taiko, element) {
if (element.startsWith("table")) {
const tableCellObject = JSON.parse(element.replace("table", "").trim());
const tableElementValue = tableCellObject["tableElement"];
const columnNameValue = tableCellObject["columnName"];
const rowValue = tableCellObject["row"];
const tableElement = await getElement(taiko, tableElementValue);
const tableElementLocatorValue = tableElement.description.replace("CustomSelector with query", "").trim();
const columnHeaders = await taiko.$(tableElementLocatorValue + "//thead//th").elements();
let columnIndex = 0;
for (let index in columnHeaders) {
const columnHeader = (await columnHeaders[index].text()).toString();
if (columnHeader === columnNameValue) {
columnIndex = ++index;
break;
}
}
return await taiko.$("(" + tableElementLocatorValue + "//tbody//tr[" + rowValue + "])//td[" + columnIndex + "]");
}

return element;
}

async function getElement(taiko, element) {
let elementText;
if (element.includes("with text")) {
const elementParts = element.split("with text");
Expand All @@ -153,13 +170,28 @@ exports.getPageElement = async (taiko, element) => {
if (hasElement) {
let elementLocatorValue = fileObject[element];
elementLocatorValue = elementText ?
elementLocatorValue.replace("{text}", elementText) : elementLocatorValue;
elementLocatorValue.replaceAll("{text}", elementText) : elementLocatorValue;

return await taiko.$(elementLocatorValue);
}

return await element;
}

exports.getPageElement = async (taiko, element) => {
let _element = await getHelper(taiko, element);
if (_element !== element) {
return await _element;
}

_element = await tableCellElement(taiko, element);
if (_element !== element) {
return await _element;
}

return await getElement(taiko, element);
}

exports.checkElementState = async (taiko, element, elementState) => {
const pageElement = await this.getPageElement(taiko, element);
switch (elementState) {
Expand Down
16 changes: 13 additions & 3 deletions lib/page_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const taiko = require("taiko");
const assert = require("assert");
const selectors = require("./_selectors");
const helpers = require("./_helpers");
const path = require("path");
const { getPageElement, checkElementState, scrollToElementView } = require("./_pageElement");

step(["Goto <url>", "Open <url>", "Goto <url> in new tab", "Open <url> in new tab"], async url => {
Expand Down Expand Up @@ -368,7 +369,7 @@ step(["Click <element> <elementText>", "Click on <element> <elementText>", "Clic
});

step("Clear <element> text box", async element => {
await taiko.clear(await getPageElement(taiko, element))
await taiko.clear(await getPageElement(taiko, element));
});

step(["Enter <value> to <element> text box", "Enter <value> to <element>"], async (value, element) => {
Expand Down Expand Up @@ -401,8 +402,7 @@ step("Verify <element> text is <elementText>", async (element, elementText) => {
elementText = helpers.getValue(elementText);
gauge.message("Verifying " + element + " text contains " + elementText);

await taiko.waitFor(element + ' did not contain text ' + elementText,
async () => (await (await getPageElement(taiko, element)).text()).toString()
await taiko.waitFor(async () => (await (await getPageElement(taiko, element)).text()).toString()
.replaceAll("\\s+", " ")
.replaceAll("\n", " ")
.includes(elementText));
Expand All @@ -419,6 +419,16 @@ step(["Save <element> as <elementTextKey>", "Save <element> text as <elementText
gauge.message("Saved " + elementText + " as " + elementTextKey);
});

/* file actions steps 👇 */

step("Upload <filePath> file to <element>", async (filePath, element) => {
const fileAbsolutePath = process.env.file_upload_directory && !path.isAbsolute(filePath) ?
path.join(process.env.file_upload_directory, filePath) : path.join(filePath);
await taiko.attach(fileAbsolutePath, await getPageElement(taiko, element));
});

/* file actions steps 👆 */

step(["Wait <seconds> seconds", "Wait for <seconds> seconds"], async seconds => {
await taiko.waitFor(seconds * 1000);
});
7 changes: 3 additions & 4 deletions lib/page_steps_continue_on_failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ step(["Verify <element> <elementText> <elementState>", "Verify <element> <elem
await checkElementState(taiko, element, elementState);
});

step("Verify <element> text is <elementText>",
{continueOnFailure: true}, async (element, elementText) => {
step("Verify <element> text is <elementText>",
{continueOnFailure: true}, async (element, elementText) => {
elementText = helpers.getValue(elementText);
gauge.message("Verifying " + element + " text contains " + elementText);

await taiko.waitFor(element + ' did not contain text ' + elementText,
async () => (await (await getPageElement(taiko, element)).text()).toString()
await taiko.waitFor(async () => (await (await getPageElement(taiko, element)).text()).toString()
.replaceAll("\\s+", " ")
.replaceAll("\n", " ")
.includes(elementText))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@softrams/gauge-taiko-steps",
"version": "0.1.3",
"version": "0.1.4",
"description": "Implementation of common test steps with Taiko driver for writing tests with Gauge framework",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 06a9db5

Please sign in to comment.