1

I recently started running Selenium WebDriver on a new 24 inch monitor. Previously, I ran it from a 15 inch screen. I need the browser dimensions to be the same size as I ran it before (I've always ran it in maximized mode). Since I currently have a 24 inch screen maximized mode means browser would much larger than it was previously which is not desired since it needs to be the same size as browser when run on 15 inch screen.

I'm currently using:

driver.manage().window().setSize(dimensions)

to set the size of Selenium browser to the same size it was when running in maximized mode on my 15 inch screen. The dimensions shown on my 15 inch screen is: Height: 837 Width: 1549. I've set the same dimensions on 24 inch screen but even though Selenium prints the said dimensions when calling getHeight + getWidth in reality the browser looks much bigger (I compared it side by side with my 15 inch screen).

Why is this so?

S.O.S
  • 848
  • 10
  • 30
  • It's because of different device pixel ratio. This maybe help https://stackoverflow.com/questions/24209628/how-to-override-device-pixel-ratio – pburgr Sep 14 '20 at 07:40
  • @pburgr Do you mind expounding a bit? I looked into device pixel ratio. It seems that the size of a "pixel' might vary from one screen to another. Is this the case? So the same number of pixels on one screen might be larger on another. Hence, the appearance of a larger browser on 24 inch screen. If this is the case, is there a way to calculate and adjust for the difference? Thanks! – S.O.S Sep 14 '20 at 15:55
  • check display setting in OS on both machines. First you need same zoom. Second while running browser by webdriver with your dimensions, type in console `console.log(window.devicePixelRatio);` to check the device pixel ratio. If it is different from the smaller monitor, you need to overwrite it. Btw why you need the specific dimensions? – pburgr Sep 15 '20 at 06:50
  • @pburgr thanks. You are correct. The device pixel ration on the smaller screen = 2.5 and on the larger screen = 1. Can you explain why this effects the physical height and width of the browser? Isn't the size of a pixel the same regardless? Also, how would I set device pixel ratio to 1 across all machines so that the size of the browser is consistent? Thanks again – S.O.S Sep 23 '20 at 18:04
  • there is a different between physical and logical screen size. That's why you see opticaly different size but same width and height. Check this https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio. I didn't tested it, but might worth the shot. Other practice to avoid those problems is to run tests in docker image, where you have better control of the environment. – pburgr Sep 24 '20 at 10:47

0 Answers0