Skip to content
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

added devicePixelRatio support #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

XerxesNoble
Copy link

Support for screens that have a greater devicePixelRatio than 1.

There may be another way to do this, because baselines created on a retina display will fail when run on a regular display and vice versa.

I'm not very familiar with this module, but I'd imagine exposing the ability to set this implicitly to a developer, rather than calculating it automatically might be a better option.

Currently in my project I am doing this:

def get_dimensions(cls):
    # Support for retina displays
    dpr = cls._parent.execute_script('return window.devicePixelRatio')
    location = cls.location
    size = cls.size
    return {
        "top": location['y'] * dpr,
        "left": location['x'] * dpr,
        "width": size['width'] * dpr,
        "height": size['height'] * dpr
    }
    
NeedleWebElement.get_dimensions = get_dimensions

Browser support for devicePixelRatio: http://caniuse.com/#search=devicePixelRatio

@jphalip
Copy link
Collaborator

jphalip commented Mar 4, 2017

Thanks for the suggestion. Could you explain exactly what problem you're facing right now? Is it that baselines were created on a non-retina screen and that tests then fail when run on a retina screen?

I think ideally it should be the developer's responsibility to normalize the environment in which tests are run. Perhaps this could be done by forcing the pixel ratio for all test runs.

Would your tests work if you somehow forced the device ratio? Maybe something like this: execute_script('window.devicePixelRatio = 2') ?

If that works then we could add an API endpoint in Needle to make this easier.

@XerxesNoble
Copy link
Author

Sure, when creating a baseline set on my retina screen, the position & size of the screenshots are in the wrong place.

For example, lets say I have a browser open at 600x600. If I want to screenshots an element that is directly in the center (top: 200, left: 200, width: 100, height: 100). The resulting baseline will appear as if the snap shot was at half the size and half the position (top:100, left: 100, width: 50, height: 50)

I don't think forcing execute_script('window.devicePixelRatio = 2') will work as it is a read only property.

As it is right now, any baselines that are created on a retina screen will fail when executed on a regular screen and vice versa.. My solution does not solve that, it only makes this software usable when working with a retina screen.

@jphalip
Copy link
Collaborator

jphalip commented Mar 15, 2017

So I've given this some thought and I think it's generally best practice to ensure that the environment in which tests are run always stays consistent.

For this one could perhaps use the webdriver's options to force the pixel ratio to a certain value. As a reference, see this code snippet taken from the Chrome Webdriver's site:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options = chrome_options)

Would you like to give this a try? If that works then perhaps we could add some documentation to Needle.

@XerxesNoble
Copy link
Author

This unfortunately doesn't suit my use case since I don't want mobile emulation. I agree with your consistent environment point. However, in the case where a baseline is saved on a local machine to be tested on a CI tool like travis, it is not always the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants