Last active
July 11, 2020 03:08
-
-
Save rajaraodv/152da1053ac101736ce22449aa5dee72 to your computer and use it in GitHub Desktop.
An example Test Reporter method in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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!