4

How can we set zoom level in selenium/protractor zoom page size to 90 percent etc

Milan Kumar
  • 401
  • 1
  • 5
  • 15

4 Answers4

5

Dont know whether an equivalent is there in protractor (since never worked), but this is how I would zoom-in and out in java webdriver via JavascriptExecutor using:

document.body.style.transform='scale(0.9)'

where 0.9 is scale percentage. Though for zoom u can also use

document.body.style.zoom='90%'

but this won't work on firefox and opera. Hope this could be helpful.

Vivek Singh
  • 3,501
  • 2
  • 20
  • 27
3

For Selenium:

((JavascriptExecutor)driver).executeScript("document.body.style.zoom='90%';");

For Protractor:

browser.executeScript("document.body.style.zoom='90%';");
budi
  • 5,903
  • 8
  • 51
  • 77
Milan Kumar
  • 401
  • 1
  • 5
  • 15
  • 1
    The protractof form works but button are not clickable after setting zoom level ? Any cluse on that ? The error I get is the following :[08:03:56] E/launcher - ElementNotVisibleError: element not visible (Session info: chrome=56.0.2924.87) (Driver info: chromedriver=2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 3.10.0-514.6.1.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information) – jmcollin92 Feb 02 '18 at 07:17
2

To follow what is suggested for other selenium language bindings at Selenium webdriver zoom in/out page content, one way to set the zoom level would be to send CTRL (COMMAND on mac) + - combination (to mimic the "zoom level down" action):

browser.actions().keyDown(protractor.Key.CONTROL).sendKeys(protractor.Key.SUBTRACT).keyUp(protractor.Key.CONTROL).perform();

or on mac:

browser.actions().keyDown(protractor.Key.COMMAND).sendKeys(protractor.Key.SUBTRACT).keyUp(protractor.Key.CONTROL).perform();

Though, I would consider an alternative approach here (not tested).

Open firefox, set the zoom level for a desired site (firefox by default would remember site-specific zoom levels), then open "Troubleshooting information" and locate your firefox profile on disk. Then use the instructions provided at How to change firefox profile to start firefox with a pre-saved profile while running your protractor tests.

Community
  • 1
  • 1
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
  • Note that `CONTROL` is pressed down but never released. You need to either do `sendKeys(Keys.NULL)` or `keyUp(protractor.Key.CONTROL)` or just do a chord `protractor.Key.chord(protractor.Key.CONTROL, protractor.Key.SUBTRACT)` – tepez Jan 15 '16 at 11:55
  • 1
    Just as a note, I've had some issues with this when sharding tests, as the window must be in focus for this to work. – user2020347 Apr 06 '16 at 23:13
  • This don't work for me with google-chrome for Linux v 56.0 64 Bit on Centos 3.10.0-514.6.1.el7.x86_64. Any clues ? – jmcollin92 Feb 02 '18 at 07:15
0

Here are two ways the zoom level can be altered with both Selenium and Java (one of these approaches is for Chrome and the other is for Firefox):


Chrome

When using v̲e̲r̲s̲i̲o̲n̲ ̲3̲.̲3̲.̲1 of the Selenium Java Client Driver and C̲h̲r̲o̲m̲e̲D̲r̲i̲v̲e̲r̲ ̲2̲.̲2̲8, the following works (where the number in single quotes represents the zoom level to use; 1 = 100%, 1.5 = 150%, etc.):

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.body.style.zoom = '1.5'");

Firefox

The zoom level can be modified with the following:
1. The aforementioned Java Client Driver
2. G̲e̲c̲k̲o̲D̲r̲i̲v̲e̲r̲ ̲v̲0̲.̲1̲5̲.̲0
3. These classes:
java.awt.Robot
java.awt.event.KeyEvent

First of all, instantiate the Robot class:

Robot robot = new Robot();

This code causes the zoom level to decrease:

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_MINUS);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_MINUS);

This code causes the zoom level to increase:

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_EQUALS);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_EQUALS);
User253489
  • 139
  • 1
  • 4