Skip to content

Instantly share code, notes, and snippets.

@rajaraodv
Last active July 11, 2020 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajaraodv/152da1053ac101736ce22449aa5dee72 to your computer and use it in GitHub Desktop.
Save rajaraodv/152da1053ac101736ce22449aa5dee72 to your computer and use it in GitHub Desktop.
An example Test Reporter method in JavaScript
//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');
});
});
@asaf-shochet
Copy link

hi,
you have a type on line 56.
it should be 'displayed', and not 'isDisplayed' 👍

@rajaraodv
Copy link
Author

hi,
you have a type on line 56.
it should be 'displayed', and not 'isDisplayed' 👍

fixed. Thanks

@amarnathk1547
Copy link

DIV__customsear__42 doesn't seem to be the correct one to identify the search icon, just before someone spends some time on this. Probably #A__btnsearchm__59 or #LI____58 would be a better pick for search icon

@rajaraodv
Copy link
Author

DIV__customsear__42 doesn't seem to be the correct one to identify the search icon, just before someone spends some time on this. Probably #A__btnsearchm__59 or #LI____58 would be a better pick for search icon

Thanks, updated it

@vasanthk91
Copy link

@rajaraodv Comparing the version V1 & V2, the id's of many elements are not matching. The same element having different id. Ex: The list & grid icons in the list header.

image

both elements are available in version V1 & V2. but since the id mismatches, the test fails which is not expected. So instead id, can we use the CSS class to select the element?

@RajaBellebon
Copy link

My guess is ... ids with numbers are generated dynamically you may not want to use them :)

@mdoig
Copy link

mdoig commented Jul 4, 2020

@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
Copy link
Author

rajaraodv commented Jul 6, 2020 via email

@mdoig
Copy link

mdoig commented Jul 11, 2020

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment