Earlier we learnt how to execute WebDriver software test In Internet Explorer browser using InternetExplorerDriver In previous post. We have also learnt how to resolved "Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones." error by setting "Enable Protected Mode" check box value same
for all zones manually In Internet Explorer browser.
Manual browser setting will remain limited to your computer only means If same software test will be executed on other computer then your test will fail If IE browser's "Enable Protected Mode" settings not configured In that computer. To resolve this Issue, We need something In our software automation test script which can configure all these settings Itself on run time. Means It should handle all zones protected mode security settings Itself when you run your test script on any machine. Is It possible? Answer Is "Yes".
We can do It using DesiredCapabilities class of selenium WebDriver software testing Interface. Bellow given code will set IEDriver capability to Ignore "Enable Protected Mode" settings and will allow you to run your test without any error regardless whatever your settings are there for "Enable Protected Mode" In IE browser.
// Set capability of IE driver to Ignore all zones browser protected mode settings.
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
// Initialize InternetExplorerDriver Instance using new capability.
WebDriver driver = new InternetExplorerDriver(caps);
To experiment It practically, I have created simple calc software test script as bellow. Before executing this example software test script, You need to reset all zones to default level as bellow.
- Open IE browser.
- Go to Tools -> Internet Options.
- Select Security tab.
- Click on "Reset all zones to default level" button and then click on "OK" button as shown In bellow Image.
Now "Enable Protected Mode" check box status will be different for all zones. Now execute bellow given test In eclipse and verify result In console.
package Testing_Pack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class IEEnableProtected {
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 capability of IE driver to Ignore all zones browser protected mode settings.
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
// Initialize InternetExplorerDriver Instance using new capability.
WebDriver driver = new InternetExplorerDriver(caps);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// 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");
System.out.println("Calc test result Is : " + result);
driver.close();
}
}
Above software test will run without any error regardless whatever settings are there for "Enable Protected Mode". Read next post to know how to set IE zoom level to 100% on runtime.
Very nicely explained about desired Capability Class. Keep up the good works Thanks
ReplyDeletefor some reason when I ran this code after resetting my protected mode to the default, I was getting this error
ReplyDeletenable to find element with xpath == //input[@id='1'] (WARNING: The server did not provide any stacktrace information)
only after I disabled the protected mode for all zones was I able to run this code successfully. Why is this?
Thank you,
Anon
This isn't a good permanent solution as described here: http://jimevansmusic.blogspot.com.au/2012/08/youre-doing-it-wrong-protected-mode-and.html
ReplyDeleteThe "Selenium Grid Extras" project looks like one solution if using Selenium Grid: http://elementalselenium.com/tips/70-grid-extras
but to just get the feature that updates IE settings it seems like a bit of a sledgehammer solution given everything else it does.