What are valid deviceNames for Chrome emulation testing with selenium webdriver? - device-emulation

What are valid deviceNames for Chrome emulation testing with selenium webdriver?
And also where can i get the device width,height and user agent for the device?

This is the device list:
https://cs.chromium.org/chromium/src/chrome/test/chromedriver/chrome/mobile_device_list.cc
In Java you can get height and width values by this way:
initial_size = driver.manage().window().getSize();
height = initial_size.getHeight();
width = initial_size.getWidth();

Related

Chrome Driver default resolution

My front-end developers, changed the layout of the application which I'm testing, therefore some buttons are in slightly different places. I have about 50 automated tests that I will have to improve if the idea I came up with proves to be ineffective. Namely, is it possible to change the size of the pages opened by default via Headless Chrome Driver? Most of my tests are as follows: open the website > log in > click the button on the top bar of the application. The problem is that the buttons have been hidden under the hamburger button, and one of the ways to "see" them is to reduce the resolution of Google Chrome to 80%, so is it possible to program Chrome Driver and/or Geckodriver to open websites and web apps in 80% resolution by default?
There is a way to achieve the browser resolution changes before tests are actually started. Below codes are for java and you can add them to your driver options. You can find your desired resolutions and then you can change the values.
ChromeOption for 1920 x 1080 :
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1080");
Firefox options for 1920 x 1080 :
FirefoxDriver firefoxOptions = new FirefoxDriver();
firefoxOptions.addArguments("-width=1920");
firefoxOptions.addArguments("-height=1080");
You can also change resolution after driver initialization with dimension, here is an example for 1920 x 1080 :
Dimension dimensions = new Dimension(1920,1080);
driver.manage().window().setSize(dimensions);
For Magnification feature of browser you can use below codes :
Magnification + :
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL,Keys.ADD));
Magnification - :
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL,Keys.SUBTRACT));

Can a website detect when you are using selenium with geckodriver?

Is it possible to detect instances of firefox browsers that are being controlled by Selenium and geckodriver?
Note there is a corresponding answer for chromedriver, but I'd like to know whether this is possible for firefox/geckodriver.
Yes you can detect geckodriver controlled selenium with a simple check in JavaScript
var runningSelenium = !("showModalDialog" in window);
As others have pointed out, there are a variety of different ways that a site can fingerprint and detect that you are running a browser that has been automated by selenium. Luckily though, some of the detection mechanisms are remarkably simple and just look for a bunch of environmental defaults, such as the screen size etc.
For example, when using Selenium for testing the app OAUTH sign-up sequences for Dropbox etc, the CAPTCHA stage can be avoided by just setting the screen to a non-default value (and offsetting the browser window to simulate a taskbar):
##headless = Headless.new( dimensions: '1600x1200x24' )
##headless.start
browser = Watir::Browser.new :firefox
width = browser.execute_script( 'return screen.width;' )
height = browser.execute_script( 'return screen.height;' ) - 95
browser.driver.manage.window.resize_to( width, height )
browser.driver.manage.window.move_to( 0,0 )

Selenium Java - anyway of know whether the browser is minimized?

I am using Selenium with Java. Is there a way to know whether the browser of the webdriver is minimized?
you can use getSize() to get the window size, like this
Dimension d= driver.manage().window().getSize();//returns of the browser window dimension
System.out.println(d);

Setting Device Width for Remote Webdriver in Selenium

I am using Selenium grid 40 with Firefox remote driver that runs in windows 7. I also use C# API. I was wondering how to Set Device width on headless Firefox browser. The device width is less than ipads max width and this causes it to pick up ipad specific css that is defined like below:
#media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
/* For portrait layouts only */
}
#media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
/* For landscape layouts only */
}
I have already changed window size using:
driver.Manage().Window.Size = new System.Drawing.Size(1290,900)
But it still picks up those css directives.
Other information: My grid node is virtual machine that nobody actually logs into. I remotely run the selenium grid, and that might be the reason why device width is small. it might default to smallest resolution for windows. If there is way to change that it might help me, but I am not aware of it.
Update: I tried to set all instances of DefaultSettings.XResolution, DefaultSettings.YResolution and DefaultSettings.BitsPerPel in registry to 1290, 900 and 16 through a powershell script and restart the computer but it didn't work.
I don't know how to set the device width using selenium, but I figured out how to set it using the remote frame buffer. For me this was Xvbf. I pass in the screen resolution when I start the service.
Below is an example of an Xvfb service with a resolution of 1024x768 with a depth of 24.
https://gist.github.com/dloman/931d65cbb79b00593ac3dd5d0cdf37d9
My experience is limited to using the Python implementation of Selenium so this may not work for you, but there you can use driver.set_window_size(width, height) before performing the driver.get() action that will load the desired page.
The key is the order in which you perform these actions: you want to tell selenium to change the dimensions of the browser before you load the page, because otherwise you are simulating the same behaviour as resizing a browser window after the initial page load. As you may know, when resizing a browser window, a page reload is needed for some 'responsive' features to be activated.

Getting different height and width in different browser using selenium webdriver

This is the code for getting the Dimension.
Dimension countrypickerDim = countryelement.getSize();
Results
IE
Height =24
Width =94
Firefox and Chrome
Height=24
Width =96
i talked with developers and they are saying that they are using the Google Fonts so results will be deffer on different browser.
Please let me know how to deal with that.
Update your test accordingly, it's the "desired" behavior, IE is hell, nothing you can do with that.
The simplest method: switch-case for different browsers that assert the dimension according to the browser you working on.