-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Any way to force CSS media queries to detect mobile #970
Comments
Related comment on devtools protocol: #311 (comment) |
That part of the debugger protocol would only work for Chrome and is not cross browser compatible. If we exposed that, it would either have to be restricted to Chrome only. Can you provide me an example of CSS media queries not working? Those should be bound to the window, not the screen. I understand that the screen API's don't account for the iframe - but to work around this is fairly trivial, you create wrapper functions around those values and use Having some code in regards to what you're doing with |
Media query is
and it is recommended detection of mobile phone. I think cypress works as expected, since media queries are bound to device, not screen. |
Yeah I can't think of a way around this other than to hide the Cypress UI and then change the width and height of the actual browser window. It's actually possible to do - those API's are exposed to us - but probably not worth the effort. |
No problem will find a way |
Just now I have tested Facebook, Gmail and Twitter and all of them use media queries based on |
Agreed. This is possible to do and we will do this - even if its just for Chrome. This belongs in the same category as "native events", which is something we can implement. @chrisbreiding is scheduled to start working on cross browser support in December and that's when we'll switch everything over. We can do this by using the underlying debugger protocol as opposed to the extensions which is how we currently automate chrome. Once we do that, we'll be able to fire any kind of native event, but also expose and even lower level API that would enable you to take advantage of anything that the debugger protocol offers. If you're curious these other issues talk more detail about the switchover: |
Thanx a lot |
Any updates on this? I'm currently running into issues trying to properly spoof mobile devices and its breaking my tests. |
Hi @thebluick, we still have this on our roadmap, but no defined deadline or work done for this issue at the moment. |
With the default 1000px viewport,
|
The larger issue I am having dealing with this is getting some sort of weird hybrid where the window size is 1280x720 but the rendering is showing the mobile/responsive view. This is breaking tests since we have a different layout in mobile. Is there no way to preserve viewport size and/or reset it manually? |
Broken since: b62dcc0 The issue here cypress sets the window size and not the device size when tries to emulate mobile view. cypress-io/cypress#970 For now, let's keep window size media rules too. [tml: Window size changes depending on orientation. Device size does not. We need to change each rule that used only max-device-width into one using two subrules using max-width combined with comma (i.e. the OR operator), one for portrait using 767px as the limit, one for landscape using 1023px as the limit.] We do this in other places of the css code anyway. In a long term, it would be good to find a better way to emulate mobile view by cypress or to detect mobile from JS code, so we can enable mobile mode easily for cypress tests. Change-Id: Ic7974f44fcbf6ed2883e93acd28153709514c216
Broken since: b62dcc0 The issue here cypress sets the window size and not the device size when tries to emulate mobile view. cypress-io/cypress#970 For now, let's keep window size media rules too. [tml: Window size changes depending on orientation. Device size does not. We need to change each rule that used only max-device-width into one using two subrules using max-width combined with comma (i.e. the OR operator), one for portrait using 767px as the limit, one for landscape using 1023px as the limit.] We do this in other places of the css code anyway. In a long term, it would be good to find a better way to emulate mobile view by cypress or to detect mobile from JS code, so we can enable mobile mode easily for cypress tests. Change-Id: Ic7974f44fcbf6ed2883e93acd28153709514c216
A bit of a crude solution that worked for me was swapping Cypress.Commands.add('swapDeviceWidth', () => {
cy.document().then(doc=>{
let replaced = 0
Array.from(doc.styleSheets).forEach(sheet => {
try {
Array.from(sheet.cssRules).forEach((rule, index) => {
const {cssText} = rule
const regexQuery = /(\(\s*)(min|max)-device-(width|height)(\s*:)/
const hasDeviceWidth = cssText.match(regexQuery)
if (hasDeviceWidth) {
const newRule = cssText.replace(regexQuery, '$1$2-$3$4')
sheet.removeRule(index)
sheet.insertRule(newRule, index)
replaced++
}
})
}catch(err){/* prevent InvalidAccessError on external sources*/}
})
cy.log(`Replaced ${replaced} -device- rules`)
})
}) |
This is not just about resolution, there's also these things to detect touch devices: |
In this issue on the Material-UI library and this Stackoverflow question it is discovered that Cypress tests on headless chrome with Material UI's The reason behind this is that Material UI renders the As far as I know, there is no way for Cypress to force such a media query to match, so this is a feature that would be nice. |
Any updates on this? Would love to be able to set pointer media query in addition to viewport size in the cypress config. Issue is also the Material Date picker as mentioned by @pganster |
+1 having the ability to set pointer/ other MediaQuery params is much needed to automate mobile use cases. Please share if there are any workarounds. Thanks! |
MUI X pickers detects mobile based on touch, and the lack of feature for this prevents us from visual testing these components. |
Running into the same with the latest MUI X pickers too. If I find a solution will update here accordingly. |
For those running into this issue, I found a "fix"/work-around. MUI X date pickers have a prop It has a default value of My specific solution was to provide a default prop to my MUI theme with the given media query:
|
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. |
This issue shouldn't be closed, because it's still actual. |
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. |
First of all THANK YOU for excellent tool. We (front end developers) have been waiting years for this tool.
There is just one small issue. Using
cy.viewport('type-of-mobile-phone')
makes testingiframe
smaller, but CSS media queries andwindow.screen.width, height
remains the same (since it resolves actual size of screen.Is there any possible way how to fix this? I was doing some research, and only idea I have is to implement somehow Google Dev Tools protocol, that is able to force Chrome to fake anything.
https://chromedevtools.github.io/devtools-protocol/
The text was updated successfully, but these errors were encountered: