【问题标题】:running selenium webdriver test cases against multiple browsers针对多个浏览器运行 selenium webdriver 测试用例
【发布时间】:2013-04-11 05:09:20
【问题描述】:

我是硒测试的新手。我想在多个浏览器上针对 Internet Explorer、Firefox、Opera 和 chrome 运行 selenium test cases。我必须遵循什么方法。各位大侠能不能给我推荐一下最好的流程。

selenium web 驱动是否支持多种浏览器???

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

【问题讨论】:

  • 我使用参数化方法自动化了多个浏览器的测试用例。我使用了link 中提到的示例

标签: java eclipse ant selenium-webdriver junit4


【解决方案1】:
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");
    }       
}

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

【讨论】:

【解决方案2】:

您可以使用 WebDriver Extensions 框架的 JUnitRunner

这是“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);
    }
}

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

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

可以使用提供的 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>

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

注释测试类
@DriverPaths(chrome="path/to/chromedriver", internetExplorer ="path/to/internetexplorerdriver")

指向驱动程序的注释。

请注意,上面的示例使用来自 WebDriver Extensions Bot class 的静态方法,以使测试更具可读性。但是,您并不依赖于使用它们。上面用纯 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");
    }

【讨论】:

    【解决方案3】:

    web驱动当然支持多种浏览器,也有对移动端的支持

    ChromeDriver

    IEDiver

    FirefoxDriver

    OperaDriver

    AndroidDriver

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

    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);
    

    希望对您有所帮助。

    【讨论】:

    • 感谢@Khalil 的回复。您能否提供任何示例。
    • 只要给出你想做的,你使用的浏览器,我会举个例子。注意:如果您查看我发布的链接,您将开始了解每个浏览器驱动程序。不要犹豫再问我任何澄清
    猜你喜欢
    • 2012-04-03
    • 2016-08-16
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多