添加事件等待类,用于等待某个事件后再进行下一个操作时使用,未完善

This commit is contained in:
彭宇琦 2020-05-24 17:01:54 +08:00
parent 0c8c09c856
commit c76487a31b
2 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,50 @@
package pres.auxiliary.work.selenium.event;
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import pres.auxiliary.work.selenium.element.Element;
public class EventWait {
/**
* 控制等待
*/
private WebDriverWait wait;
/**
* 构造WaitEvent类对象默认60秒的查询超时时间可设置超时时间{@link #setOvertime(long)}
* @param driver WebDriver类对象
*/
public EventWait(WebDriver driver) {
this(driver, 60);
}
/**
* 构造WaitEvent类对象并设置等待超时时间单位为秒
* @param driver WebDriver类对象
*/
public EventWait(WebDriver driver, long overtime) {
wait = new WebDriverWait(driver, overtime, 200);
wait.withMessage("元素仍然存在,操作超时:" + overtime + "");
}
/**
* 用于设置元素查询超时时间单位为秒
* @param overtime 超时时间
*/
public void setOvertime(long overtime) {
wait.pollingEvery(Duration.ofSeconds(overtime));
}
/**
* 用于等待元素消失
* @param waitElement 需要等待的元素
*/
public void disappear(Element waitElement) {
wait.until(driver -> {
return !waitElement.getWebElement().isDisplayed();
});
}
}

View File

@ -0,0 +1,86 @@
package pres.auxiliary.work.selenium.event;
import java.io.File;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import pres.auxiliary.work.selenium.brower.ChromeBrower;
import pres.auxiliary.work.selenium.brower.ChromeBrower.ChromeOptionType;
import pres.auxiliary.work.selenium.element.CommonBy;
import pres.auxiliary.work.selenium.element.DataListBy;
/**
* <p><b>文件名</b>EventWaitTest.java</p>
* <p><b>用途</b>
* 用于对{@link EventWait}类进行测试
* </p>
* <p><b>页面</b>
* 运维管理系统岗前答题库模块
* </p>
* <p><b>编码时间</b>2020年5月24日下午4:37:25</p>
* <p><b>修改时间</b>2020年5月24日下午4:37:25</p>
* @author
* @version Ver1.0
* @since JDK 12
*
*/
public class EventWaitTest {
EventWait wait;
ClickEvent c;
TextEvent t;
CommonBy cby;
ChromeBrower cb = new ChromeBrower(new File("Resource/BrowersDriver/Chrom/78.0394.70/chromedriver.exe"));
@BeforeClass
public void init() {
cb.addConfig(ChromeOptionType.CONTRAL_OPEN_BROWER, "127.0.0.1:9222");
cby = new CommonBy(cb);
c = new ClickEvent(cb.getDriver());
t = new TextEvent(cb.getDriver());
wait = new EventWait(cb.getDriver());
}
@AfterClass
public void quit() {
cb.getDriver().quit();
}
/**
* 测试{@link EventWait#disappear(pres.auxiliary.work.selenium.element.Element)}方法
*/
@Test
public void disappearTest() {
//获取编号列
DataListBy element = new DataListBy(cb);
element.add("/html/body/div[1]/div/div/section/div/div[2]/div[3]/table//td[contains(@class,'el-table_1_column_1 ')]/div/span");
System.out.println(element.getMinColumnSize());
System.out.println("开始页:");
//获取并打印编号列
for (int i = 1; i <= element.getMinColumnSize(); i++) {
System.out.println("====================================");
for (String name : element.getNames()) {
System.out.println("编号" + t.getText(element.getElement(name, i)));
}
System.out.println("====================================");
}
//点击搜索按钮使其弹出等待加载的图标
c.click(cby.getElement("//button[@class='btn-next']"));
//等待图标消失
wait.disappear(cby.getElement("//*[@class='circular']"));
System.out.println("下一页:");
//重新再获取并打印编号列
for (int i = 1; i <= element.getMinColumnSize(); i++) {
System.out.println("====================================");
for (String name : element.getNames()) {
System.out.println("编号" + "=" + t.getText(element.getElement(name, i)));
}
System.out.println("====================================");
}
}
}