Well implementation of cross browser highly depends on what testing framework you use. In case of you are using TestNG, you can create testng.xml file which will have parameters such as BrowserName, browserVersions, OS, etc and pass those to as parameters to TestNG test class.
<suite name="smoke-test-suite" parallel="tests" thread-count="5">
<parameter name="ReportLocation" value="."></parameter>
<parameter name="remoteURL" value="localhost"></parameter>
<parameter name="remotePort" value="4444"></parameter>
<parameter name="OS" value="WINDOWS"></parameter>
<parameter name="browser" value="firefox"></parameter>
<parameter name="version" value="23.0"></parameter>
<parameter name="internal" value="true"></parameter>
<test name="SingleTest" >
<classes>
<class name="SingleTest" >
</class>
</classes>
</test>
</suite>
Sample TestNG.xml
In TestNG test class, you can use @BeforeClass or @BeforeMethod or @BeforeSuite annotation and read TestNG.xml file parameters via @Parameter annotation.
@BeforeMethod
@Parameters({ "remoteURL", "baseURL", "OS", "browser", "version", "internal" })
public void beforeTest(String remoteURL, String baseURL, String OS, String browser, String version, String internal)
throws IOException {
this.testCase = new TestCase();
this.testCase.setExecutionEnvironment("{browser:"+browser+",browserVersion:"+version+",OperatingSystem:"+OS+"}");
this.testCase.setParentURL(baseURL);
this.testCase.setTestCaseId("KT"+testCaseCount++);
this.testCase.setScreenshotDirectory(log.getReportDirectory()+"\\images");
config = new WebDriverConfig();
config.setRemoteURL(remoteURL);
this.baseURL = baseURL;
config.setOS(OS);
config.setBrowserName(browser);
config.setBrowserVersion(version);
config.setIntenal(Boolean.parseBoolean(internal));
driver = xRemoteWebDriver.getInstance(config, log);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(this.baseURL);
}
Using above annotations you can define DesiredCapabilities and initiate appropriate browser.
public static WebDriver getInstance(WebDriverConfig config, Log log) throws MalformedURLException, FileNotFoundException{
if(!config.isIntenal()){
DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName(config.getBrowserName());
cap.setCapability("platform", config.getOS());
cap.setVersion(config.getBrowserVersion());
return
new xRemoteWebDriver(new URL(config.getRemoteURL()), cap, log);
}
else{
File chromeDriver = new File("drivers\\chromedriver.exe");
File IEDriver = new File("drivers\\IEDriverServer.exe");
if(config.getBrowserName().equals("firefox"))
return new FirefoxDriver();
if(config.getBrowserName().equals("chrome")){
if(!chromeDriver.exists()) throw new FileNotFoundException("chromedriver.exe not found under 'drivers' folder");
System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath());
return new ChromeDriver();
}
if(config.getBrowserName().equals("internet explorer")){
if(!IEDriver.exists()) throw new FileNotFoundException("IEDriverServer.exe is not found under 'drivers' folder");
System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath());
return new InternetExplorerDriver();
}
}
return new FirefoxDriver();
}