I am using Selenium WebDriver with Java & TestNG framework. I want to use Firefox, IE, Chrome in one single code at a time for doing cross-browser testing. I can only in
For Ruby you can do the following if you want to run your single code in all three browsers one after the other:
def all_browsers
browsers = [:firefox,:ie,:chrome].each do |br|
$driver = Selenium::WebDriver.for br
$driver.manage.window.maximize
$driver.navigate.to("http://google.com")
end
For Java, since the posters question was asking for a Java answer, I wrote a complete multi-browser parallel window example.
For C#
Add
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
Create a config file which will contain browser name. Implement a method for interaction with the config file
And you can use below code for initialize browsers:
string browser = GetConfigProperty("browser"); //Get browser name from the config
switch (browser)
{
case "chrome":
driver = new ChromeDriver();
break;
case "firefox":
driver = new FirefoxDriver();
break;
case "InternetExplorer":
driver = new InternetExplorerDriver();
break;
}
good luck!
Create testng.xml having the following content:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test" verbose="1">
<test name="Login_IE">
<parameter name="browser" value="iexplore"/>
<classes>
<class name="com.gb.test.selenium.login.Login" />
</classes>
</test>
<test name="Login_FF">
<parameter name="browser" value="firefox"></parameter>
<classes>
<class name="com.gb.test.selenium.login.Login" />
</classes>
</test>
<test name="Login_Chrome">
<parameter name="browser" value="chrome"></parameter>
<classes>
<class name="com.gb.test.selenium.login.Login" />
</classes>
</test>
</suite>
Now write your Java WebDriver code as below:
@Parameters({"browser"})
@BeforeClass
public void setUp(String browser) {//throws MalformedURLException{
if(browser.equalsIgnoreCase("iexplore")){
File file = new File("E:\\IEDriverServer_Win32_2.29.1\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
}
if(browser.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
}
if(browser.equalsIgnoreCase("chrome")){
File file = new File("F:\\chromedriver_win_22_0_1203_0b\\chromedriver.exe");//v22
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
}
jse = (JavascriptExecutor) driver;
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_IN_SECONDS, TimeUnit.SECONDS);
driver.get("URLOfSite");
}