IEDriver - How To Resolve Set IE browser Zoom Level To 100% Error On RunTime

If your IE browser zoom level Is not set to 100% then you will get an error like "Browser zoom level was set to XYZ%. It should be set to 100% ".  Earlier we learnt In THIS POST -> How to resolve this error by setting IE browser's zoom level to 100% manually before running software test automation script. Now If you need
to run your software test script In different computers then everywhere you need to set browser's zoom level every time manually before running your software web application's test script. Otherwise your software automation test script will fail.

As described In my previous post, We can resolve "Enable Protected Mode For All Zones" error run-time. So can we do same thing for browser zoom level? Answer Is Yes. We can apply some extra code In our software automation test script to set browser zoom level 100% run-time automatically.

Here we will use same class DesiredCapabilities of WebDriver Interface which we have used In previous post to resolve error. Here we will set browser's capability to Ignore zoom setting and disable the native events. Then we will set browser zoom level to 100%. Bellow given code snippet will perform above given all operations and set browser zoom level 100%.

// Set desired capabilities to Ignore IEDriver zoom level settings and disable native events.
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("EnableNativeEvents", false);
caps.setCapability("ignoreZoomSetting", true);

// Initialize InternetExplorerDriver Instance using new capability.
WebDriver driver = new InternetExplorerDriver(caps);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

// Press CTRL + 0 keys of keyboard to set IEDriver Instance zoom level to 100%.
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));

To experiment above code snippet practically,

  • Set your IE browser's zoom level to 50% as shown In bellow Image



  • And then run bellow given test script In eclipse.

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.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class IESetZoom {
 public static void main(String[] args) throws Exception {

  // Set path of IEDriverServer.exe
  // Note : IEDriverServer.exe should be In D: drive.
  System.setProperty("webdriver.ie.driver", "D://IEDriverServer.exe");

  // Set desired capabilities to Ignore IEDriver zoom level settings and disable native events.
  DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
  caps.setCapability("EnableNativeEvents", false);
  caps.setCapability("ignoreZoomSetting", true);

  // Initialize InternetExplorerDriver Instance using new capability.
  WebDriver driver = new InternetExplorerDriver(caps);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

  // Press CTRL + 0 keys of keyboard to set IEDriver Instance zoom level to 100%.
  driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));

  // Load sample calc test URL
  driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");

  // Execute sample calc test.
  driver.findElement(By.xpath("//input[@id='1']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  Thread.sleep(5000);
  System.out.println("Calc test result Is : " + result);
  driver.close();
 }
}

Above test will be executed without any error. This Is the best way to resolve Set IE browser Zoom Level To 100% error. It will apply zoom level settings automatically on each machine where this software test script will be executed. So you no need to worry about It. Next post will show you how to handle SSL certificate error in IE browser.

2 comments:

  1. When I press Ctrl-0 it defaults to 125% Any idea why?

    ReplyDelete
  2. For the above program i am getting error

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method chord(CharSequence[]) in the type Keys is not applicable for the arguments (Keys, String)

    at Testing_Pack.IESetZoom.main(IESetZoom.java:28)

    Please suggest

    ReplyDelete