如何使用带有 Java 的 Selenium WebDriver 获取浏览器名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35258079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get browser name using Selenium WebDriver with Java?
提问by Swathi Chowdary
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.
我有一个测试用例,需要根据浏览器名称执行,即 IE 或 Chrome。在这个测试用例中,某些部分将取决于浏览器类型。
How will I get the browser name in between the execution? Example if it is IE, I need to pass the data. If it is Chrome browser, I need to select the data.
如何在执行之间获取浏览器名称?例如,如果是 IE,我需要传递数据。如果是Chrome浏览器,我需要选择数据。
采纳答案by Shubham Jain
You can use below code to know browser name, version and OS details:-
您可以使用以下代码了解浏览器名称、版本和操作系统详细信息:-
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
System.out.println(browserName);
String os = cap.getPlatform().toString();
System.out.println(os);
String v = cap.getVersion().toString();
System.out.println(v);
packages you need to import
你需要导入的包
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
Hope it will help you :)
希望它会帮助你:)
回答by Andrew Regan
You're the tester, so it's up to you to write code/scripts to explicitlytest each of the various browser/version combinations and their various nuances and subtleties (whilst trying to reuse as much logic as you can, minimise duplication etc.)
您是测试人员,因此由您编写代码/脚本来明确测试各种浏览器/版本组合及其各种细微差别和微妙之处(同时尝试尽可能多地重用逻辑,尽量减少重复等)
The nature of WebDriver is that you, the tester, are doing the driving - not the browser. Don't try to detect things.
WebDriver 的本质是您,测试人员,正在驾驶 - 而不是浏览器。不要试图检测事物。
So given that you have different behaviour for IE and for Chrome, you should explicitly create a WebDriver
instance for each (in different @Test
s) and set up the required data (likewise properties, Capabilities
etc.) as appropriate.
因此,鉴于您对 IE 和 Chrome 有不同的行为,您应该WebDriver
为每个(在不同的@Test
s 中)显式创建一个实例,并根据需要设置所需的数据(同样的属性Capabilities
等)。
By all means share common lookup code between the tests, but until your tests are robust and working you shouldn't try to refactor them.
无论如何,在测试之间共享共同的查找代码,但是在您的测试健壮且有效之前,您不应该尝试重构它们。
回答by user7610
In Python, you may access the driver.capabilities
dict like this
在 Python 中,您可以driver.capabilities
像这样访问dict
driver.capabilities['browserName']
https://groups.google.com/forum/#!topic/selenium-users/nbSujBSc6q8
https://groups.google.com/forum/#!topic/selenium-users/nbSujBSc6q8
回答by DebanjanB
To retrieve the Browser Name, Browser Nameand Platform Nameyou can use either of the following approaches:
要检索Browser Name、Browser Name和Platform Name ,您可以使用以下任一方法:
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 Capabilitiesobject 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(); } }
直接使用API:
代码块:
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(); } }
使用Capabilities对象和
getCapability()
方法:代码块:
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(); } }