How can we set zoom level in selenium/protractor i.e zoom to 90%

為{幸葍}努か 提交于 2019-12-22 08:45:09

问题


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


回答1:


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.




回答2:


For Selenium:

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

For Protractor:

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



回答3:


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.




回答4:


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);


来源:https://stackoverflow.com/questions/29092434/how-can-we-set-zoom-level-in-selenium-protractor-i-e-zoom-to-90

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!