Closed olegKusov closed 3 months ago
If you want to create a command, then yes, browser command can only resolve and reject, and resolved command can't reject afterwards.
With webdriverio mock callbacks, i can offer you this approach:
beforeEach
to listen for network responses and write meta info to the browser if 404 appearedafterEach
and remove network spyIn code, it would look like this:
// .testplane.conf.ts
browser.mockRestoreAll()
module.exports = {
plugins: {
'@testplane/global-hook': {
beforeEach: async ({browser}) => {
const networkSpy = await browser.mock("**"); // create network spy
networkSpy.on('match', async ({statusCode, url}) => {
if (statusCode === 404) {
await browser.setMeta("notFoundUrl", url); // set meta property to check if in afterEach
// Note: we can't reject or throw errors inside "on" callback, because it would produce unhandled rejection
}
});
},
afterEach: async ({browser}) => {
await browser.mockRestoreAll(); // unsubscribe to browser mocks
const notFoundUrl = await browser.getMeta("notFoundUrl");
if (notFoundUrl) {
// but we can throw an error here
throw new Error("Not found at " + url);
}
}
},
// other testplane plugins
},
// other testplane settings...
};
As i mentioned, we can't throw errors inside of browserMock.on
callback, as well as we can't just interrupt test body execution from somewhere outside of it.
If you want to, you can overwrite "url"
command so it would check for browser.getMeta("notFoundUrl")
after "url"
resolve, but for "click"
part it is much harder. All we can see is:
browser.click
was calledbrowser.click
resolved (clicked)We can't tell if network request was called because of click. Click itself could be the reason and could not be the reason of that network request. Even wrapping "url"
will not 100% help you because it only waits for document.readyState
to be complete
, but you might have some lazy network requests.
With the url part, i can offer you openAndWait
command: https://github.com/gemini-testing/testplane/blob/master/docs/writing-tests.md#openandwait
It would wait for page to load and check for network errors. Notice how you still need to specify waitNetworkIdleTimeout
, because there could be a gap between one request resolved and the second one started (for example, when your react bundle just loaded, it needs to be processed, and only then your fetch
in useEffect
is called)
I am closing it due to inactivity.
Verify latest release
Hermione version
8.0.4
Last Hermione version that worked
No response
Which area(s) of Hermione are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue or a replay of the bug
No response
Reproduction steps
I want to create custom command for browser.mock
The problem is that its not clear how to make it work.
Actual Behavior
In webdriverio docs there is one example. And as far as I understand they waiting for all requests are done and only after that they can be sure that mock.on will be called. But its very strange logic. What if i click to button in my test after page load and request will be 404. then this command will no work (e.g. it will not fail test). And with this logic we need to create mock every time we making some actions and not at the top level of test how I understand it, e.g
One more thing that looks strange to me is that statusCode handled separately. So if we want to mock request with some data and change status Code then we need to duplicate logic
Expected Behavior
I just want to get how to fail test if mock.respond returns error.
Which Node.js version are you using?
18.12.1