everybody. I've been having a problem with my code. I'm using webdriverio, appium and mocha to automate some test cases for an app. I have to take a screenshot on every test fail so i can troubleshoot the problem later. Using hooks, i've defined the afterTest hook like this
/**
* Function to be executed after a test (in Mocha/Jasmine only)
* @param {Object} test test object
* @param {Object} context scope object the test was executed with
* @param {Error} result.error error object in case the test fails, otherwise `undefined`
* @param {any} result.result return object of test function
* @param {Number} result.duration duration of test
* @param {Boolean} result.passed true if test has passed, otherwise false
* @param {Object} result.retries informations to spec related retries, e.g. `{ attempts: 0, limit: 0 }`
*/
afterTest: function(test, context, { error, result, duration, passed, retries }) {
if(!passed){
// @ts-ignore
browser.takeScreenshot();
}
}
The problem is that it doesn't work. Looking at the logs i see that the sessions is terminated before the screenshot call is made.
[0-0] 2022-05-30T17:12:05.972Z INFO webdriver: [POST] http://localhost:4723/session/50c82187-4939-478d-8b45-75c83ac88cdb/elements
[0-0] 2022-05-30T17:12:05.972Z INFO webdriver: DATA { using: 'xpath', value: '//*[@text="Lista de Granjas"]' }
[0-0] 2022-05-30T17:12:31.797Z INFO webdriver: COMMAND takeScreenshot()
[0-0] 2022-05-30T17:12:31.798Z INFO webdriver: [GET] http://localhost:4723/session/50c82187-4939-478d-8b45-75c83ac88cdb/screenshot
[0-0] Error in "Login to app.shouldnt login with valid credentials"
Error: Expect $(`//*[@text="Lista de Granjas"]`) to have text containing
Expected: "FarmListPage.title"
Received: "Lista de Granjas"
at Context.<anonymous> (/home/kalves/Documents/poc-aut/config/test/specs/login.tests.ts:17:47)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
[0-0] 2022-05-30T17:12:36.001Z INFO webdriver: RESULT []
[0-0] 2022-05-30T17:12:36.038Z INFO webdriver: COMMAND deleteSession()
[0-0] 2022-05-30T17:12:36.038Z INFO webdriver: [DELETE] http://localhost:4723/session/50c82187-4939-478d-8b45-75c83ac88cdb
[0-0] 2022-05-30T17:12:36.440Z WARN webdriver: Request failed with status 500 due to An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: socket hang up
[0-0] 2022-05-30T17:12:36.441Z INFO webdriver: Retrying 1/3
[0-0] 2022-05-30T17:12:36.441Z INFO webdriver: [GET] http://localhost:4723/session/50c82187-4939-478d-8b45-75c83ac88cdb/screenshot
[0-0] 2022-05-30T17:12:36.471Z WARN webdriver: Request failed with status 404 due to A session is either terminated or not started
[0-0] 2022-05-30T17:12:36.472Z INFO webdriver: Retrying 2/3
[0-0] 2022-05-30T17:12:36.472Z INFO webdriver: [GET] http://localhost:4723/session/50c82187-4939-478d-8b45-75c83ac88cdb/screenshot
[0-0] FAILED in ./app/agriness-mobile-farm-swine-250f2675c1c546b480b66a11c4a022d1-signed.apk - /test/specs/login.tests.ts
2022-05-30T17:12:36.510Z INFO @wdio/cli:launcher: Run onWorkerEnd hook
2022-05-30T17:12:36.512Z INFO @wdio/cli:launcher: Run onComplete hook
How do i stop this from happening?
Apparently, the issue was that every call webdriverIO makes is asynchronous, therefore the answer was to modify the afterTest to include the async keyword and put a await before calling browser.takeScreenshot().
afterTest: async function(test, context, { error, result, duration, passed, retries }) {
if (passed) {
return;
}
await browser.takeScreenshot();
console.log('\n\tScreenshot saved', '\n');
},