Why to hardcode the browser to run the program? I want to decide that at run time only!
If this is your need, then you are right at the track. Necessity is the mother of invention
Now that you have learned the parametrization technique, let's play with that. Let's instruct the program about the browser in which we want to run the program during run time in the testng.xml file. Be prepared, it is going to be so much fun.
- Create a class called MyBrowserMyRules in the seleniumWithTestNG package.
- You will have the testng.xml file already, now replace that testng.xml file with the content below.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite" verbose="2">
<parameter name="browser" value="firefox"/>
<test name="Run multiple tests">
<classes>
<!-- <class name="seleniumWithTestNG.OpenGoogle"/>
<class name="seleniumWithTestNG.SkipTest"/>
<class name="seleniumWithTestNG.TestWithPriority"/>
<class name="seleniumWithTestNG.WhoIsThis"/> -->
</classes>
</test>
</suite>
NOTE: You can choose three different browsers: Chrome, Firefox, IE.
I presume that, you can understand the code given below. if you have ny doubts, even if you think it is silly, no problem, you can very well sign in to Gitbook and post the query by clicking on the + mark available on the right hand side.
Code for MyBrowserMyRules is given below.
package seleniumWithTestNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MyBrowserMyRules {
WebDriver driver;
@BeforeSuite
@Parameters ("browser")
public void launchBrowser(String ChosenBrowser){
if(ChosenBrowser.equalsIgnoreCase("Firefox")){
System.out.println("You have chosen "+ ChosenBrowser);
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");
// Launch the FireFox browser.
driver = new FirefoxDriver();
} else if (ChosenBrowser.equalsIgnoreCase("Chrome")) {
System.out.println("You have chosen "+ ChosenBrowser);
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\chrome driver\\chromedriver.exe");
// Launch the FireFox browser.
driver = new ChromeDriver();
} else if (ChosenBrowser.equalsIgnoreCase("IE")) {
System.out.println("You have chosen "+ ChosenBrowser);
System.out.println("May God bless You! I prefer Firefox only");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");
// Launch the FireFox browser.
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
}
@Test
public void openGoogle(){
driver.get("http://www.google.com");
}
@AfterSuite
public void closeBrowser(){
driver.quit();
}
}
Run the program (Right click on the class -> Run As -> TestNG Test) and check the output. I have chosen all the three browsers and below are the results.
OUTPUT FOR CHROME:
You have chosen chrome
PASSED: openGoogle
===============================================
Run multiple tests
Tests run: 1, Failures: 0, Skips: 0
===============================================
OUTPUT FOR FIREFOX:
You have chosen firefox
PASSED: openGoogle
===============================================
Run multiple tests
Tests run: 1, Failures: 0, Skips: 0
===============================================
OUTPUT FOR IE:
You have chosen IE
May God bless You! I prefer Firefox only
PASSED: openGoogle
===============================================
Run multiple tests
Tests run: 1, Failures: 0, Skips: 0
===============================================
Hope you had fun in understanding and implementing the parametrization technique. We will learn more in the upcoming chapters.