phantomjs icon indicating copy to clipboard operation
phantomjs copied to clipboard

[PhantomJS 2.1.1] matchMedia queries not honoring viewPort width and height

Open jose-brandao opened this issue 9 years ago • 1 comments

  1. Which version of PhantomJS are you using? Tip: run phantomjs --version. 2.1.1

  2. What steps will reproduce the problem? 2.1. Set a mobile view port and user agent. 2.2. Open a mobile user agent. 2.3. phantomJS MatchMedia says that he is a tablet instead of a phone.

  3. Which operating system are you using? macOS Sierra

  4. Did you use binary PhantomJS or did you compile it from source? Installed with npm install -g phantomjs

  5. Please provide any additional information below.

I'm setting a mobile viewPort and userAgent:

var casper= require('casper').create({
    viewportSize: { 
            width: 414, 
            height: 736
    }
});

casper.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B137 Safari/601.1';

After opening a given web page, always opens in tablet version instead of phone. The reason was this:

matchMedia('only screen and (max-device-width: 767px), only screen and (max-device-height: 767px)').matches;

This query should return true and returns false, since my view port is 414x736. Other browsers like chrome and firefox return true on this.

An additional information is that, the 'tablet' query returns true on phantom: matchMedia('only screen and (min-device-width: 768px) and (min-device-height: 768px)').matches;

I have watched the web page dimensions in run time and they match what I have set on the viewPort but for some reason matchMedia doesn't agree.

Is there any known problem with PhanomJS matchMedia ? I have been searching for a while and didn't find any precise information about this, only a few posts refering match media problems.

Thanks in advance.

jose-brandao avatar Jul 31 '17 10:07 jose-brandao

Since PhantomJS is abandoned I let here an example on how to completely emulate a mobile device with Headless Chrome.

https://github.com/josebrandao13/headlessChromeMatchMedia

jose-brandao avatar Aug 14 '17 15:08 jose-brandao