美文网首页
selenium+testng自动化框架

selenium+testng自动化框架

作者: 寻叶亭 | 来源:发表于2018-07-31 09:24 被阅读73次

前提条件:
1.安装maven
2.eclipse中新建一个maven项目

image.png image.png

3.引入外部依赖,直接在pom.xml文件中添加即可,比如引入testng(用来断言),在http://mvnrepository.com中搜索testng

image.png

然后选择版本后点击

image.png

直接copy

image.png

然后如下图所示加到pom.xml中


image.png

同样方法依次引入selenium(操作页面元素用)、log4j(打印日志用)、commons(截图用)


image.png

4.在eclipse中新建一个基类,封装一个找页面元素的方法

public class BasicPO {
    public WebDriver driver;
    public static String url;
    public static long globaltime = 1;
    long filename = System.currentTimeMillis();//获取当前时间,用以截图命名
    public org.apache.logging.log4j.Logger logger = LogManager.getLogger(ResolverUtil.Test.class.getName());
    public WebElement findelement(By by,long timeout){
        try {
            //显示等待,找到元素了立马执行,未找到等待几秒后抛出异常
            WebElement element = new WebDriverWait(driver, timeout).until(ExpectedConditions.presenceOfElementLocated(by));
            //找到元素了打印info日志
            logger.info("|"+this.getClass().getName()+by.toString()+"对象被访问"+filename);
            return (element);
        } catch (Exception e) {
            Commond.shotscreen(driver, filename);//截图
            //打印error日志,且将截图地址输出
            logger.error("|"+this.getClass().getName()+by.toString()+"对象无法找到"+filename);
            return(null);
            }
        }
    public WebElement findelement(By by){//重载,若未指定等待时间则给默认时间
            try {
                WebElement element = new WebDriverWait(driver, globaltime).until(ExpectedConditions.presenceOfElementLocated(by));
                logger.info("|"+this.getClass().getName()+by.toString()+"对象被访问"+filename);
                return (element);
            } catch (Exception e) {
                Commond.shotscreen(driver, filename);
                logger.error("|"+this.getClass().getName()+by.toString()+"对象无法找到 | 截图文件:"+filename);
                return(null);
            }
    }
}   

5.对百度搜索页面操作进行封装

public class BaiduSearchPO extends BasicPO {
    public static By baidusearchinput = By.id("kw");
    public static By baidusearchbutton = By.id("su");
    public BaiduSearchPO(WebDriver driver) {
        this.driver = driver;
        String url = "http://www.baidu.com";
        driver.navigate().to(url);
    }
    public void baidusearch(String searchstring) throws InterruptedException {
        this.findelement(baidusearchinput).sendKeys(searchstring);
        this.findelement(baidusearchbutton).click();
        Thread.sleep(3000);
        driver.quit();
    }
}

6.接着实现截图方法:

public class Commond {
    public static void shotscreen(WebDriver driver,Long filename) throws IOException {
        File screenshotfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshotfile, new File("d:/log/screen/"+filename+".png"));
    }
}

7.在Eclipse中在线安装TestNG插件
打开Eclipse Help ->Install from Site, 然后Add "http://beust.com/eclipse"

image.png

8.最后就是写用例部分了,新建一个testngclass 其中BeforeMethod、Test和AfterMethod是testng的注解,分别代表初始化、执行主体和结尾

public class BaiduTest {
    @BeforeMethod
    public void setup(){
    }
     WebDriver wd = new ChromeDriver(); 
    @Test
    public  void f() throws InterruptedException
    {
        BaiduSearchPO baiduSearchPO = new BaiduSearchPO(wd);
        baiduSearchPO.baidusearch("www");
        String actual = wd.findElement(By.xpath("//*[@id=\"1\"]/div[1]/div[2]/p[1]/text()[1]")).getText();
        System.out.println(actual);
        Assert.assertTrue(actual.contains("www"));
    }
    @AfterMethod
    public void teardown(){
        wd.quit();
    }
}

9.正常访问到页面元素时打印访问对象名称

image.png

10.找不到页面元素时报错,打印未找到的对象并截图

image.png

相关文章

网友评论

      本文标题:selenium+testng自动化框架

      本文链接:https://www.haomeiwen.com/subject/bphtvftx.html