分离验证码识别事件;添加文本处理断言事件;修改js事件问题
This commit is contained in:
parent
be80eb20e3
commit
c977a366e1
|
@ -1,5 +1,7 @@
|
|||
package pres.auxiliary.work.selenium.event;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.openqa.selenium.NoSuchElementException;
|
||||
import org.openqa.selenium.TimeoutException;
|
||||
|
||||
|
@ -99,10 +101,13 @@ public class AssertEvent extends AbstractEvent {
|
|||
*/
|
||||
public boolean assertTextContainKey(Element element, boolean isJudgeAllKey, String... keys) {
|
||||
//判断是否传入关键词
|
||||
boolean result = true;
|
||||
if (keys != null && keys.length != 0) {
|
||||
result = judgetText(textEvent.getText(element), isJudgeAllKey, false, keys);
|
||||
}
|
||||
// boolean result = true;
|
||||
// if (keys != null && keys.length != 0) {
|
||||
// result = judgetText(textEvent.getText(element), isJudgeAllKey, false, keys);
|
||||
// }
|
||||
|
||||
boolean result = assertTextContainKey(element, (e) -> textEvent.getText(e),
|
||||
isJudgeAllKey, keys);
|
||||
|
||||
logText = "断言“" + element.getElementData().getName() + "”元素的文本内容包含"
|
||||
+ (isJudgeAllKey ? "所有" : "部分") + "关键词" + arrayToString(keys);
|
||||
|
@ -155,10 +160,13 @@ public class AssertEvent extends AbstractEvent {
|
|||
*/
|
||||
public boolean assertTextNotContainKey(Element element, boolean isJudgeAllKey, String... keys) {
|
||||
//判断是否传入关键词
|
||||
boolean result = true;
|
||||
if (keys != null && keys.length != 0) {
|
||||
result = judgetText(textEvent.getText(element), isJudgeAllKey, true, keys);
|
||||
}
|
||||
// boolean result = true;
|
||||
// if (keys != null && keys.length != 0) {
|
||||
// result = judgetText(textEvent.getText(element), isJudgeAllKey, true, keys);
|
||||
// }
|
||||
|
||||
boolean result = assertTextNotContainKey(element, (e) -> textEvent.getText(e),
|
||||
isJudgeAllKey, keys);
|
||||
|
||||
logText = "断言“" + element.getElementData().getName() + "”元素的文本内容不包含"
|
||||
+ (isJudgeAllKey ? "所有" : "部分") + "关键词" + arrayToString(keys);
|
||||
|
@ -182,10 +190,12 @@ public class AssertEvent extends AbstractEvent {
|
|||
public boolean assertAttributeContainKey(Element element, String attributeName, boolean isJudgeAllKey,
|
||||
String... keys) {
|
||||
//判断是否传入关键词
|
||||
boolean result = true;
|
||||
if (keys != null && keys.length != 0) {
|
||||
result = judgetText(textEvent.getAttributeValue(element, attributeName), isJudgeAllKey, false, keys);
|
||||
}
|
||||
// boolean result = true;
|
||||
// if (keys != null && keys.length != 0) {
|
||||
// result = judgetText(textEvent.getAttributeValue(element, attributeName), isJudgeAllKey, false, keys);
|
||||
// }
|
||||
boolean result = assertTextContainKey(element, (e) -> textEvent.getAttributeValue(e, attributeName),
|
||||
isJudgeAllKey, keys);
|
||||
|
||||
logText = "断言“" + element.getElementData().getName() + "”元素的“" + attributeName
|
||||
+ "”属性值包含"
|
||||
|
@ -209,11 +219,14 @@ public class AssertEvent extends AbstractEvent {
|
|||
*/
|
||||
public boolean assertAttributeNotContainKey(Element element, String attributeName, boolean isJudgeAllKey,
|
||||
String... keys) {
|
||||
//判断是否传入关键词
|
||||
boolean result = true;
|
||||
if (keys != null && keys.length != 0) {
|
||||
result = judgetText(textEvent.getAttributeValue(element, attributeName), isJudgeAllKey, true, keys);
|
||||
}
|
||||
// //判断是否传入关键词
|
||||
// boolean result = true;
|
||||
// if (keys != null && keys.length != 0) {
|
||||
// result = judgetText(textEvent.getAttributeValue(element, attributeName), isJudgeAllKey, true, keys);
|
||||
// }
|
||||
|
||||
boolean result = assertTextNotContainKey(element, (e) -> textEvent.getAttributeValue(e, attributeName),
|
||||
isJudgeAllKey, keys);
|
||||
|
||||
logText = "断言“" + element.getElementData().getName() + "”元素的“" + attributeName
|
||||
+ "”属性值不包含"
|
||||
|
@ -223,6 +236,64 @@ public class AssertEvent extends AbstractEvent {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对元素内容进行处理,并断言处理后的文本内容中包含指定的关键词。
|
||||
* 可设置元素的文本内容是否需要判断传入的所有关键词,具体参数介绍
|
||||
* 可参考{@link #assertTextContainKey(Element, boolean, String...)}方法
|
||||
*
|
||||
* @param element {@link Element}对象
|
||||
* @param dispose 对元素内容的处理方法,传入的参数为{@link Element}对象,返回的参数为字符串
|
||||
* @param isJudgeAllKey 是否需要完全判断所有关键词
|
||||
* @param keys 关键词组
|
||||
* @return 断言结果
|
||||
* @throws TimeoutException 元素无法操作时抛出的异常
|
||||
* @throws NoSuchElementException 元素不存在或下标不正确时抛出的异常
|
||||
*/
|
||||
public boolean assertTextContainKey(Element element, Function<Element, String> dispose, boolean isJudgeAllKey, String... keys) {
|
||||
//判断是否传入关键词
|
||||
boolean result = true;
|
||||
String text = "";
|
||||
if (keys != null && keys.length != 0) {
|
||||
text = dispose.apply(element);
|
||||
result = judgetText(text, isJudgeAllKey, false, keys);
|
||||
}
|
||||
|
||||
logText = "断言“" + element.getElementData().getName() + "”元素处理后的文本内容(" + text +")包含"
|
||||
+ (isJudgeAllKey ? "所有" : "部分") + "关键词" + arrayToString(keys);
|
||||
resultText = String.valueOf(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对元素内容进行处理,并断言处理后的文本内容中不包含指定的关键词。
|
||||
* 可设置元素的文本内容是否需要判断传入的所有关键词,
|
||||
* 具体参数介绍可参考{@link #assertTextContainKey(Element, boolean, String...)}方法
|
||||
*
|
||||
* @param element {@link Element}对象
|
||||
* @param dispose 对元素内容的处理方法,传入的参数为{@link Element}对象,返回的参数为字符串
|
||||
* @param isJudgeAllKey 是否需要完全判断所有关键词
|
||||
* @param keys 关键词组
|
||||
* @return 断言结果
|
||||
* @throws TimeoutException 元素无法操作时抛出的异常
|
||||
* @throws NoSuchElementException 元素不存在或下标不正确时抛出的异常
|
||||
*/
|
||||
public boolean assertTextNotContainKey(Element element, Function<Element, String> dispose, boolean isJudgeAllKey, String... keys) {
|
||||
//判断是否传入关键词
|
||||
boolean result = true;
|
||||
String text = "";
|
||||
if (keys != null && keys.length != 0) {
|
||||
text = dispose.apply(element);
|
||||
result = judgetText(text, isJudgeAllKey, true, keys);
|
||||
}
|
||||
|
||||
logText = "断言“" + element.getElementData().getName() + "”元素处理后的文本内容(" + text +")包含"
|
||||
+ (isJudgeAllKey ? "所有" : "部分") + "关键词" + arrayToString(keys);
|
||||
resultText = String.valueOf(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于断言元素的内容与预期的文本一致
|
||||
* @param element {@link Element}对象
|
||||
|
@ -235,7 +306,8 @@ public class AssertEvent extends AbstractEvent {
|
|||
//判断是否传入关键词
|
||||
text = text == null ? "" : text;
|
||||
|
||||
boolean result = judgetText(textEvent.getText(element), true, false, text);
|
||||
// boolean result = judgetText(textEvent.getText(element), true, false, text);
|
||||
boolean result = assertTextContainKey(element, true, text);
|
||||
|
||||
logText = "断言“" + element.getElementData().getName() + "”元素的文本内容为“" + text + "”";
|
||||
resultText = String.valueOf(result);
|
||||
|
@ -255,7 +327,8 @@ public class AssertEvent extends AbstractEvent {
|
|||
//判断是否传入关键词
|
||||
text = text == null ? "" : text;
|
||||
|
||||
boolean result = judgetText(textEvent.getText(element), true, true, text);
|
||||
// boolean result = judgetText(textEvent.getText(element), true, true, text);
|
||||
boolean result = assertTextNotContainKey(element, true, text);
|
||||
|
||||
logText = "断言“" + element.getElementData().getName() + "”元素的文本内容不为“" + text + "”";
|
||||
resultText = String.valueOf(result);
|
||||
|
|
|
@ -47,6 +47,7 @@ public class JsEvent extends AbstractEvent {
|
|||
*/
|
||||
public JsEvent(AbstractBrower brower) {
|
||||
super(brower);
|
||||
js = (JavascriptExecutor) brower.getDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,7 +3,6 @@ package pres.auxiliary.work.selenium.event;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import org.openqa.selenium.Keys;
|
||||
import org.openqa.selenium.NoSuchElementException;
|
||||
|
@ -160,7 +159,9 @@ public class TextEvent extends AbstractEvent {
|
|||
* @return 输入的内容
|
||||
* @throws TimeoutException 元素无法操作时抛出的异常
|
||||
* @throws NoSuchElementException 元素不存在或下标不正确时抛出的异常
|
||||
* @deprecated 下个版本删除该方法,改为使用{@link #getImageText(Element)}与{@link #input(Element, String)}方法结合代替
|
||||
*/
|
||||
@Deprecated
|
||||
public String codeInput(Element textElement, Element codeImageElement) {
|
||||
actionOperate(codeImageElement, null);
|
||||
// 判断验证码信息是否加载,加载后,获取其Rectang对象
|
||||
|
@ -193,56 +194,37 @@ public class TextEvent extends AbstractEvent {
|
|||
}
|
||||
|
||||
/**
|
||||
* 该方法用于将一个指定的整数随机填写到传入的控件组中<br>
|
||||
* <b><i>建议在指定值远远大于控件的数量时再使用该方法,否则将会出现不可预期的问题</i></b>
|
||||
*
|
||||
* @param num 指定的整数
|
||||
* @param textElements 通过查找页面得到的一组控件元素对象
|
||||
* @return 由于涉及到多个文本框,故其返回值有多个,将以“值1,值2,值3...”的形式进行返回
|
||||
* @deprecated 当前方法有些BUG,请勿调用,下个版本修复
|
||||
* @throws TimeoutException 元素无法操作时抛出的异常
|
||||
* @throws NoSuchElementException 元素不存在或下标不正确时抛出的异常
|
||||
* 用于对“数字+英文”类型的图片内容进行识别的方法,根据图片元素位置,识别图片中的内容。
|
||||
* 注意,该方法识别成功的概率不高,在“数字+英文”的模式下,经常将数字识别为英文。
|
||||
* @param element 图片元素
|
||||
* @return 识别图片的结果
|
||||
*/
|
||||
@Deprecated
|
||||
public String avgIntergeInput(int num, Element... elements) {
|
||||
//TODO 存在BUG,需要修改
|
||||
//定义存储控件数量及需要随机的数量
|
||||
int contrlNum = elements.length;
|
||||
String inputNumText = "";
|
||||
String[] inputNum = new String[contrlNum];
|
||||
|
||||
// 向下取整获得平均数
|
||||
int avgNum = num / contrlNum;
|
||||
// 向上取整获得差值
|
||||
int diffNum = (int) Math.ceil(avgNum / 10.0);
|
||||
|
||||
//求取通过随机得出的值之和,用于计算最终其随机值之和与实际值的差值
|
||||
int sum = 0;
|
||||
//循环,生成控件个数个随机值,其随机值在给定数值的均值之前
|
||||
for (int i = 0; i < contrlNum; i++) {
|
||||
//注意:2 * diffNum为以下算式的简写:
|
||||
//minNum = avgNum - diffNum;
|
||||
//maxNum = avgNum + diffNum;
|
||||
//int ranNum = new Random().nextInt(maxNum - minNum + 1) + minNum;
|
||||
int ranNum = new Random().nextInt(2 * diffNum + 1) + (avgNum - diffNum);
|
||||
sum += ranNum;
|
||||
inputNum[i] = String.valueOf(ranNum);
|
||||
public String getImageText(Element element) {
|
||||
actionOperate(element, null);
|
||||
// 判断验证码信息是否加载,加载后,获取其Rectang对象
|
||||
Rectangle r = webElement.getRect();
|
||||
// 构造截图对象,并创建截图
|
||||
Screenshot sc = new Screenshot(brower.getDriver(), new File("Temp"));
|
||||
File image = null;
|
||||
try {
|
||||
image = sc.creatImage("code");
|
||||
} catch (WebDriverException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//由于数值是随机的,可能会出现随机值相加不为指定值,故需要补上差值,但由于差值通过算法后不会很大,故可随机附加到一个控件值上
|
||||
if ( (diffNum = sum - num) != 0 ) {
|
||||
inputNum[new Random().nextInt(contrlNum)] = String.valueOf(Integer.valueOf(inputNum[new Random().nextInt(contrlNum)]) - diffNum);
|
||||
}
|
||||
// 设置图片识别的语言包存放位置
|
||||
RecognitionImage.setTessdataPath(new File(TESSDATA));
|
||||
// 识别图片,并存储至结果中
|
||||
resultText = RecognitionImage.judgeImage(image, r.x, r.y, r.width, r.height);
|
||||
|
||||
//将随机值填写至控件中
|
||||
for (int i = 0; i < contrlNum; i++) {
|
||||
inputNumText += (input(elements[i], inputNum[i]) + ",");
|
||||
}
|
||||
logText = "识别“" + element.getElementData().getName() + "”图片中内容,内容为:"
|
||||
+ resultText;
|
||||
|
||||
return inputNumText.substring(0, inputNumText.length() - 1);
|
||||
return resultText;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 用于向控件中上传指定的文件。
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue