4

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.

Edward Mehr
  • 651
  • 9
  • 21

2 Answers2

0

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

bheussler
  • 1,406
  • 2
  • 15
  • 18
-1

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.

Cronax
  • 355
  • 1
  • 5
  • 17
  • I believe driver.set_window_size(x,y) in python is the same as driver.Manage().Window.Size = new System.Drawing.Size(x,y) in C#. I have already tried that. I want to set the device size as opposed to window size so directives like min-device-width would get picked up correctly. On headless driver device size seems to default to a hardcoded size which is not customizable. Or I haven't figured it out yet. – Edward Mehr Feb 13 '15 at 22:43