New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When including zepto library, zepto object is passed as remote jQuery instead of expected Cypress jQuery to use for chaining. #6529
Comments
I can't recreate this. Please provide a full example of the HTML you are testing against. My test code below: <!DOCTYPE html>
<html>
<body>
<h2>dress</h2>
</body>
</html> it('have text not exist', () => {
// h2 doesn't even exist
cy.get('h2')
.should(($el) => {
expect($el).to.have.text('dress')
})
})
it('have text', () => {
cy.visit('index.html')
cy.get('h2')
.should(($el) => {
// assertion passes
expect($el).to.have.text('dress')
})
cy.get('h2')
.should(($el) => {
// assertion fails
expect($el).to.have.text('foo')
})
}) Unfortunately we have to close this issue as there is not enough information to reproduce the problem. This does not mean that your issue is not happening - it just means that we do not have a path to move forward. Please comment in this issue with a reproducible example and we will reopen the issue. 🙏 |
Thanks for the reply. Here is my complete test code in describe('image component', () => {
beforeEach(() => {
cy.viewport('iphone-x')
cy.visit('http://159.65.226.122', {
onBeforeLoad(win) {
Object.defineProperty(win.navigator, 'userAgent', {
value:
'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
})
},
})
})
it.only('title display', () => {
cy.get('div[data-component-data-id=302749] h2:first')
.should($el => {
expect($el).to.have.text('dress')
})
})
}) And some settings in Cypress.on('uncaught:exception', () => {
// returning false here prevents Cypress from
// failing the test
return false
}) I can confirm that the html in |
@luokuning Please provide the fully HTML/CSS/JavaScript or a hosted application since we cannot visit |
@jennifer-shehane Sorry for the inconvenient. Here is the minimize HTML: <!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<div data-component-data-id="302749">
<h2 class="mshe-text-left">dress</h2>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
</body>
</html> And it turns out that when I comment the script tag which load |
I am able to recreate this with the following code. I don't believe this is a regression (tested back to 3.4.1).
<!DOCTYPE html>
<html lang="en">
<body>
<h2>dress</h2>
// commenting out zepto script causes this to pass
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
</body>
</html> it('title display', () => {
cy.visit('index.html')
cy.get('h2')
.should($el => {
expect($el).to.have.text('dress')
})
})
I have no idea. Application scripts can kind of do anything in JavaScript and they are obviously doing something with the elements on the page that Cypress is not expecting. |
Another failing use case brought up in #7917 @Ayase-252 mentioned:
Yes, this is how Cypress currently works. If cypress finds that the application under test (AUT) has a global jQuery instance, rather than wrap elements in cypress’s own jQuery, it will use the jQuery instance in the AUT which can have some surprising implications such as when the remote jQuery has been overwritten. The reason this was originally designed this way was so that 3rd party jQuery plugins you’ve installed in your AUT would automatically be “accessible” and queryable from cypress’s API. So taking this behavior away would negate your ability to do this: But this is likely not a strong argument for keeping this behavior. Whenever Cypress is using “global” functions on AUT instances/objects, the “global” functions cannot be trusted because they come from the AUT, which means they could have been replaced. A proposed solution is to always use
it('test', () => {
cy.visit('index.html')
cy.get('body').then(($el) => {
// `$el` is a zepto object now, the following will
// fail because $el is not a valid jQuery object
cy.wrap($el).screenshot();
});
})
<body>
hello world
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
</body> |
This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided. |
This issue has been closed due to inactivity. |
I'm using cypress v4.0.2, and here is a snip of my test code:
and cypress complains about the assertion:
So I have to change
expect($el).to.have.text('dress')
toexpect($el[0]).to.have.text('dress')
, then the complain dismissed and test passed.I debug the assertion a bit, turns out
$el[0]
is also a jquery element.I also print
$el
and$el[0]
to the console:Isn't
cy.get
equivalent to jquery $? Why should I have to get the first element from $el?The text was updated successfully, but these errors were encountered: