Selenium Java(5)集成testng

版权声明:转载请注明出处 https://blog.csdn.net/cowbin2012/article/details/90200272

添加依赖

在pom.xml中添加需要的依赖,这里只要selenium和testng就行

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
</dependency>

编写测试类

public class TestBaidu {

     WebDriver driver;

    @Test(priority = 0)
    public  void chrome() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized"); // 启动时自动最大化窗口
        options.addArguments("--disable-popup-blocking"); // 禁用阻止弹出窗口
        options.addArguments("no-sandbox"); // 启动无沙盒模式运行
        options.addArguments("disable-extensions"); // 禁用扩展
        options.addArguments("no-default-browser-check"); // 默认浏览器检查
        Map<String, Object> prefs = new HashMap();
        prefs.put("credentials_enable_service", false);
        prefs.put("profile.password_manager_enabled", false);
        options.setExperimentalOption("prefs", prefs);// 禁用保存密码提示框
        driver = new ChromeDriver(options);
        //设置寻找一个元素的时间
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    @Test(priority = 1)
    public void  view() throws InterruptedException {
        driver.get("https://www.baidu.com/");
        //得到浏览器的标题
        System.out.println(driver.getTitle());
        Thread.sleep(1000);
    }

    @Test(priority = 2)
    public void  search() throws InterruptedException {

        new WebDriverWait(driver, new Long(5))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"kw\"]")))
                .sendKeys("如加网络");

        new WebDriverWait(driver, new Long(5))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"su\"]")))
                .click();

        driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a")).click();
    }

}

新建TestNG.xml并配置测试

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="百度搜索">
    <test name="搜索业务">
        <classes>
            <class name="cn.myframe.TestBaidu"></class>
        </classes>
    </test>
</suite>

运行程序

运行TestNG.xml

1557738282129

结果:

1557738381306

猜你喜欢

转载自blog.csdn.net/cowbin2012/article/details/90200272