Wednesday, October 15, 2014

3.Browser things in WebDriver

Accepting Untrusted SSL certificate in WebDriver for Chrome and Firefox Browser


firefox-certificate-errorSome time we get these SSL errors or notification when we try to execute our scripts, but WebDriver has provided us to give capabilities to the Chrome Driver/Firefox to handle these things
Here I am posting the code that would help you in resolving the SSL error mainly in Chrome Browser
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new ChromeDriver(capability);

Above script will help you in accepting all the SSL certificate and by doing so you would not get any SSL error while executing your code.
In Firefox you need to apply this code
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class acceptSSLCerti {
 public static void main(String[] args) {
 //Class ProfilesIni details
 ProfilesIni allProfiles = new ProfilesIni();
 // Use FirefoxProfile Constructor
 FirefoxProfile myProfile = allProfiles.getProfile("CertificateIssue"); myProfile.setAcceptUntrustedCertificates(true); myProfile.setAssumeUntrustedCertificateIssuer(false);
 WebDriver Driver = new FirefoxDriver(myProfile); Driver.get("http://abodeqa.wordpress.com");
}
}
Hope after using these code you would not face SSL error in your script execution.

Launch Chrome Browser Using WebDriver


Launching Chrome Browser using WebDriver
In WebDriver, We launch FireFox and Internet Explorer by using
WebDriver driver = new FirefoxDriver(); //this line would launch Firefox
WebDriver driver = new InternetExplorerDriver(); //this line would launch IE browser

But when we write below line like FireFox and IE
WebDriver driver = new ChromeDriver();
Then It throws Error and Here I am pasting Error Trace shown in Eclipse
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
IT seems really tedious na, But there is one way to resolve this Error and this could be done by using this
1- Download zip file of chromedriver for Windows from here
 2- Unzip downloaded Chromedriver for Windows and find the absolute path of chromedriver.exe
3- Now set Property of System by using this line
System.setProperty(“webdriver.chorme.driver”,”E:\\DD MISHRA\\workspace\\chromedriver_win_26.0.1383.0\\chromedriver.exe”);
and after this line write your traditional line to launch the browser like this
WebDriver driver =new ChromeDriver();

So why not we write one script that help us to see the launching of Chrome

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome {
WebDriver driver;
@Before
public void launchChrome()
{
System.setProperty("webdriver.chrome.driver", "E:\\DD MISHRA\\workspace\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
driver = new ChromeDriver();
}
@Test
public void testChrome()
{
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Selenium");
}
@After
public void kill()
{
driver.close();
driver.quit();
}
}


Unexpected error launching Internet Explorer. Protected Mode must be set to the same value


When we try to launch IE using WebDriver on a very first time on a fresh computer by using this code
WebDriver driver = new InternetExplorerDriver();
driver.get("http://abodeqa.wordpress.com");
Then we filled with amazement when we find this exception
org.openqa.selenium.WebDriverException: Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_25'
Driver info: driver.version: InternetExplorerDriver
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
 at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:409)
 at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:103)
 at org.openqa.selenium.ie.InternetExplorerDriver.setup(InternetExplorerDriver.java:104)
 at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:61)
On seeing the exception message we normally come to know the root of this exception and after few minute of search on Google we are loaded with two option.
Option 1:- by using capabilities
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new InternetExplorerDriver(caps);
Why We use Capability:
When the rewritten IE driver was first introduced, it was decided that it would enforce its required Protected Mode settings, and throw an exception if they were not properly set. Protected Mode settings, like almost all other settings of IE, are stored in the Windows registry, and are checked when the browser is instantiated.
But the Indian Idiotic IT department does not give proper rights to change the system setting..(This gives them immense pleasure like they have hired donkey and would keep them busy in finding the bypass of this hack of their non-sense credential hunt..)(:D) So to cope with such problems we need this capability.Because this bypass the registry check for initializing IE browser.
But this also cause some unwanted thing like
  1. Hangs
  2. Element location not working
  3. clicks not being propagated
Option 2: If you have rights to change the small setting in your system than try this
Go to Internet Option in IE –> Security–>Check or Unchecked “Enable Protected Mode.” protected-mode-setting

Note that you don’t have to change the slider for security level, and you don’t have to disable Protected Mode. I routinely run with Protected Mode turned on for all zones, as I think it provides a more secure browsing experience.

Adding add-on in Firefox and Chrome using WebDriver

Title sounds interesting. But think once again if you are told to start testing on instance of Firefox or Chrome installed with certain add-on then might be this article would favorably help you in time of need.
Let me do the operation on Firefox..so are you ready !!!
Steps:
1-  For this we need to create one profile
FirefoxProfile firefoxprofile = new FirefoxProfile();

2- Now go to add-on’s binary download page and download the add-on with .xpi extension and save it in Default Download location.Suppose taking path of .xpi file is c:/add-on
oh still you are thinking what we need to do then
3- Read the file location by using File class that we normally use for creation of files and directories, file searching, file deletion etc.
File addonpath = new File("path of .xpi file");
4-  Now time has came to call the addExtension() of FirefoxProfile class.This method will install add-on in new profile created with new Instance of Firefox.
firefoxprofile.addExtension(addonpath);
Now pass this profile in to new instance of FirefoxDriver
As a whole this code will look like this in Eclipse to install add-on in Firefox using WebDriver

FirefoxProfile firefoxprofile = new FirefoxProfile();
File addonpath = new File("path of .xpi file");
firefoxprofile.addExtension(addonpath);
WebDriver driver = new WebDriver(firefoxprofile)

So now task is complete for Firefox but still we need to do the same for Chrome.
But don’t think that same step we are going to follow again. Because in Chrome we need to create instance of ChromeOption class . This class has many methods like addExtension()- This method is used to install add-on in new instance of Chrome, setBinary()- This method is used to Sets the path to the Chrome executable, setArguments()- Adds additional command line arguments to be used when starting Chrome.
Steps for Chrome:
1-
Download the add-on in default location and read it using File Class again like above
File addonpath = new File("path of .crx file");
2- Now create instance of ChromeOptions
ChromeOptions chrome = new ChromeOptions();
chrome.addExtensions(addonpath);
WebDriver driver = new ChromeDriver(options);


No comments:

Post a Comment