In my previous post, we talked about how can we use various browsers for testing using selenium WebDriver.
Refer below post:
Now. If you want to use multiple browsers and you want them to choose its value dynamically, let us suppose from properties file or from some xml file, we can do that.
So, you can set the browser value, suppose in properties file (at run time), then that particular browser should be invoked for testing.
First of all, you should know how to read properties file.
Please refer below post for learning How to read propertiesfile in selenium (java)?
Once you are able to read properties file, below lines of code will help you to select browser from properties file –
package com.aayushCorp.utilities;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class BrowserSelection {
public WebDriver selectBrowser() throws Exception {
FileInputStream fis = null;
File configfile = new File( "C:UsersPrakashworkspaceAayush-SeleniumsrccomaayushCorpexternalResourcesConfiguration.Properties");
fis = new FileInputStream(configfile);
Properties prop = new Properties();
prop.load(fis);
WebDriver driver = null;
String browser = prop.getProperty("browser");
while (browser == null) {
System.out.println("Browser is not specified in Configuration file. Terminating process !!!");
System.exit(0);
}
if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
System.out.println("Browser selected for testing is : Mozilla Firefox");
} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:chromedriver.exe");
driver = new ChromeDriver();
System.out.println("Browser selected for testing is : Google Chrome");
} else if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", "C:IEDriverServer.exe");
driver = new InternetExplorerDriver();
System.out.println("Browser selected for testing is : Internet Explorer");
} else {
System.out.println("Selected browser value should be either firefox or chrome or ie --> Update in Configuration file is required");
System.exit(0);
}
return driver;
}
}
In above peice of code, Selenium will launch either firefox or chrome or ie based on what value you specifies in properties file.
Hope this helps. Post your comments / suggestions.