I have some function that return windows.location.pathname, then I transform this function and based on result make some data transformation, but when I run cypress tests, instead of windows.location.pathname I get some cypress object, so If I want use window.location how I should handle it in cypress (e2e tests )
const getLocaleBeforeFlowsLoaded = (location = history.history.location) => {
const locale = location.pathname.split('/')[1]
// get error here since we have "__cypress" instead of locale above
const priceFormatter = new Intl.NumberFormat(locale, {
currencyDisplay: 'symbol',
style: 'currency',
currency: 'EUR',
minimumFractionDigits: 0,
})
return priceFormatter
}
I suppose it happens because cypress doen't ready by this moment, since in other places it works fine, it's just function that started before app started
CodePudding user response:
based on cypress documentation :
cy.location() // Get location object
cy.location('pathname') // Get the path name of the location object
cy.location('port') // Get the port of the location object
...
CodePudding user response:
It looks like you have the location from the wrong window.
The history
variable gives you the test runner location (hence __cypress
in URL), but the app under test is in an <iframe>
so it has a different set of global window
, location
, etc.
Try this
const getLocaleBeforeFlowsLoaded = (location) => {
const locale = location.pathname.split('/')[1]
const priceFormatter = new Intl.NumberFormat(locale, {
currencyDisplay: 'symbol',
style: 'currency',
currency: 'EUR',
minimumFractionDigits: 0,
})
return priceFormatter
}
cy.visit(...)
cy.location().then(location => {
const priceFormatter = getLocaleBeforeFlowsLoaded(location)
...
})