简体   繁体   English

如何使用 Selenium WebDriver 和 Java 获取浏览器名称?

[英]How to get browser name using Selenium WebDriver with Java?

I have a test case and need to execute based on the browser name ie IE or Chrome.我有一个测试用例,需要根据浏览器名称执行,即 IE 或 Chrome。 In this test case some part will depend on browser type.在这个测试用例中,某些部分将取决于浏览器类型。

How will I get the browser name in between the execution?如何在执行之间获取浏览器名称? Example if it is IE, I need to pass the data.例如,如果是 IE,我需要传递数据。 If it is Chrome browser, I need to select the data.如果是 Chrome 浏览器,我需要选择数据。

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;

OR或者

   Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();

    String browserName = cap.getBrowserName();
    String browserVersion = (String)cap.getCapability("browserVersion");
    String osName = Platform.fromString((String)cap.getCapability("platformName")).name().toLowerCase();

    return browserName + browserVersion + "-" + osName;

Hope it will help you :)希望它会帮助你:)

In Python, you may access the driver.capabilities dict like this在 Python 中,您可以像这样访问driver.capabilities字典

driver.capabilities['browserName']

https://groups.google.com/forum/#!topic/selenium-users/nbSujBSc6q8 https://groups.google.com/forum/#!topic/selenium-users/nbSujBSc6q8

To retrieve the Browser Name , Browser Version and Platform Name you can use either of the following approaches:要检索浏览器名称浏览器版本平台名称,您可以使用以下任一方法:

  • Using the API directly:直接使用 API:

    • 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:使用Capabilities对象和getCapability()方法:

    • 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(); } }

For those using C# you can do the following to detect browser when using either the local browser driver or remotewebdriver:对于使用 C# 的用户,您可以在使用本地浏览器驱动程序或 remotewebdriver 时执行以下操作来检测浏览器:

        public static bool IsSafari(IWebDriver driver)
        {
            // Using remotewebdriver e.g. browserstack
            if (SomeConfig.UsingRemoteWebDriver)
                return GetRemoteDriverBrowserName(driver) == "safari";
            // Using local browser driver
            return driver.GetType() == typeof(SafariDriver);
        }

        public static bool IsInternetExplorer(IWebDriver driver)
        {
            // Using remotewebdriver e.g. browserstack
            if (SomeConfig.UsingRemoteWebDriver)
                return GetRemoteDriverBrowserName(driver) == "internet explorer";
            // Using local browser driver
            return driver.GetType() == typeof(InternetExplorerDriver);
        }

        private static string GetRemoteDriverBrowserName(IWebDriver driver)
        {
            return ((RemoteWebDriver)driver).Capabilities.GetCapability("browserName").ToString().ToLower();
        }

You're the tester, so it's up to you to write code/scripts to explicitly test 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. WebDriver 的本质是,测试者,在做驱动——而不是浏览器。 Don't try to detect things.不要试图检测事物。

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 有不同的行为,您应该为每个(在不同的@Test中)显式创建一个WebDriver实例,并根据需要设置所需的数据(同样是属性、 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.无论如何,在测试之间共享通用的查找代码,但是在你的测试是健壮的并且可以工作之前,你不应该尝试重构它们。

To list all capabilities in Java you can use:要列出 Java 中的所有功能,您可以使用:

import org.openqa.selenium.remote.RemoteWebDriver;

for (String s : ((RemoteWebDriver) driver).getCapabilities().getCapabilityNames()) {
    System.out.println(s + " :: " + ((RemoteWebDriver) driver).getCapabilities().getCapability(s));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium WebDriver + Java获取浏览器控制台错误消息? - How to get browser console error messages using Selenium WebDriver + Java? 如何使用 Selenium WebDriver + Java 获取浏览器实例的 PID? - How to get PID of a browser instance using Selenium WebDriver + Java? 如何通过将Selenium WebDriver与Java一起在同一浏览器中打开新标签页? - How to open a new tab in the same browser by using Selenium WebDriver with Java? 如何使用Java关闭Selenium WebDriver中的子浏览器窗口 - How to close child browser window in Selenium WebDriver using Java 使用 Java 使用 Selenium WebDriver 捕获浏览器日志 - Capturing browser logs with Selenium WebDriver using Java 如何<a class </a>在Selenium WebDriver中使用Java</a>单击带有标签名称的链接 - How to click a link with Tag name <a class </a> using Java in Selenium WebDriver 如何使用 Selenium webdriver 打开特定浏览器 - How to open specific browser using Selenium webdriver 如何使用 Selenium Webdriver 验证浏览器通知 - How to validate browser notifications using Selenium Webdriver 如何使用Java使用Selenium WebDriver获取所有跨度值? - How to get all the span value with Selenium WebDriver using Java? 如何使用Java使用Selenium Webdriver在此html中获取标记值? - How to get the <b> tag value in this html with Selenium Webdriver using Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM