I have a test case and need to execute based on the browser name i.e. IE or Chrome. In this test case some part will depend on browser type.
How will I get the brows
To retrieve the Browser Name , Browser Version and Platform Name you can use either of the following approaches:
Using the API directly:
Code Block:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class browserCapabilitiesRetrieve {
public static void main(String[] args) {
// initial configuration
System.out.println("Browser Name is : "+((RemoteWebDriver) driver).getCapabilities().getBrowserName().toLowerCase());
System.out.println("Browser Version is : "+((RemoteWebDriver) driver).getCapabilities().getVersion().toString());
System.out.println("Platform Name is : "+((RemoteWebDriver) driver).getCapabilities().getPlatform().toString());
driver.quit();
}
}
Using the Capabilities object and getCapability()
method:
Code Block:
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class FirefoxBrowserCapabilitiesRetrieve_getCapability {
public static void main(String[] args) {
// initial configuration
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
System.out.println("Browser Name is : "+cap.getBrowserName());
System.out.println("Browser version is : "+cap.getVersion());
System.out.println("Platform is : "+cap.getPlatform().toString());
driver.quit();
}
}