eclipse 针对多个浏览器运行 selenium webdriver 测试用例

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

running selenium webdriver test cases against multiple browsers

javaeclipseantselenium-webdriverjunit4

提问by user1441341

I am new to selenium testing. I want to run selenium test caseson multiple browsers against internet explorer, Firefox, opera and chrome. What approach i have to follow. Can you people please suggest me which is the best process.

我是硒测试的新手。我想selenium test cases在多个浏览器上针对 Internet Explorer、Firefox、opera 和 chrome 运行。我必须遵循什么方法。你们可以建议我哪个是最好的过程。

Does selenium web driver supports multiple browsers or not???

selenium web driver 是否支持多个浏览器???

We had written login script. It runs successful for Firefox, chrome and internet explorer individually. But i want to run it for those multiple browsers sequentially.

我们已经编写了登录脚本。它分别在 Firefox、chrome 和 Internet Explorer 上运行成功。但我想按顺序为那些多个浏览器运行它。

采纳答案by user1441341

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Sample {
    private WebDriver _driver;

    @Test
    public void IEconfiguration() throws Exception {
        System.setProperty("webdriver.ie.driver",
        "D:/Softwares/Selenium softwares/drivers/IEDriverServer.exe");
        _driver = new InternetExplorerDriver();
        login();
    }

    @Test
    public void FFconfiguration() throws Exception {
        _driver = new FirefoxDriver();
        login();
    }

    @Test
    public void CRconfiguration() throws Exception {
        System.setProperty("webdriver.chrome.driver",
                "D:/Softwares/Selenium softwares/drivers/chromedriver.exe");
        _driver = new ChromeDriver();
        //_driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
        login();
    }

    public void login() throws Exception {
        _driver.get("http://www.google.com");
    }       
}

Before that we have to install the chrome and internet explorer drivers .exe files and run those.

在此之前,我们必须安装 chrome 和 Internet Explorer 驱动程序 .exe 文件并运行它们。

回答by Khalil

web driver supports multiple browsers of course, there is also support for mobile

web驱动当然支持多种浏览器,也支持手机

ChromeDriver

浏览器驱动程序

IEDiver

潜水员

FirefoxDriver

火狐驱动程序

OperaDriver

歌剧驱动程序

AndroidDriver

安卓驱动

Here is an exemple to run the same tests in multiple browsers.

这是在多个浏览器中运行相同测试的示例。

package ma.glasnost.test;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
        .........
DesiredCapabilities[] browserList = {DesiredCapabilities.chrome(),DesiredCapabilities.firefox(),DesiredCapabilities.internetExplorer(), DesiredCapabilities.opera()};
for (DesiredCapabilities browser : browserList)
{
    try {
        System.out.println("Testing in Browser: "+browser.getBrowserName());
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080/..."), browser);

Hope that helps.

希望有帮助。

回答by AndiDev

You could use the WebDriver Extensionsframework's JUnitRunner

您可以使用WebDriver 扩展框架的 JUnitRunner

Here is an example test googling for "Hello World"

这是一个示例测试谷歌搜索“Hello World”

@RunWith(WebDriverRunner.class)
@Firefox
@Chrome
@InternetExplorer
public class WebDriverExtensionsExampleTest {

    // Model
    @FindBy(name = "q")
    WebElement queryInput;
    @FindBy(name = "btnG")
    WebElement searchButton;
    @FindBy(id = "search")
    WebElement searchResult;

    @Test
    public void searchGoogleForHelloWorldTest() {
        open("http://www.google.com");
        assertCurrentUrlContains("google");

        type("Hello World", queryInput);
        click(searchButton);

        waitFor(3, SECONDS);
        assertTextContains("Hello World", searchResult);
    }
}

just make sure to add the WebDriver Extensionsframework amongst your maven pom.xml dependencies

只需确保在您的 maven pom.xml 依赖项中添加WebDriver 扩展框架

<dependency>
    <groupId>com.github.webdriverextensions</groupId>
    <artifactId>webdriverextensions</artifactId>
    <version>1.2.1</version>
</dependency>

The drivers can be downloaded using the provided maven plugin. Simply add

可以使用提供的 maven 插件下载驱动程序。只需添加

<plugin>
    <groupId>com.github.webdriverextensions</groupId>
    <artifactId>webdriverextensions-maven-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
        <execution>
            <goals>
                <goal>install-drivers</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <drivers>
            <driver>
                <name>internetexplorerdriver</name>
                <version>2.44</version>
            </driver>
            <driver>
                <name>chromedriver</name>
                <version>2.12</version>
            </driver>
        </drivers>
    </configuration>
</plugin>

to your pom.xml. Or if you prefer downloading them manually just annotate the test class with the

到你的 pom.xml。或者,如果您更喜欢手动下载它们,只需使用

@DriverPaths(chrome="path/to/chromedriver", internetExplorer ="path/to/internetexplorerdriver")

annotation pointing at the drivers.

指向驱动程序的注释。

Note that the above example uses static methods from the WebDriver Extensions Bot classto make the test more readable. However you are not tied to using them. The above test rewritten in pure Selenium WebDriver would look like this

请注意,上面的示例使用来自 WebDriver Extensions Bot 类的静态方法来使测试更具可读性。但是,您并不会依赖于使用它们。上面用纯 Selenium WebDriver 重写的测试看起来像这样

    @Test
    public void searchGoogleForHelloWorldTest() throws InterruptedException {
        WebDriver driver = WebDriverExtensionsContext.getDriver();

        driver.get("http://www.google.com");
        assert driver.getCurrentUrl().contains("google");

        queryInput.sendKeys("Hello World");
        searchButton.click();

        SECONDS.sleep(3);
        assert searchResult.getText().contains("Hello World");
    }