欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java+Selenium自动化测试(二)

程序员文章站 2023-03-26 16:26:00
Java+Selenium+TestNG自动化测试框架整合 1、简化代码 封装一个定位元素的类,类型为ElementLocation package com.test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriv ......

java+selenium+testng自动化测试框架整合

1、简化代码

封装一个定位元素的类,类型为elementlocation 

package com.test;
import org.openqa.selenium.by;
import org.openqa.selenium.webdriver;
import java.util.concurrent.timeunit;
/**
 * 每次定位元素都调用相同的方法
 * 将每个方法都封装起来仅供调用
 */
public class elementlocation {
    /**
     * 通过id定位元素
     * @param id
     * @param elementid
     */
    public  void finelementbyid(string id, string elementid, webdriver driver){
         driver.manage().timeouts().implicitlywait(10, timeunit.seconds);
         driver.findelement(by.byid.id(id)).sendkeys(elementid);
    }
    /**
     * 通过xpath定位元素
     * @param xpath
     * @param text
     */
    public void findelementbyxpath(string xpath,string text,webdriver driver){
        driver.manage().timeouts().implicitlywait(10, timeunit.seconds);
        driver.findelement(by.byxpath.xpath(xpath)).sendkeys(text);
    }
/** * 先清除文本框内容再通过id定位元素 * @param id * @param text * @param text */ public void findelementbyidclearsendkeys(string id,string text,webdriver driver){ driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.findelement(by.byid.id(id)).clear(); driver.findelement(by.byid.id(id)).sendkeys(text); } /** * 先清除文本框内容再通过xpath定位元素 * @param xpath * @param text */ public void findelementbyxpathclearsendkeys(string xpath,string text,webdriver driver){ driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.findelement(by.byxpath.xpath(xpath)).clear(); driver.findelement(by.byxpath.xpath(xpath)).sendkeys(text); } /** * 先清除文本框内容再通过cssselector定位元素 * @param cssselector * @param text */ public void findelementbycssclearsendkeys(string cssselector,string text,webdriver driver){ driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.findelement(by.bycssselector.cssselector(cssselector)).clear(); driver.findelement(by.bycssselector.cssselector(cssselector)).sendkeys(text); } /** * cssselector点击事件 * @param cssselector */ public void findelementbycssclick(string cssselector,webdriver driver){ driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.findelement(by.bycssselector.cssselector(cssselector)).click(); } /** * xpath点击事件 * @param xpath */ public void findelementbyxpathclick(string xpath,webdriver driver){ driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.findelement(by.byxpath.xpath(xpath)).click(); } }

2、整合testng测试框架

  1、java整合testng这里用的jar包,先下载一个jar包;

  2、将jar包导入工程里面(这里导入的是idea)

整合部分以登录为例:

package com.test;

import org.openqa.selenium.webdriver;
import org.openqa.selenium.chrome.chromedriver;
import org.openqa.selenium.support.ui.webdriverwait;
import org.testng.annotations.aftermethod;
import org.testng.annotations.beforemethod;
import org.testng.annotations.test;

public class login  {
    webdriver driver = null;
    //调用上面定位元素的类
    elementlocation elementlocation =  new elementlocation();
    //在所有方法运行之前运行
    @beforemethod
    public void before(){
        system.setproperty("webdriver.chrome.driver", "e:\\selenium\\chromedriver.exe");
        driver = new chromedriver();
        string url = "";
        driver.manage().window().maximize();
        driver.get(url);
    }
    /**
     * 定位登录界面元素
     * 1.输入正确手机号码
     * 2.输入正确密码
     * 3.登录成功
     */
    @test
    public void test_login1(){
        elementlocation.findelementbycssclearsendkeys("input[type='text']","11115232323",driver);
        elementlocation.findelementbycssclearsendkeys("input[type='password']","111111",driver);
        elementlocation.findelementbycssclick("button[type='button']",driver);
        system.out.println("登录成功,跳转到首页");
    }//在所有方法运行完之后运行
    @aftermethod
    public void after(){
        try{
            thread thread = new thread();
            thread.sleep(5000);
        driver.quit();  }catch (interruptedexception e){ e.printstacktrace(); } } }

注:

1、提供调用定位元素的方法,还是有些复杂,在后面继续用的时候再进行优化。

2、整合testng  

  1、选用jar包比较方便,现在很多都不用jar包文件直接整合maven中,后面继续优化。
  2、testng提供了很多注解,目前只用了三个。
  @beforemethod:在运行其他方法之前运行该方法,仅运行一次
  @aftermethod:在运行完其他方法之后运行该方法,仅运行一次
  @test:运行当前方法