As you know, Selenium webdriver Is very big project which supports many different languages to create test cases and execute them In different browsers. It support keyboard and mouse events very effectively using Advanced User Interaction API. I have listed Advanced User Interaction API
tutorial links On THIS PAGE. Page Zoom In/Out Is one of the tricky action and we can do It using advanced user Interaction API.
How to perform Zoom In/Out?
We will use webdriver advanced user Interaction API to perform this operation. We can perform page zoom action using Method sendKeys(WebElement element, java.lang.CharSequence... keysToSend). Here element Is webelement which will receive keystrokes and CharSequence Is sequence of characters to send on element.
Bellow given example will expain you more how to zoom In/out/default level of page.
package Testing_Pack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ZoomBrowser {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.com/");
}
@Test
public void getScrollStatus(){
//Call zooming functions to zoom in and out page.
zoomIn();
zoomOut();
zoomOut();
set100();
}
public void zoomIn(){
//To zoom In page 4 time using CTRL and + keys.
for(int i=0; i<4; i++){
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
}
}
public void zoomOut(){
//To zoom out page 4 time using CTRL and - keys.
for(int i=0; i<4; i++){
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
}
}
public void set100(){
//To set browser to default zoom level 100%
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));
}
}
When you run above example, 1st It will zoom In page 4 times then It will zoom out page 8 time and then It will set browser at default 100% zoom level.
doesn't work now. :(
ReplyDeleteI am getting the error as
ReplyDeleteorg.openqa.selenium.WebDriverException: unknown error: cannot focus element
public void zoomIn(){
ReplyDelete//To zoom In page 4 time using CTRL and + keys.
for(int i=0; i<2; i++){
driver.findElement(By.xpath(" .//*[@id='widget-zoom-in']")).click ();
//driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
}
}
public void zoomOut(){
//To zoom out page 4 time using CTRL and - keys.
for(int i=0; i<2; i++){
driver.findElement(By.xpath(".//*[@id='widget-zoom-out']")).click ();
}
}
this function is use full to zoom in google maps any way thanks to given such a useful solution
sridharnath.t
WebElement html = driver.findElement(By.tagName("html"));
ReplyDeletehtml.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD)); // Zoom In
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT)); // Zoom Out
this is working for me ..
WebElement html = driver.findElement(By.tagName("html"));
ReplyDeletehtml.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT)); //Zoom out
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD)); //Zoom in
I have tried but somehow its not working. Browser is getting opened and nothing is happening for Firfefox and
ReplyDelete"for chrome org.openqa.selenium.WebDriverException: unknown error: cannot focus element" this is the error
Hi Gaurav,
DeleteHave you solved this problem? Can you show me? I got the same error.
Thanks
Using Robot class worked for me:
ReplyDeletefor(int i=0; i<3; i++){
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_MINUS);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_MINUS);
}
this will zoom out 3 times.
Using Robot class worked for me:
ReplyDeletefor(int i=0; i<3; i++){
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_MINUS);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_MINUS);
}
this will zoom out 3 times.
Getting "java.lang.NullPointerException" exception executing the code .
ReplyDeleteGetting below error messages :-
ReplyDeletejava.lang.NullPointerException
at Selenium_exam.New_zoom.zoomIn(New_zoom.java:41)
at Selenium_exam.New_zoom.getScrollStatus(New_zoom.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)