Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
auto-test-case
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄承天
auto-test-case
Commits
4d0614fa
提交
4d0614fa
authored
1月 06, 2021
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[feature]
增加等待元素机制
上级
2142b055
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
31 行增加
和
47 行删除
+31
-47
SeleniumExecutor.java
...main/java/com/zjty/autotest/service/SeleniumExecutor.java
+31
-47
没有找到文件。
src/main/java/com/zjty/autotest/service/SeleniumExecutor.java
浏览文件 @
4d0614fa
...
...
@@ -15,7 +15,6 @@ import lombok.extern.slf4j.Slf4j;
import
org.apache.logging.log4j.util.Strings
;
import
org.openqa.selenium.*
;
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.WebDriverWait
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -35,7 +34,7 @@ import static java.util.Objects.nonNull;
/**
* @author C
*/
@SuppressWarnings
({
"SpringAutowiredFieldsWarningInspection"
,
"Duplicates"
,
"SpringJavaAutowiredFieldsWarningInspection"
})
@SuppressWarnings
({
"SpringAutowiredFieldsWarningInspection"
,
"Duplicates"
,
"SpringJavaAutowiredFieldsWarningInspection"
,
"DuplicateExpressions"
})
@Slf4j
@Service
public
class
SeleniumExecutor
{
...
...
@@ -240,10 +239,8 @@ public class SeleniumExecutor {
break
;
case
CLICK:
content
=
format
(
"单击:[%s]"
,
target
);
element
=
locateElement
(
target
);
if
(
isNull
(
element
))
{
element
=
getElement
(
targets
);
}
waitForELement
(
target
,
targets
);
element
=
getElement
(
target
,
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
if
(
nonNull
(
element
))
{
//如果该点击打开了新标签页 则通过对比当前与历史找出新的标签页句柄并存入Map
...
...
@@ -266,10 +263,8 @@ public class SeleniumExecutor {
break
;
case
DOUBLE_CLICK:
content
=
format
(
"双击:[%s]"
,
target
);
element
=
locateElement
(
target
);
if
(
isNull
(
element
))
{
element
=
getElement
(
targets
);
}
waitForELement
(
target
,
targets
);
element
=
getElement
(
target
,
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
if
(
nonNull
(
element
))
{
//如果该双击打开了新标签页 则通过对比当前与历史找出新的标签页句柄并存入Map
...
...
@@ -290,10 +285,8 @@ public class SeleniumExecutor {
break
;
case
MOUSE_DOWN:
content
=
format
(
"长单击:[%s]"
,
target
);
element
=
locateElement
(
target
);
if
(
isNull
(
element
))
{
element
=
getElement
(
targets
);
}
waitForELement
(
target
,
targets
);
element
=
getElement
(
target
,
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
if
(
nonNull
(
element
))
{
boolean
newWindow
=
nonNull
(
command
.
getOpensWindow
())
&&
command
.
getOpensWindow
();
...
...
@@ -314,10 +307,8 @@ public class SeleniumExecutor {
break
;
case
MOUSE_MOVE:
content
=
format
(
"鼠标移动至:[%s]"
,
target
);
element
=
locateElement
(
target
);
if
(
isNull
(
element
))
{
element
=
getElement
(
targets
);
}
waitForELement
(
target
,
targets
);
element
=
getElement
(
target
,
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
if
(
nonNull
(
element
))
{
boolean
newWindow
=
nonNull
(
command
.
getOpensWindow
())
&&
command
.
getOpensWindow
();
...
...
@@ -336,10 +327,8 @@ public class SeleniumExecutor {
break
;
case
MOUSE_UP:
content
=
format
(
"停止长按:[%s]"
,
target
);
element
=
locateElement
(
target
);
if
(
isNull
(
element
))
{
element
=
getElement
(
targets
);
}
waitForELement
(
target
,
targets
);
element
=
getElement
(
target
,
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
if
(
nonNull
(
element
))
{
boolean
newWindow
=
nonNull
(
command
.
getOpensWindow
())
&&
command
.
getOpensWindow
();
...
...
@@ -360,7 +349,8 @@ public class SeleniumExecutor {
case
SELECT:
String
label
=
seleniumHelper
.
getValue
(
value
);
content
=
format
(
"下拉框选择:[%s]"
,
label
);
element
=
locateElement
(
target
);
waitForELement
(
target
,
targets
);
element
=
getElement
(
target
,
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
if
(
nonNull
(
element
))
{
movetoElement
(
element
);
...
...
@@ -374,14 +364,10 @@ public class SeleniumExecutor {
break
;
case
MOUSE_OVER:
content
=
format
(
"鼠标悬停:[%s]"
,
target
);
element
=
locateElement
(
target
);
element
=
getElement
(
target
,
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
if
(
isNull
(
element
))
{
element
=
getElement
(
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
}
if
(
nonNull
(
element
))
{
new
Actions
(
driver
).
moveToElement
(
element
).
perform
(
);
movetoElement
(
element
);
success
=
true
;
}
else
{
message
=
"无法定位该元素"
;
...
...
@@ -390,10 +376,7 @@ public class SeleniumExecutor {
break
;
case
TYPE:
content
=
format
(
"输入内容:[%s]"
,
value
);
element
=
locateElement
(
target
);
if
(
isNull
(
element
))
{
element
=
getElement
(
targets
);
}
element
=
getElement
(
target
,
targets
);
costTime
=
System
.
currentTimeMillis
()
-
stepStartTime
;
if
(
nonNull
(
element
))
{
String
elementType
=
element
.
getAttribute
(
"type"
);
...
...
@@ -455,24 +438,35 @@ public class SeleniumExecutor {
}
}
private
void
movetoElement
(
WebElement
element
)
{
JavascriptExecutor
jse
=
(
JavascriptExecutor
)
driver
;
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数据定位元素
* 只要其中一对定位到
* 就返回
*/
private
WebElement
getElement
(
List
<
List
<
String
>>
targets
)
{
WebElement
element
;
for
(
List
<
String
>
target
:
targets
)
{
element
=
locateElement
(
target
);
private
WebElement
getElement
(
String
target
,
List
<
List
<
String
>>
targets
)
{
WebElement
element
=
locateElement
(
target
);
if
(
isNull
(
element
))
{
for
(
List
<
String
>
atarget
:
targets
)
{
element
=
locateElement
(
atarget
);
if
(
nonNull
(
element
))
{
return
element
;
}
}
}
return
null
;
}
...
...
@@ -487,25 +481,19 @@ public class SeleniumExecutor {
String
content
=
target
.
get
(
0
);
String
value
=
getValue
(
content
);
try
{
WebDriverWait
webDriverWait
=
new
WebDriverWait
(
driver
,
3
);
switch
(
type
)
{
case
CSS:
webDriverWait
.
until
(
webDriver
->
nonNull
(
ExpectedConditions
.
visibilityOfElementLocated
(
By
.
cssSelector
(
value
)).
apply
(
webDriver
)));
return
driver
.
findElement
(
By
.
cssSelector
(
value
));
case
ID:
webDriverWait
.
until
(
webDriver
->
nonNull
(
ExpectedConditions
.
visibilityOfElementLocated
(
By
.
id
(
value
)).
apply
(
webDriver
)));
return
driver
.
findElement
(
By
.
id
(
value
));
case
NAME:
webDriverWait
.
until
(
webDriver
->
nonNull
(
ExpectedConditions
.
visibilityOfElementLocated
(
By
.
name
(
value
)).
apply
(
webDriver
)));
return
driver
.
findElement
(
By
.
name
(
value
));
case
LINK_TEXT:
webDriverWait
.
until
(
webDriver
->
nonNull
(
ExpectedConditions
.
visibilityOfElementLocated
(
By
.
linkText
(
value
)).
apply
(
webDriver
)));
return
driver
.
findElement
(
By
.
linkText
(
value
));
case
XPATH_HREF:
case
XPATH_ID_RELATIVE:
case
XPATH_POSITION:
case
XPATH_INNER_TEXT:
webDriverWait
.
until
(
webDriver
->
nonNull
(
ExpectedConditions
.
visibilityOfElementLocated
(
By
.
xpath
(
value
)).
apply
(
webDriver
)));
return
driver
.
findElement
(
By
.
xpath
(
value
));
default
:
}
...
...
@@ -520,16 +508,12 @@ public class SeleniumExecutor {
String
type
=
type_value
[
0
];
String
value
=
type_value
[
1
];
try
{
WebDriverWait
webDriverWait
=
new
WebDriverWait
(
driver
,
3
);
switch
(
type
)
{
case
"name"
:
webDriverWait
.
until
(
webDriver
->
nonNull
(
ExpectedConditions
.
visibilityOfElementLocated
(
By
.
name
(
value
)).
apply
(
webDriver
)));
return
driver
.
findElement
(
By
.
name
(
value
));
case
"css"
:
webDriverWait
.
until
(
webDriver
->
nonNull
(
ExpectedConditions
.
visibilityOfElementLocated
(
By
.
cssSelector
(
value
)).
apply
(
webDriver
)));
return
driver
.
findElement
(
By
.
cssSelector
(
value
));
case
"id"
:
webDriverWait
.
until
(
webDriver
->
nonNull
(
ExpectedConditions
.
visibilityOfElementLocated
(
By
.
id
(
value
)).
apply
(
webDriver
)));
return
driver
.
findElement
(
By
.
id
(
value
));
default
:
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论