Nightwatch waitForElementVisible - abortOnFailure parameter set to false - test exit status non-zero - nightwatch.js

I am using the waitForElementVisible(<selector>, <timeout>, false) command from the Nightwatch API docs and it is not behaving quite like I expected. How can I tweak this code in order to get the intended behavior?
Intended behavior:
call .waitForElementVisible('foobar', 10, false)
see command fail and continue execution with the next command
all other commands pass
see exit status of 0 from script
Actual behavior:
call .waitForElementVisible('foobar', 10, false)
see command fail and continue execution with the next command
all other commands pass
see exit status of 1 from script
Here is example code to reproduce
module.exports = {
tags: ['smoke'],
before: browser =>
browser
.maximizeWindow('current').url('https://google.com'),
after: browser => browser.end(),
'smoke test': browser =>
browser
.waitForElementVisible('foobar', 10, false)
.waitForElementVisible('img')
.assert.visible('img'),
};
And here is the console output from running that command:
Starting selenium server in parallel mode... started - PID: 75459
Started child process for: 01_smoke
01_smoke \n
01_smoke [01 Smoke] Test Suite
=========================
01_smoke
01_smoke Results for: smoke test
01_smoke ✖ Timed out while waiting for element <foobar> to be present for 10 milliseconds. - expected "visible" but got: "not found"
01_smoke at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
01_smoke ✔ Element <img> was visible after 33 milliseconds.
01_smoke ✔ Testing if element <img> is visible.
01_smoke
01_smoke Retrying (1/3): smoke test
01_smoke ✖ Timed out while waiting for element <foobar> to be present for 10 milliseconds. - expected "visible" but got: "not found"
01_smoke at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
01_smoke ✔ Element <img> was visible after 21 milliseconds.
01_smoke ✔ Testing if element <img> is visible.
01_smoke
01_smoke Retrying (2/3): smoke test
01_smoke ✖ Timed out while waiting for element <foobar> to be present for 10 milliseconds. - expected "visible" but got: "not found"
01_smoke at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
01_smoke ✔ Element <img> was visible after 20 milliseconds.
01_smoke ✔ Testing if element <img> is visible.
01_smoke Retrying (3/3): smoke test
01_smoke ✖ Timed out while waiting for element <foobar> to be present for 10 milliseconds. - expected "visible" but got: "not found"
01_smoke at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
01_smoke ✔ Element <img> was visible after 20 milliseconds.
01_smoke ✔ Testing if element <img> is visible.
01_smoke FAILED: 1 assertions failed and 2 passed (53ms)
>> 01_smoke finished.
_________________________________________________
TEST FAILURE: 1 assertions failed, 2 passed. (6.259s)
✖ 01_smoke
- smoke test (53ms)
Timed out while waiting for element <foobar> to be present for 10 milliseconds. - expected "visible" but got: "not found"
at Object.smokeTest [as smoke test] (/path/to/tests/01_smoke.js:12:8)
at _combinedTickCallback (internal/process/next_tick.js:131:7)

Yes, and that's the way it should be! I think you misinterpreted the way the abortOnFailure flag works for the waitForVisible command. The false flag only gives the method the character to be evaluated by the test-runner as a non-breaking
step. But that doesn't mean it doesn't keep count of that step as being a failed step.
Note: A similar thing happens in the assert/verify case (where verify is a non-breaking assertion, similar to the abortOnFailure: false parameter for waitForElementVisible).
I can see where one would get that impression though. If you read the API call's description it said:
If the element fails to be present and visible in the specified amount
of time, the test fails. You can change this by setting abortOnFailure
to false.
Which leaves you thinking that perhaps the test will end up passing, even though the waitForVisible command failed. But... the Parameters section of the API call comes to our aid, removing that false assumption:
By the default if the element is not found the test will fail. Set
this to false if you wish for the test to continue even if the
assertion fails. To set this globally you can define a property
abortOnAssertionFailure in your globals.
Lastly... where the DOCs might fail you, the code will never lie:
process.on('exit', function (code) {
var exitCode = code;
if (exitCode === 0 && globalResults && (globalResults.errors > 0 || globalResults.failed > 0)) {
exitCode = 1;
}
process.exit(exitCode);
});
That above is the is a snippet Nightwatch's test-runner (nightwatch/lib/runner/run.js). See for yourself what it considers as valid exit code 1 conditions.
Cheers!

Related

Mocha - retry after timeout

I am using mocha with following flags: --timeout 450000 --retries 3.
Mocha retries whenever something goes wrong during test execution or if the assertion fails i.e. it respects the --retries 3 flag and retries for three (3) more times to see if the test pass before failing it.
But when the tests timeout, it doesn't respect the --retries 3 flag. Instead it simply fails the test with the following message:
Error: Timeout of 450000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
How can I make mocha retry if the test has timed out? This is important because in my setup the tests can do timeout because they depend on network conditions.

