zoukankan      html  css  js  c++  java
  • Selenium2(java)TestNG的使用 七

    TestNG,即Testing Next Generation,下一代测试技术,是一套根据JUnit和NUnit思想而构建的利用注释来强化测试功能的一个测试框架,即可以用来做单元测试,也可以用来做集成测试。

    安装:Help-->Install New Software

    点击Add,在弹出的对话框输入:

    clipboard

    点击OK,一路安装即可

    TestNG与selenium结合使用

    1. 新建Java项目selenium_testng_test
    2. 导入selenium和testng类库:项目右键-->Build Path-->Add Libraries分别添加selenium和testng类库
    3. 在src目录下新建一个包名:com.testng.test
    4. 新建一个TestNG测试类,命名NewTest

    clipboard[1]

    实现代码:

    package com.testng.test;
    
    import org.testng.annotations.Test;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.AfterTest;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.TimeoutException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.Assert;
    
    public class NewTest {
        //声明一个全局变量driver
        private WebDriver driver;
        @Test
        public void f() {
            By inputBox = By.id("kw");
            By searchButton = By.id("su");
            intelligenWait(driver, 10, inputBox);
            intelligenWait(driver, 10, searchButton);
            driver.findElement(inputBox).sendKeys("JAVA");
            driver.findElement(searchButton).click();
            
            try {
                Thread.sleep(2000);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        
        @BeforeTest
        public void beforeTest() {
            driver = new FirefoxDriver();
            driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            driver.get("http://www.baidu.com");
        }
    
        @AfterTest
        public void afterTest() {
            driver.quit();
        }
        
        /**这是智能等待元素加载的方法**/
        public void intelligenWait(WebDriver driver, int timeOut, final By by) {
            try {
                (new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>(){
                    public Boolean apply(WebDriver driver) {
                        WebElement element = driver.findElement(by);
                        return element.isDisplayed();
                    }
                });
            } catch (TimeoutException e) {
                Assert.fail("超时!" + timeOut + "秒之后还没有找到元素[" + by + "]", e);
            }
        }
    }
  • 相关阅读:
    第15.26节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QListWidget详解
    第二十一章、 Model/View便利类列表部件QListWidget详解
    PyQt(Python+Qt)学习随笔:QListWidget的信号简介
    iis日志存放位置 及 查看方法
    如何查看和分析IIS日志
    IIS网站设置禁止IP访问设置方法
    修改php默认的FastCGI模式为ISAPI模式的方法
    VPS/云主机CPU占用100%故障排查
    APACHE服务器httpd.exe进程占用cpu100%的解决方法
    httpd.exe占用100%CPU
  • 原文地址:https://www.cnblogs.com/sundalian/p/5164618.html
Copyright © 2011-2022 走看看