No, I tried this also, you can't do it.
The docs say
Values passed into args must be serializable
Here is my proof of concept
class MyPageObject {
add() {
return 1+2
}
}
it('function serialization with cy.origin', () => {
const testObject = new MyPageObject()
console.log(typeof testObject.add) // yields "function"
console.log(JSON.stringify(testObject)) // yields "{}" - add() has been removed
cy.origin('https://example.com', { args: { testObject } }, ({testObject}) => {
console.log(testObject, testObject.add) // yields "{}" and "undefined"
})
})
Technically, you can pre-serialize the object and override the default serialization behavior for function properties (turn them into strings as you see in devtools), then use the dreaded "eval" within the cy.origin()
to re-instantiate all the methods.
But no, basically page objects are not a good fit with the Cypress paradigm.
Using JSON or object instead
As suggested in the notes, a simple object can be used to pass selectors in.
const selectors = requires('selectors.json')
/*
Selectors is an object with string properties, it is serializable
{
login: 'input#user-name',
...
*/
it('object map of selectors with cy.origin', () => {
console.log(typeof selectors.login) // yields "string"
console.log(JSON.stringify(selectors)) // yields "{login: ...}"
cy.origin('https://example.com', { args: {selectors} }, ({selectors}) => {
console.log(JSON.stringify(selectors)) // yields "{login: ...}"
})
})