Tuesday 9 September 2014

To make browser and URL configurable in selenium webdriver



Objective : 

Our main objective is to make the URL name and browser configurable , so that it can be changed as per Testing needs. Many a time the regression suite needs to be run on different environments. In that case, URL must be changed, if we have 100 test cases, it would not be manually feasible to change 100 scripts, hence we make it configurable.


Advantages: 

1. If we make the URL to be placed at one place, we need not write the entire URL name in all the test scripts.

2. For web based application, it needs to be tested on several browsers, hence keeping the browser name in config files is helpful.


Steps to create Config.properies file for URL and Browser.


         1.       Create a folder name Config files in your project structure.

    2.      Create a text file and rename it as config.properties.





         3.       Configure the URL name and browser to be specified in this Config.properties file.

4.      Create a new test case and use the URL and browser properties. 
a)      Initialize the properties file. 
b)      Use the properties file attribute as shown below.

   

package Testcases;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

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

public class Urlconfig {
     
      @Test
      public void urlconfig() throws Throwable
     
      {
           
            Properties Config=new Properties();
            FileInputStream file=new FileInputStream(System.getProperty("user.dir")+"\\ConfigFiles\\Config.properties");
            Config.load(file);
           
            WebDriver driver = new FirefoxDriver();
            driver.get(Config.getProperty("URL"));
      }

}


5. Configure the URL name and browser to be specified in this Config.properties file
  
      6.    Create a file in package common libraries named BrowserConfig.java

       
package CommonLibraries;
      import java.io.FileInputStream;
import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.util.Properties;

import org.junit.Test;
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;



public class BrowserConfig_new {
     



     
     
           
            //public static Properties TestConfig=null;
            //String browser="firefox";
            public static Properties TestConfig=null;
           
            public static WebDriver driver=null;
            public static EventFiringWebDriver usedriver=null;
            //public static EventFiringWebDriver driverIE=null;

                 
           
      public static void openbrowser() throws IOException {

           
           
            if(driver==null)
            {
            /*Properties p=new Properties();
      FileInputStream f=new FileInputStream("D:\\Workspace\\TestProject\\TestConfig\\TestConfig.properties");
      p.load(f);*/
                 
                 
                  Properties testconfig=new Properties();
                  FileInputStream f=new FileInputStream(System.getProperty("user.dir")+"\\ConfigFiles\\Config.properties");
                  testconfig.load(f);
                  //start();
     
                 
                       
      if(testconfig.getProperty("browser").equals("firefox"))
                  {
     
            driver = new FirefoxDriver();
            //driver.get(TestConfig.getProperty("sitename"));
           
                  }
     
      else if(testconfig.getProperty("browser").equals("ie"))
      {

            System.setProperty("webdriver.ie.driver","D:\\Workspace\\TestProject\\iedriver\\IEDriverServer.exe");
           
             driver =new InternetExplorerDriver();
           
            //driver.get("http://www.rediff.com/");
                 
      }

      else if(testconfig.getProperty("browser").equals("chrome"))
      {

            System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\ChromeDriver\\chromedriver.exe");
            //System.setProperty(("userdir")+ "chromedriver\\chromedriver.exe","chromedriver.exe");
     
             driver= new ChromeDriver();
           
            //driver.get("https://in.yahoo.com/?p=us");
                 
      }
     
      usedriver = new EventFiringWebDriver(driver);
      }
           
     
           
      }
      }





      7.    Now specify the browser to be launched for the test in the Config.properties file.

8.    The browser specified will be launched and URL from the config file will be fetched and will open in the browser.

9.    Project Structure for this should be as follows:



 













 

No comments:

Post a Comment