如何在多个浏览器中运行 Selenium 测试以使用 Java 进行跨浏览器测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11823150/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:30:06  来源:igfitidea点击:

How to run Selenium tests in multiple browsers for cross-browser testing using Java?

javainternet-explorergoogle-chromefirefoxselenium-webdriver

提问by Nazifa Chowdhury

I am using Selenium WebDriver with Java & TestNG framework. I want to use Firefox, IE, Chrome in one single code at a time for doing cross-browser testing. I can only initialize Firefox as

我正在使用带有 Java 和 TestNG 框架的 Selenium WebDriver。我想一次在一个代码中使用 Firefox、IE、Chrome 进行跨浏览器测试。我只能将 Firefox 初始化为

driver = new FirefoxDriver();

but cannot initialize other browsers in the same way. For example:

但不能以同样的方式初始化其他浏览器。例如:

driver = new InternetExplorerDriver();

gives the error InternetExplorerDriver cannot be resolved.

给出错误 InternetExplorerDriver cannot be resolved.

driver = new ChromeDriver();

gives the error ChromeDriver cannot be resolved.

给出错误 ChromeDriver cannot be resolved.

How can I initialize IE and Chrome and execute my tests in all the desired browser ?

如何初始化 IE 和 Chrome 并在所有需要的浏览器中执行我的测试?

回答by alexey.chumagin

For C#

对于 C#

Add

添加

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;

Create a config file which will contain browser name. Implement a method for interaction with the config file

创建一个包含浏览器名称的配置文件。实现与配置文件交互的方法

And you can use below code for initialize browsers:

您可以使用以下代码来初始化浏览器:

string browser = GetConfigProperty("browser"); //Get browser name from the config
        switch (browser)
        {
            case "chrome":
                driver = new ChromeDriver();
                break;
            case "firefox":
                driver = new FirefoxDriver();
                break;
            case "InternetExplorer":
                driver = new InternetExplorerDriver();
                break;
        }

good luck!

祝你好运!

回答by Ripon Al Wasim

Create testng.xml having the following content:

创建具有以下内容的 testng.xml:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test" verbose="1">  
    <test name="Login_IE">
    <parameter name="browser" value="iexplore"/>
        <classes>
            <class name="com.gb.test.selenium.login.Login" />
        </classes>
    </test>
    <test name="Login_FF">
    <parameter name="browser" value="firefox"></parameter>
    <classes>
    <class name="com.gb.test.selenium.login.Login" />
    </classes>
    </test>
<test name="Login_Chrome">
    <parameter name="browser" value="chrome"></parameter>
    <classes>
    <class name="com.gb.test.selenium.login.Login" />
    </classes>
    </test>
</suite>

Now write your Java WebDriver code as below:

现在编写您的 Java WebDriver 代码如下:

@Parameters({"browser"})
  @BeforeClass
  public void setUp(String browser) {//throws MalformedURLException{
      if(browser.equalsIgnoreCase("iexplore")){
          File file = new File("E:\IEDriverServer_Win32_2.29.1\IEDriverServer.exe");
          System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
          driver = new InternetExplorerDriver();

      }
      if(browser.equalsIgnoreCase("firefox")){
          driver = new FirefoxDriver();
      }
      if(browser.equalsIgnoreCase("chrome")){
          File file = new File("F:\chromedriver_win_22_0_1203_0b\chromedriver.exe");//v22
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
        driver = new ChromeDriver();
      }

      jse = (JavascriptExecutor) driver;
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_IN_SECONDS, TimeUnit.SECONDS);
          driver.get("URLOfSite");
  }

回答by Rain

For Ruby you can do the following if you want to run your single code in all three browsers one after the other:

对于 Ruby,如果您想在所有三个浏览器中一个接一个地运行您的单个代码,您可以执行以下操作:

def all_browsers
  browsers = [:firefox,:ie,:chrome].each do |br|
  $driver = Selenium::WebDriver.for br
  $driver.manage.window.maximize
  $driver.navigate.to("http://google.com")
end

回答by djangofan

For Java, since the posters question was asking for a Java answer, I wrote a complete multi-browser parallel window example.

对于Java,由于poster 问题是要求Java 答案,所以我写了一个完整的多浏览器并行窗口示例