Tuesday 15 March 2016

Running WebDriver Test in Different Browsers



Once you are done with setup of eclipse project with Selenium WebDriver as shown in the previous tutorial then you are now ready to add your own test cases.But before adding test cases we will see how we can run our test cases on different browsers like firefox, chrome and Internet Explorer.

As we have created project with name BasicSelenium we need to create a class where we can write our test cases.

Video Tutorial -



1. Right click on BasicSelenium project  -> create new package named "com.selenium.tests".

2. Right click on BasicSelenium project ->  create mew java class with name "BrowserSample".

3. Add main method in the class as we know in java execution starts from main method.

4. Running the code is simple you can click on Run button at the top bar or right click on Java class and click on Run.

Running Test in FireFox Browser -

Copy the below code inside main method. Let us now understand what the below code does. The first line of code is responsible for launching FireFox browser. Then pass the url which you want to open and then stop the driver.
This is simple scenario where you want to open a particular url in FireFox Browser. 


package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrowserTests {

 public static void main(String[] args) throws Exception {
  WebDriver driver=new FirefoxDriver();
  driver.get("http://qaautomated.blogspot.in");
  Thread.sleep(3000);
  driver.quit();
 }

}

For Selenium WebDriver 3 you need to download driver for firefox browser from HERE and use below code to launch firefox browser -


package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrowserTests {

 public static void main(String[] args) throws Exception {
  WebDriver drive ;
System.out.println("launching firefox browser"); 
  System.setProperty("webdriver.gecko.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\geckodriver.exe");
  driver = new FirefoxDriver();
driver.get("http://qaautomated.blogspot.in");
  Thread.sleep(3000);
  driver.quit();
 }

}



Running Test in Chrome Browser -

One of the advantage of Selenium WebDriver is that it Supports Multiple Browsers and hence we can test with chrome browser as well. You can use below code for launching chrome browser and opening the url of your choice, but first you need to download chromedriver from HERE.


package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrowserTests {

 public static void main(String[] args) throws Exception {
  System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  Thread.sleep(1000);
  driver.get("http://qaautomated.blogspot.in");
  Thread.sleep(3000);
  driver.quit();
 }

}

Running Test in Internet Explorer -
 As we downloaded chromedriver to run our selenium webdriver test on Chrome browser similarly we will need IEDriver which you can download HERE.  Extract the zip folder and note down the path.Apart from this we need to set some capabilities so that we can open url in IE. Refer code given below.



package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class BrowserTests {

 public static void main(String[] args) throws Exception {
  
   DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
   capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
   System.setProperty("webdriver.ie.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\IEDriverServer.exe");
   WebDriver driver = new InternetExplorerDriver(capabilities);
   driver.manage().window().maximize();
   driver.get("http://qaautomated.blogspot.in");
   
   driver.quit();
   }
 }

I hope this post guide everyone who is looking to launch selenium webdriver test cases on different types of browsers.

1 comment: