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'); | |
}); | |
}); |
Author
rajaraodv
commented
Jul 6, 2020
via email
There are ways of doing this. As a workaround, print the result (except the
status) before the assertion error and then print "fai" as part of the
assertion.
…On Fri, Jul 3, 2020 at 9:41 PM Marshall Doig ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@rajaraodv <https://github.com/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 😩).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/152da1053ac101736ce22449aa5dee72#gistcomment-3363679>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEY5IMX6YGSEQRDUAPHUC3RZ2XG3ANCNFSM4NOU5FEA>
.
@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