简体   繁体   English

如何在Selenium WebDriver Java中使用ChromeDriver

[英]How to use ChromeDriver in Selenium WebDriver Java

I'm trying to get it to run multiple tests with the @test, but I'm having a bit of trouble getting it to work. 我正在尝试让它使用@test运行多个测试,但是让它工作起来有点麻烦。 I found this example from https://sites.google.com/a/chromium.org/chromedriver/ Here is my code: 我从https://sites.google.com/a/chromium.org/chromedriver/找到了这个示例,这是我的代码:

package com.chrometester.webdriver;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.openqa.jetty.util.TestCase;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver; 
import org.testng.annotations.Test;



@RunWith(BlockJUnit4ClassRunner.class)
public class ChromeTest extends TestCase {

    private static ChromeDriverService service;
    private WebDriver driver;



    @Before
    public void createDriver() {
        driver = new RemoteWebDriver(service.getUrl(),
                DesiredCapabilities.chrome());
    }

    @Test
    public void testGoogleSearch() {
        driver.get("http://www.google.com");
        // rest of the test...
    }


    @After
    public void quitDriver() {
        driver.quit();
    }


}

Maybe the problem is that you are using the @Test annotation from TestNG instead of Junit. 也许问题在于您使用的是来自TestNG的@Test注释,而不是Junit。 Change the import, and it should work. 更改导入,它应该可以工作。

junit.framework.TestCase junit.framework.TestCase
Testcase import I suppose. 我想测试用例导入。

Maybe better to use TestNG instead of Junit. 使用TestNG代替Junit也许更好。 Here example 这里的例子

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import java.io.File;
import java.io.IOException;
public class Settings {
protected static WebDriver driver;
protected static String baseURL = "your base url";
public static ChromeDriverService service;
@BeforeClass
public static void createAndStartService() {
    service = new ChromeDriverService.Builder().usingDriverExecutable(new File("pass to your chrome")).usingAnyFreePort()
            .withSilent(true)
            .withVerbose(false)
            .build();
    try {
        service.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@AfterClass
public static void createAndStopService() {
    service.stop();
}
@BeforeMethod
public void setUp() throws IOException {
    driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
    driver.get(baseURL);
}
@AfterMethod
public void tearDown()
{
    driver.quit();
}

public static WebDriver getDriver()
{
    return driver;
}

And your class with test will be look like 与您的测试班将看起来像

public class Tests extends Settings {
    @Test(groups = "smoke tests registration", description = "example")
    public void test_base_url() {
        driver.get(baseURL);
    }

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

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