Simulating Browser Zoom Controls with Selenium
Though such scenarios are not common in automation activities, you may encounter need to simulate browser zoom operation. These scenarios are common if you need to carry out accessibility testing. In this tutorial, we have implemented Selenium zoom example with Java’s Action class.
Zoom In and Zoom Out operations are carried out by ‘CTRL +’ and ‘CTRL – ‘ key press respectively. We will simulate these commands in Selenium WebDriver using Action class. Commands for those are Keys.chord(Keys.CONTROL, Keys.ADD) and Keys.chord(Keys.CONTROL, Keys.SUBTRACT). Similarly for zooming back in to the default level, Keys.chord(Keys.CONTROL, “0”) command is used.
To apply zoom on web page, we need to reference it with its HTML tag by using driver.findElement(By.tagName(“html”)) code. Now, let’s see the above commands in action with below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public void ZoomExample() throws Exception { driver = new FirefoxDriver(); baseUrl = "https://www.google.co.in"; driver.get(baseUrl + "/"); driver.findElement(By.tagName("html")).sendKeys( Keys.chord(Keys.CONTROL, Keys.ADD)); driver.findElement(By.tagName("html")).sendKeys( Keys.chord(Keys.CONTROL, Keys.ADD)); driver.findElement(By.tagName("html")).sendKeys( Keys.chord(Keys.CONTROL, Keys.ADD)); driver.findElement(By.tagName("html")).sendKeys( Keys.chord(Keys.CONTROL, Keys.ADD)); driver.findElement(By.tagName("html")).sendKeys( Keys.chord(Keys.CONTROL, Keys.ADD)); driver.findElement(By.tagName("html")).sendKeys( Keys.chord(Keys.CONTROL, Keys.SUBTRACT)); driver.findElement(By.tagName("html")).sendKeys( Keys.chord(Keys.CONTROL, "0")); } |
The above code example will zoom web page in by 5 levels, zoom out by 1 level and will return to the default zoom level.
As you can see from the above steps, Selenium zoom implementation is easy using Selenium WebDriver. Try it out at your end and let us know how it goes.
Keys.Chord is not a valid definition in OpenQA.selenium.Keys
Can you let me know if any library needs to be added or is there any equivalent for Keys.Chord ?
not working for chrome browser getting exception as :
org.openqa.selenium.WebDriverException: unknown error: cannot focus element
(Session info: chrome=44.0.2403.155)
(Driver info: chromedriver=2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 28 milliseconds
Build info: version: ‘2.45.0’, revision: ‘5017cb8e7ca8e37638dc3091b2440b90a1d8686f’, time: ‘2015-02-27 09:10:26’
System info: host: ‘SANSINGH2L2’, ip: ‘10.192.109.48’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.7.0_71’
Session ID: 6bcfd7bdb232d97dc291415b5501e02c
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=D:\Users\sansingh\AppData\Local\Temp\scoped_dir8548_32416}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=44.0.2403.155, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, hasTouchScreen=false, applicationCacheEnabled=false, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:89)
at com.selenium.helperWinAPI.HelperFunctionWinAPI.increaseZoomLevel(HelperFunctionWinAPI.java:214)
at com.selenium.spiketest.TestBrowserDD.testZoomLevel(TestBrowserDD.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
This does not work with Chrome. Is it a bug with the library?
Doesn’t work with chrome.