'Is there a way to "force" a visit?
I'm using the cy.visit()
command but the website i'm visiting (which i don't own) doesn't always fire the load
event, although the content itself that i need for testing does appear on the website.
Despite the content appearing, since the load
event is not fired sometimes (for some reason which i can't fix since i don't have ownership over this website), the cy.visit()
command fails.
Is there a way to "force" it somehow, similar to how we can pass { force: true}
for the cy.click()
command?
Solution 1:[1]
It's hard to simulate the problem, but I think I managed by setting pageLoadTimeout
really low (30ms).
You can catch the onLoad fail in an event handler and checking for the page load error message.
I recommend doing it in a beforeEach().
beforeEach(() => {
Cypress.config("pageLoadTimeout", 30) // set this to whatever time length
// you feel is appropriate to start testing
// You'll need to experiment to get this right
// and in CI it will be a lot longer
cy.once('fail', (err) => { // "once" to just catch a single error
const message = err.parsedStack[0].message
if (message.match(/Timed out after waiting `\d+ms` for your remote page to load/)) {
return false
}
throw err // any other error, fail it
})
cy.visit('www.example.com');
})
it('checks the heading of the page', () => {
cy.get('h1').should('have.text', 'Example Domain') // ?
})
Solution 2:[2]
As you can already assume, that is highly discouraged. It also really depends on how it fails and with which errors, but, without any code to reproduce, you may want to try this if you haven't already:
cy.visit('/', {failOnStatusCode: false});
Reference: https://docs.cypress.io/api/commands/visit#Arguments
Solution 3:[3]
Add the below to your cypress commands file
Cypress.Commands.add('forceVisit', url => {
cy.window().then(win => {
return win.open(url, '_self');
});
});
And in your tests, you can call
cy.forceVisit("www.google.com")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Zaista |
Solution 3 | Krupal Vaghasiya |