Test run in cypress headless gives error cy.click()

I've a test cases written in cucumber and cypress. The test case run successfully through cypress Test runner, but fails while running through headless mode using the command.
node_modules\.bin\cypress run --spec **/*.features
CypressError: Timed out retrying: `cy.click()` failed because this element is not visible:
Questions:
what is the possible reason to have this element not visible error?
How can i handle the wait in headless mode?
The fix is :
Fix all your issues displayed on the console w.r.t xpaths, sync or
any such.

Cypress test ran with browser vs head-lessly

How can I debug a test that is failing when ran headlessly but passing
when ran with a opened browser ?
below is the error I'm getting when running the test without opened browser:
AssertionError: Timed out retrying: Expected to find content: 'Logout' within the element: [ <a.navbar-item>, 2 more... ] but never did.
Meaning ... the test passes locally but fails within CI tool.
Please ask if you need to know any details.
I would suggest recording a video and using console.log statements in your test. https://docs.cypress.io/guides/guides/screenshots-and-videos.html#Screenshots

Does --num-flaky-test-attempts rerun the whole suite or just the failed test?

The documentation for --num-flaky-test-attempts parameter of gcloud firebase test android run says the following:
Specifies the number of times a test execution should be reattempted if one or more of its test cases fail for any reason.
This means it reruns only the failed tests but not the whole suite, right? In other words, as soon as a test passes it won't be retried, right?
The command line parameter --num-flaky-test-attempts of gcloud firebase test android run appears to rerun all of the tests instead of just the failed tests.
I ran a suite of tests using --num-flaky-test-attempts 10 and here timestamps from the logs for one test in the suite:
04-27 03:41:51.225 passed
04-27 03:41:50.519 passed
04-27 03:41:43.533 failed
04-27 03:41:48.625 failed
04-27 03:42:13.886 failed
04-27 03:41:33.749 failed
04-27 03:41:43.694 failed
04-27 03:41:42.101 failed
04-27 03:41:20.310 passed
04-27 03:40:17.819 passed
04-27 03:33:14.154 failed
It appears to have executed the entire test suite each time. In some cases the test mentioned above passed and in some cases it failed. It passed and failed multiple times, so clearly it's rerunning the test no matter whether it passed or failed.
I believe there were 11 total tests because I specified --num-flaky-test-attempts 10 which means it attempted to run the suite once and since that failed it ran 10 more times for a total of 11.
Here is the full command in case that's helpful to anyone:
gcloud firebase test android run \
--project locuslabs-android-sdk \
--app app/build/outputs/apk/debug/app-debug.apk \
--test app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk \
--device model=walleye,version=28,locale=en_US,orientation=portrait \
--test-targets "class com.locuslabs.android.sdk.TestUITest" \
--use-orchestrator \
--num-flaky-test-attempts 10 \
--timeout 30m \
--environment-variables numShards=10,shardIndex=2 \
--verbosity debug
The documentation states the following for --num-flaky-test-attempts:
Specifies the number of times a test execution should be reattempted if one or more of its test cases fail for any reason. An execution that initially fails but succeeds on any reattempt is reported as FLAKY.
I.e. if one test case in a test execution fails, Test Lab will re-run the whole test execution again. A test execution is comprised of running the whole test suite on one device.
Example: You execute your test suite on two devices, lets call them A and B. The whole test suite succeeds on A, but one test case fails on B. In this case only the test suite on device B will be re-attempted.

Failed test in protractor stops the execution for rest test cases

I am running the protractor Suite (spec file having multiple test cases), If any test case fails, protractor does not continue with the next test case execution and all the rest of test cases also fail.
EXPECTED BEHAVIOR:
Upon failure on any test case, protractor should continue with next test case execution.
I used "Protractor-Fail-Fast" Npm package to stop the rest test case execution if any test case fail. But ideally I am not looking for the same.
But this will not help me!
Just for reference: In Visual Studio MS test, If I created ordered test (same as Spec file in protractor having multiple test cases) and then set test setting like "continue on failure", ordered test execution will continue even if some test case failed.
I am looking for a similar test setting or any solution for protractor.
If you dont't want to stop all tests run just stop using Protractor-Fail-Fast library? Protractor tests run till the end by default even if some of the tests are failed.
set ignoreUncaughtExceptions: true in config file as following:
/**
* If set, Protractor will ignore uncaught exceptions instead of exiting
* without an error code. The exceptions will still be logged as warnings.
*/
ignoreUncaughtExceptions?: boolean;
you can get above description from here
export.config = {
...
ignoreUncaughtExceptions: true
}

Resources