Implementing openBrowser And closeBrowser Functions In WebDriver Test

Whenever you lot start your whatever webdriver test, First of all you lot ask to start your browser. And at the destination of your test, You ask to unopen your browser. Now supposing you lot convey 100s of examine cases thence you lot volition write browser opening in addition to closing code In each in addition to every test? Or supposing you lot wants to run all your tests In Google chrome browser Instead of Mozilla Firefox browser thence you lot volition drib dead to alter code In your each in addition to every examine cases?

Other i affair you lot ask to verify earlier opening novel browser In selenium webdriver Is -> Supposing you lot are running multiple webdriver examine cases in addition to browser Is already opened during previous examine execution thence you lot practise non wants to opened upward novel browser. In this case, You volition convey to lay exceptional banking concern fit which tin verify that If browser Is already opened during previous examine execution thence practise non opened upward novel browser in addition to start executing examine In existing browser.


To overcome all these things, You convey to practise mutual component which tin perform bellow given actions.
  1. Can Open browser when you lot telephone phone It from whatever examine case.
  2. Can opened upward browser As per your choice. Means If you lot wants to run all your examine In google chrome browser thence You tin practise It past times changing only unmarried word.
  3. Can banking concern fit If browser window Is already opened during previous examine execution thence practise non opened upward novel window in addition to start execution In existing window.
Follow bellow given steps for configure initBrowser() component which tin perform all inwards a higher house things.
  • Create Testng_Pack bundle Under your projection folder.
  • Under Testng_Pack package, Create novel file(Right click on bundle folder-> New-> File) named SYSPARAM.properties.
  • Under Testng_Pack package, Create novel class(If non exist) named CommonFunctions. If you lot convey experimented my previous posts examples of creating full general component to COMPARE TWO STRINGS, INTEGERS, DOUBLES thence you lot volition convey already CommonFunctions class.
  • Under Testng_Pack package, Create novel shape called openCloseBrowser_Test. Now your bundle construction volition looks similar bellow.


Copy glue bellow plough over lines In SYSPARAM.properties file.
SYSPARAM.properties
#CHRM BrowserToTestIn=MFF

In inwards a higher house SYSPARAM.properties file, If you lot volition set BrowserToTestIn = CHRM thence your all tests volition last executed In Google chrome browser.


Copy glue bellow given lines In CommonFunctions class.
CommonFunctions.java
package Testng_Pack;  import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver;  populace shape CommonFunctions {  populace static WebDriver driver=null;   populace boolean browserAlreadyOpen=false;  populace static Properties SYSPARAM =null;    //To Initialize .properties file.  populace void initData() throws IOException{   SYSPARAM = novel Properties();   FileInputStream Ist = novel FileInputStream(System.getProperty("user.dir")+"//src//Testng_Pack//SYSPARAM.properties");   SYSPARAM.load(Ist);  }    populace void initBrowser(){   //Check If browser Is already opened during previous examine execution.   if(!browserAlreadyOpen){    //Read value of 'BrowserToTestIn' cardinal from SYSPARAM.properties file.    //If cardinal value Is MMF thence execute If disceptation    //If cardinal value Is CHRM thence execute else statement.    if(SYSPARAM.getProperty("BrowserToTestIn").equals("MFF")){     driver = novel FirefoxDriver();       }else if(SYSPARAM.getProperty("BrowserToTestIn").equals("CHRM")){     //Write lines to opened upward chrome browser.     driver = novel ChromeDriver();    }   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);   driver.manage().window().maximize();   //At terminal browserAlreadyOpen flag volition becomes truthful to non opened upward novel browser when start executing side past times side test.   browserAlreadyOpen=true;   }  }    //To Close Browser  populace void closeBrowser(){   driver.quit();   browserAlreadyOpen=false;  }  }

Now I tin call initBrowser() component In all my examine cases to opened upward browser. I convey uncomplicated instance of unmarried examine instance every bit bellow. You tin practise multiple examine cases in addition to call initBrowser() component at firstly of examine instance in addition to . Look at bellow given example.

openCloseBrowser_Test
package Testng_Pack;  import java.io.IOException;  import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  populace shape openCloseBrowser_Test extends CommonFunctions{     @BeforeTest  populace void StartBrowser_NavURL() throws IOException {   initData();    }    @AfterTest  populace void ClosingBrowser() {     closeBrowser();    }    @Test  populace void OpenBrowserAndURL(){   initBrowser();   driver.get("http://only-testing-blog.blogspot.in/");  } }

This way, Now I tin call initBrowser() and closeBrowser(); component In whatever of my examine instance whic volition trim back the size of my code in addition to I convey to alter alone i parameter to alter browser. You tin write other browsers opening syntax Inside initBrowser(); alongside else syntax If you lot wants to run your examine In whatever other browser.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post