简体   繁体   English

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

[英]running selenium webdriver test cases against multiple browsers

I am new to selenium testing. 我是硒测试的新手。 I want to run selenium test cases on multiple browsers against internet explorer, Firefox, opera and chrome. 我想在多个浏览器上针对Internet Explorer,Firefox,opera和chrome运行selenium test cases What approach i have to follow. 我必须遵循什么方法。 Can you people please suggest me which is the best process. 请问有谁请建议我哪个是最好的流程。

Does selenium web driver supports multiple browsers or not??? selenium web驱动程序是否支持多个浏览器?

We had written login script. 我们写了登录脚本。 It runs successful for Firefox, chrome and internet explorer individually. 它单独为Firefox,Chrome和Internet Explorer运行成功。 But i want to run it for those multiple browsers sequentially. 但我想按顺序为那些多个浏览器运行它。

web driver supports multiple browsers of course, there is also support for mobile Web驱动程序当然支持多个浏览器,也有移动支持

ChromeDriver ChromeDriver

IEDiver IEDiver

FirefoxDriver FirefoxDriver

OperaDriver OperaDriver

AndroidDriver 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. 希望有所帮助。

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文件并运行它们。

You could use the WebDriver Extensions framework's JUnitRunner 您可以使用WebDriver Extensions框架的JUnitRunner

Here is an example test googling for "Hello World" 这是一个用于“Hello World”的测试Google搜索示例

@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 Extensions framework amongst your maven pom.xml dependencies 只需确保在maven pom.xml依赖项中添加WebDriver Extensions框架

<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. 可以使用提供的maven插件下载驱动程序。 Simply add 只需添加

<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. 到你的pom.xml。 Or if you prefer downloading them manually just annotate the test class with the 或者,如果您希望手动下载它们,只需使用。注释测试类

@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 class to make the test more readable. 请注意,上面的示例使用WebDriver Extensions Bot类中的静态方法来使测试更具可读性。 However you are not tied to using them. 但是,您并不依赖于使用它们。 The above test rewritten in pure Selenium WebDriver would look like this 在纯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");
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM