-
-
Save rajaraodv/152da1053ac101736ce22449aa5dee72 to your computer and use it in GitHub Desktop.
//Note: This is just an example in JavaScript. This shows how to print and also how to call that method. | |
//Feel free to change it to your language and framework needs | |
const fs = require('fs'); | |
// Get the browser, viewport and device info from a variable like below or from a file or environment variable. | |
const browser = "Firefox"; | |
const viewport = "1200x700"; | |
const device = "Laptop"; | |
/** | |
* A Helper to print the test result in the following format: | |
* Task: <Task Number>, Test Name: <Test Name>, DOM Id:: <id>, Browser: <Browser>, Viewport: <Width x Height>, Device<Device type>, Status: <Pass | Fail> | |
* | |
* Example: Task: 1, Test Name: Search field is displayed, DOM Id: DIV__customsear__41, Browser: Chrome, Viewport: 1200 x 700, Device: Laptop, Status: Pass | |
* | |
* @param task int - 1, 2 or 3 | |
* @param testName. string - Something meaningful. E.g. 1.1 Search field is displayed | |
* @param domId string - DOM ID of the element | |
* @param comparisonResult boolean - The result of comparing the "Expected" value and the "Actual" value. | |
* @return boolean - returns the same comparison result back so that it can be used for further Assertions in the test code. | |
*/ | |
function hackathonReporter(task, testName, domId, comparisonResult) { | |
fs.appendFileSync('Traditional-V1-TestResults.txt', `"Task: ${task}, Test Name: ${testName}, DOM Id: ${domId}, Browser: ${browser}, Viewport: ${viewport}, Device: ${device}, Status: ${(comparisonResult ? "Pass" : "Fail")}\n`); | |
return comparisonResult; | |
} | |
describe('Task 1 - Header location', function() { | |
it('Search field should be displayed', async () => { | |
var isDisplayed = await driver.findElement(By.id('DIV__customsear__41')).isDisplayed(); | |
assert.assertTrue(hackathonReporter(1, "Search field is displayed", 'DIV__customsear__41', isDisplayed)); | |
}); | |
it('Search Icon should be displayed', async () => { | |
var isDisplayed = await driver.findElement(By.id('DIV__customsear__42')).isDisplayed(); | |
assert.assertTrue(hackathonReporter(1, "Search Icon is displayed", 'A__btnsearchm__59', isDisplayed)); | |
}); | |
}); | |
//Note if you are using "Expect" or "should" instead of Assert, | |
//create a couple of wrappers for "expect" or "should" methods | |
//Instead of should('be.visible'), | |
function shouldBeVisible(task, testName, domId) { | |
var displayed = true; | |
try { | |
cy.get(domId).should('be.visible'); | |
} catch(e) { | |
displayed = false; | |
} | |
return hackathonReporter(task, testName, domId, displayed); | |
} | |
//Call the "should" or "Expect" type assertions like below | |
describe('Task 1 - Header location', function() { | |
it('Search field should be displayed', async () => { | |
shouldBeVisible(1, "Search field is displayed", 'DIV__customsear__41'); | |
}); | |
it('Search Icon should be displayed', async () => { | |
shouldBeVisible(1, "Search Icon is displayed", 'A__btnsearchm__59'); | |
}); | |
}); |
@rajaraodv The code from lines 46-68 causes a CypressError if passing a domId
that is not visible. But even if the async
keyword is removed from that code, then an assertion error occurs (which is expected), but then that result can't be logged -- the catch
in the shouldBeVisible
function is not used.
According to the Cypress documentation, error handling cannot be added to Cypress commands. It appears a "Fail" result cannot be properly logged with Cypress in the way the instructions specify because of this -- or at least, I haven't found any way to do so (and believe me, I have been trying π©).
@rajaraodv I ended up figuring out something similar to this before you posted your reply -- just had to rubber duck virtually, I guess π Thank you for the response!
My guess is ...
ids
with numbers are generated dynamically you may not want to use them :)