提交 4d0614fa authored 作者: 黄承天's avatar 黄承天

[feature]

增加等待元素机制
上级 2142b055
...@@ -15,7 +15,6 @@ import lombok.extern.slf4j.Slf4j; ...@@ -15,7 +15,6 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings; import org.apache.logging.log4j.util.Strings;
import org.openqa.selenium.*; import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -35,7 +34,7 @@ import static java.util.Objects.nonNull; ...@@ -35,7 +34,7 @@ import static java.util.Objects.nonNull;
/** /**
* @author C * @author C
*/ */
@SuppressWarnings({"SpringAutowiredFieldsWarningInspection", "Duplicates", "SpringJavaAutowiredFieldsWarningInspection"}) @SuppressWarnings({"SpringAutowiredFieldsWarningInspection", "Duplicates", "SpringJavaAutowiredFieldsWarningInspection", "DuplicateExpressions"})
@Slf4j @Slf4j
@Service @Service
public class SeleniumExecutor { public class SeleniumExecutor {
...@@ -240,10 +239,8 @@ public class SeleniumExecutor { ...@@ -240,10 +239,8 @@ public class SeleniumExecutor {
break; break;
case CLICK: case CLICK:
content = format("单击:[%s]", target); content = format("单击:[%s]", target);
element = locateElement(target); waitForELement(target,targets);
if (isNull(element)) { element = getElement(target, targets);
element = getElement(targets);
}
costTime = System.currentTimeMillis() - stepStartTime; costTime = System.currentTimeMillis() - stepStartTime;
if (nonNull(element)) { if (nonNull(element)) {
//如果该点击打开了新标签页 则通过对比当前与历史找出新的标签页句柄并存入Map //如果该点击打开了新标签页 则通过对比当前与历史找出新的标签页句柄并存入Map
...@@ -266,10 +263,8 @@ public class SeleniumExecutor { ...@@ -266,10 +263,8 @@ public class SeleniumExecutor {
break; break;
case DOUBLE_CLICK: case DOUBLE_CLICK:
content = format("双击:[%s]", target); content = format("双击:[%s]", target);
element = locateElement(target); waitForELement(target,targets);
if (isNull(element)) { element = getElement(target, targets);
element = getElement(targets);
}
costTime = System.currentTimeMillis() - stepStartTime; costTime = System.currentTimeMillis() - stepStartTime;
if (nonNull(element)) { if (nonNull(element)) {
//如果该双击打开了新标签页 则通过对比当前与历史找出新的标签页句柄并存入Map //如果该双击打开了新标签页 则通过对比当前与历史找出新的标签页句柄并存入Map
...@@ -290,10 +285,8 @@ public class SeleniumExecutor { ...@@ -290,10 +285,8 @@ public class SeleniumExecutor {
break; break;
case MOUSE_DOWN: case MOUSE_DOWN:
content = format("长单击:[%s]", target); content = format("长单击:[%s]", target);
element = locateElement(target); waitForELement(target,targets);
if (isNull(element)) { element = getElement(target, targets);
element = getElement(targets);
}
costTime = System.currentTimeMillis() - stepStartTime; costTime = System.currentTimeMillis() - stepStartTime;
if (nonNull(element)) { if (nonNull(element)) {
boolean newWindow = nonNull(command.getOpensWindow()) && command.getOpensWindow(); boolean newWindow = nonNull(command.getOpensWindow()) && command.getOpensWindow();
...@@ -314,10 +307,8 @@ public class SeleniumExecutor { ...@@ -314,10 +307,8 @@ public class SeleniumExecutor {
break; break;
case MOUSE_MOVE: case MOUSE_MOVE:
content = format("鼠标移动至:[%s]", target); content = format("鼠标移动至:[%s]", target);
element = locateElement(target); waitForELement(target,targets);
if (isNull(element)) { element = getElement(target, targets);
element = getElement(targets);
}
costTime = System.currentTimeMillis() - stepStartTime; costTime = System.currentTimeMillis() - stepStartTime;
if (nonNull(element)) { if (nonNull(element)) {
boolean newWindow = nonNull(command.getOpensWindow()) && command.getOpensWindow(); boolean newWindow = nonNull(command.getOpensWindow()) && command.getOpensWindow();
...@@ -336,10 +327,8 @@ public class SeleniumExecutor { ...@@ -336,10 +327,8 @@ public class SeleniumExecutor {
break; break;
case MOUSE_UP: case MOUSE_UP:
content = format("停止长按:[%s]", target); content = format("停止长按:[%s]", target);
element = locateElement(target); waitForELement(target,targets);
if (isNull(element)) { element = getElement(target, targets);
element = getElement(targets);
}
costTime = System.currentTimeMillis() - stepStartTime; costTime = System.currentTimeMillis() - stepStartTime;
if (nonNull(element)) { if (nonNull(element)) {
boolean newWindow = nonNull(command.getOpensWindow()) && command.getOpensWindow(); boolean newWindow = nonNull(command.getOpensWindow()) && command.getOpensWindow();
...@@ -360,7 +349,8 @@ public class SeleniumExecutor { ...@@ -360,7 +349,8 @@ public class SeleniumExecutor {
case SELECT: case SELECT:
String label = seleniumHelper.getValue(value); String label = seleniumHelper.getValue(value);
content = format("下拉框选择:[%s]", label); content = format("下拉框选择:[%s]", label);
element = locateElement(target); waitForELement(target,targets);
element = getElement(target, targets);
costTime = System.currentTimeMillis() - stepStartTime; costTime = System.currentTimeMillis() - stepStartTime;
if (nonNull(element)) { if (nonNull(element)) {
movetoElement(element); movetoElement(element);
...@@ -374,14 +364,10 @@ public class SeleniumExecutor { ...@@ -374,14 +364,10 @@ public class SeleniumExecutor {
break; break;
case MOUSE_OVER: case MOUSE_OVER:
content = format("鼠标悬停:[%s]", target); content = format("鼠标悬停:[%s]", target);
element = locateElement(target); element = getElement(target, targets);
costTime = System.currentTimeMillis() - stepStartTime; costTime = System.currentTimeMillis() - stepStartTime;
if (isNull(element)) {
element = getElement(targets);
costTime = System.currentTimeMillis() - stepStartTime;
}
if (nonNull(element)) { if (nonNull(element)) {
new Actions(driver).moveToElement(element).perform(); movetoElement(element);
success = true; success = true;
} else { } else {
message = "无法定位该元素"; message = "无法定位该元素";
...@@ -390,10 +376,7 @@ public class SeleniumExecutor { ...@@ -390,10 +376,7 @@ public class SeleniumExecutor {
break; break;
case TYPE: case TYPE:
content = format("输入内容:[%s]", value); content = format("输入内容:[%s]", value);
element = locateElement(target); element = getElement(target, targets);
if (isNull(element)) {
element = getElement(targets);
}
costTime = System.currentTimeMillis() - stepStartTime; costTime = System.currentTimeMillis() - stepStartTime;
if (nonNull(element)) { if (nonNull(element)) {
String elementType = element.getAttribute("type"); String elementType = element.getAttribute("type");
...@@ -455,24 +438,35 @@ public class SeleniumExecutor { ...@@ -455,24 +438,35 @@ public class SeleniumExecutor {
} }
} }
private void movetoElement(WebElement element) { private void movetoElement(WebElement element) {
JavascriptExecutor jse = (JavascriptExecutor) driver; JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView(true);", element); jse.executeScript("arguments[0].scrollIntoView(true);", element);
} }
private void waitForELement(String target, List<List<String>> targets){
try {
new WebDriverWait(driver,3).until(webDriver -> nonNull(getElement(target,targets)));
} catch (Exception ignored){
}
}
/** /**
* 根据一组target数据定位元素 * 根据一组target数据定位元素
* 只要其中一对定位到 * 只要其中一对定位到
* 就返回 * 就返回
*/ */
private WebElement getElement(List<List<String>> targets) { private WebElement getElement(String target, List<List<String>> targets) {
WebElement element; WebElement element = locateElement(target);
for (List<String> target : targets) { if (isNull(element)) {
element = locateElement(target); for (List<String> atarget : targets) {
element = locateElement(atarget);
if (nonNull(element)) { if (nonNull(element)) {
return element; return element;
} }
} }
}
return null; return null;
} }
...@@ -487,25 +481,19 @@ public class SeleniumExecutor { ...@@ -487,25 +481,19 @@ public class SeleniumExecutor {
String content = target.get(0); String content = target.get(0);
String value = getValue(content); String value = getValue(content);
try { try {
WebDriverWait webDriverWait = new WebDriverWait(driver,3);
switch (type) { switch (type) {
case CSS: case CSS:
webDriverWait.until(webDriver -> nonNull(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(value)).apply(webDriver)));
return driver.findElement(By.cssSelector(value)); return driver.findElement(By.cssSelector(value));
case ID: case ID:
webDriverWait.until(webDriver -> nonNull(ExpectedConditions.visibilityOfElementLocated(By.id(value)).apply(webDriver)));
return driver.findElement(By.id(value)); return driver.findElement(By.id(value));
case NAME: case NAME:
webDriverWait.until(webDriver -> nonNull(ExpectedConditions.visibilityOfElementLocated(By.name(value)).apply(webDriver)));
return driver.findElement(By.name(value)); return driver.findElement(By.name(value));
case LINK_TEXT: case LINK_TEXT:
webDriverWait.until(webDriver -> nonNull(ExpectedConditions.visibilityOfElementLocated(By.linkText(value)).apply(webDriver)));
return driver.findElement(By.linkText(value)); return driver.findElement(By.linkText(value));
case XPATH_HREF: case XPATH_HREF:
case XPATH_ID_RELATIVE: case XPATH_ID_RELATIVE:
case XPATH_POSITION: case XPATH_POSITION:
case XPATH_INNER_TEXT: case XPATH_INNER_TEXT:
webDriverWait.until(webDriver -> nonNull(ExpectedConditions.visibilityOfElementLocated(By.xpath(value)).apply(webDriver)));
return driver.findElement(By.xpath(value)); return driver.findElement(By.xpath(value));
default: default:
} }
...@@ -520,16 +508,12 @@ public class SeleniumExecutor { ...@@ -520,16 +508,12 @@ public class SeleniumExecutor {
String type = type_value[0]; String type = type_value[0];
String value = type_value[1]; String value = type_value[1];
try { try {
WebDriverWait webDriverWait = new WebDriverWait(driver,3);
switch (type) { switch (type) {
case "name": case "name":
webDriverWait.until(webDriver -> nonNull(ExpectedConditions.visibilityOfElementLocated(By.name(value)).apply(webDriver)));
return driver.findElement(By.name(value)); return driver.findElement(By.name(value));
case "css": case "css":
webDriverWait.until(webDriver -> nonNull(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(value)).apply(webDriver)));
return driver.findElement(By.cssSelector(value)); return driver.findElement(By.cssSelector(value));
case "id": case "id":
webDriverWait.until(webDriver -> nonNull(ExpectedConditions.visibilityOfElementLocated(By.id(value)).apply(webDriver)));
return driver.findElement(By.id(value)); return driver.findElement(By.id(value));
default: default:
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论