Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
auto-test
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄承天
auto-test
Commits
7ed062e8
提交
7ed062e8
authored
3月 31, 2020
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修正操作过程中元素过期问题
上级
d8919640
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
60 行增加
和
49 行删除
+60
-49
SeleniumExecutor.java
...java/com/zjty/autotest/service/impl/SeleniumExecutor.java
+60
-49
没有找到文件。
src/main/java/com/zjty/autotest/service/impl/SeleniumExecutor.java
浏览文件 @
7ed062e8
...
@@ -8,6 +8,7 @@ import com.zjty.autotest.common.action.LabelType;
...
@@ -8,6 +8,7 @@ import com.zjty.autotest.common.action.LabelType;
import
com.zjty.autotest.pojo.report.ElementDetail
;
import
com.zjty.autotest.pojo.report.ElementDetail
;
import
com.zjty.autotest.pojo.report.Measure
;
import
com.zjty.autotest.pojo.report.Measure
;
import
com.zjty.autotest.pojo.report.Report
;
import
com.zjty.autotest.pojo.report.Report
;
import
com.zjty.autotest.pojo.test.ActResult
;
import
com.zjty.autotest.pojo.test.Input
;
import
com.zjty.autotest.pojo.test.Input
;
import
com.zjty.autotest.pojo.test.Project
;
import
com.zjty.autotest.pojo.test.Project
;
import
com.zjty.autotest.util.FileUtil
;
import
com.zjty.autotest.util.FileUtil
;
...
@@ -139,7 +140,7 @@ public class SeleniumExecutor {
...
@@ -139,7 +140,7 @@ public class SeleniumExecutor {
currentHistoryAttributes
.
clear
();
currentHistoryAttributes
.
clear
();
//开始遍历操作元素队列中的元素 返回各个元素测试信息
//开始遍历操作元素队列中的元素 返回各个元素测试信息
List
<
ElementDetail
>
elementDetails
=
Lists
.
newArrayList
();
List
<
ElementDetail
>
elementDetails
=
Lists
.
newArrayList
();
traversal
(
element
s
,
element
Details
);
traversal
(
elementDetails
);
//如果全部元素通过则是通过
//如果全部元素通过则是通过
Boolean
success
=
elementDetails
.
stream
().
allMatch
(
ElementDetail:
:
getSuccess
);
Boolean
success
=
elementDetails
.
stream
().
allMatch
(
ElementDetail:
:
getSuccess
);
//如果出错 则提供截图
//如果出错 则提供截图
...
@@ -172,43 +173,37 @@ public class SeleniumExecutor {
...
@@ -172,43 +173,37 @@ public class SeleniumExecutor {
return
measure
;
return
measure
;
}
}
private
void
traversal
(
Map
<
Attributes
,
WebElement
>
elements
,
List
<
ElementDetail
>
elementDetails
)
{
private
void
traversal
(
List
<
ElementDetail
>
elementDetails
)
{
for
(
Attributes
attributes
:
elements
.
keySet
())
{
for
(
Attributes
attributes
:
elements
.
keySet
())
{
WebElement
element
=
elements
.
get
(
attributes
);
WebElement
element
=
elements
.
get
(
attributes
);
if
(!
currentHistoryAttributes
.
contains
(
attributes
)
&&
isInputAble
(
element
))
{
if
(!
currentHistoryAttributes
.
contains
(
attributes
)
&&
isInputAble
(
element
))
{
ElementDetail
elementDetail
=
act
(
attributes
,
element
,
INPUT
);
ActResult
act
=
act
(
attributes
,
element
,
INPUT
);
ElementDetail
elementDetail
=
act
.
getElementDetail
();
elementDetails
.
add
(
elementDetail
);
elementDetails
.
add
(
elementDetail
);
if
(
act
.
getStaleness
())
{
traversal
(
elementDetails
);
}
}
}
}
}
for
(
Attributes
attributes
:
elements
.
keySet
())
{
for
(
Attributes
attributes
:
elements
.
keySet
())
{
WebElement
element
=
elements
.
get
(
attributes
);
WebElement
element
=
elements
.
get
(
attributes
);
if
(!
currentHistoryAttributes
.
contains
(
attributes
)
&&
isClickAble
(
element
))
{
if
(!
currentHistoryAttributes
.
contains
(
attributes
)
&&
isClickAble
(
element
))
{
ElementDetail
elementDetail
=
act
(
attributes
,
element
,
CLICK
);
ActResult
act
=
act
(
attributes
,
element
,
CLICK
);
boolean
urlChanged
=
checkURl
();
ElementDetail
elementDetail
=
act
.
getElementDetail
();
boolean
sourceChanged
=
checkSource
();
elementDetails
.
add
(
elementDetail
);
if
(
urlChanged
)
{
if
(
act
.
getStaleness
())
{
boolean
newPage
=
!
historyUrls
.
contains
(
driver
.
getCurrentUrl
())
traversal
(
elementDetails
);
&&
!
Sets
.
newHashSet
(
urlQueue
).
contains
(
driver
.
getCurrentUrl
())
&&
sourceChanged
;
if
(
newPage
)
{
log
.
info
(
"检测到新url:{} 加入队列 "
,
driver
.
getCurrentUrl
());
urlQueue
.
add
(
driver
.
getCurrentUrl
());
urlQueue
=
queueDuplicateRemoval
(
urlQueue
);
}
reload
();
}
if
(
nonNull
(
elementDetail
))
{
elementDetails
.
add
(
elementDetail
);
}
}
}
}
}
}
}
}
private
ElementDetail
act
(
Attributes
attributes
,
WebElement
element
,
String
actType
)
{
private
ActResult
act
(
Attributes
attributes
,
WebElement
element
,
String
actType
)
{
String
type
=
String
.
format
(
"<%s>%s"
,
element
.
getTagName
(),
element
.
getText
());
String
type
=
String
.
format
(
"<%s>%s"
,
element
.
getTagName
(),
element
.
getText
());
String
message
=
null
;
String
message
=
null
;
long
responseTime
=
-
1L
;
long
responseTime
=
-
1L
;
boolean
success
=
false
;
boolean
success
=
false
;
boolean
staleness
=
false
;
try
{
try
{
if
(
isEnabled
(
element
))
{
if
(
isEnabled
(
element
))
{
log
.
info
(
"元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}] ------ notStale:{}"
,
element
.
getTagName
(),
element
.
getText
(),
attributes
,
!
ExpectedConditions
.
stalenessOf
(
element
).
apply
(
driver
));
log
.
info
(
"元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}] ------ notStale:{}"
,
element
.
getTagName
(),
element
.
getText
(),
attributes
,
!
ExpectedConditions
.
stalenessOf
(
element
).
apply
(
driver
));
...
@@ -225,6 +220,24 @@ public class SeleniumExecutor {
...
@@ -225,6 +220,24 @@ public class SeleniumExecutor {
element
.
click
();
element
.
click
();
sleep
();
sleep
();
success
=
true
;
success
=
true
;
boolean
urlChanged
=
checkURl
();
boolean
sourceChanged
=
checkSource
();
if
(
urlChanged
)
{
boolean
newPage
=
!
historyUrls
.
contains
(
driver
.
getCurrentUrl
())
&&
!
Sets
.
newHashSet
(
urlQueue
).
contains
(
driver
.
getCurrentUrl
())
&&
sourceChanged
;
if
(
newPage
)
{
log
.
info
(
"检测到新url:{} 加入队列 "
,
driver
.
getCurrentUrl
());
urlQueue
.
add
(
driver
.
getCurrentUrl
());
urlQueue
=
queueDuplicateRemoval
(
urlQueue
);
}
reload
();
}
if
(
ExpectedConditions
.
stalenessOf
(
element
).
apply
(
driver
))
{
element
=
elements
.
get
(
attributes
);
staleness
=
true
;
}
}
}
long
endTime
=
System
.
currentTimeMillis
();
long
endTime
=
System
.
currentTimeMillis
();
responseTime
=
endTime
-
startTime
;
responseTime
=
endTime
-
startTime
;
...
@@ -249,10 +262,10 @@ public class SeleniumExecutor {
...
@@ -249,10 +262,10 @@ public class SeleniumExecutor {
}
}
}
catch
(
ElementNotInteractableException
e
)
{
}
catch
(
ElementNotInteractableException
e
)
{
log
.
error
(
"error:可操作范围之外的元素"
);
log
.
error
(
"error:可操作范围之外的元素"
);
return
n
ull
;
return
n
ew
ActResult
(
staleness
,
null
)
;
}
catch
(
StaleElementReferenceException
e
)
{
}
catch
(
StaleElementReferenceException
e
)
{
log
.
error
(
"元素过期"
);
log
.
error
(
"元素过期"
);
return
n
ull
;
return
n
ew
ActResult
(
staleness
,
null
)
;
}
catch
(
TimeoutException
e
)
{
}
catch
(
TimeoutException
e
)
{
message
=
"页面超时"
;
message
=
"页面超时"
;
success
=
false
;
success
=
false
;
...
@@ -272,11 +285,14 @@ public class SeleniumExecutor {
...
@@ -272,11 +285,14 @@ public class SeleniumExecutor {
}
}
historyAttributes
.
add
(
attributes
);
historyAttributes
.
add
(
attributes
);
currentHistoryAttributes
.
add
(
attributes
);
currentHistoryAttributes
.
add
(
attributes
);
return
new
ElementDetail
(
return
new
ActResult
(
type
,
staleness
,
(
int
)
responseTime
,
new
ElementDetail
(
success
,
type
,
message
(
int
)
responseTime
,
success
,
message
)
);
);
}
}
...
@@ -321,7 +337,7 @@ public class SeleniumExecutor {
...
@@ -321,7 +337,7 @@ public class SeleniumExecutor {
driver
.
get
(
currentUrl
);
driver
.
get
(
currentUrl
);
sleep
();
sleep
();
currentSource
=
driver
.
getPageSource
();
currentSource
=
driver
.
getPageSource
();
elements
=
getAllElements
(
driver
);
elements
=
getAllElements
(
driver
);
}
}
@SuppressWarnings
(
"unused"
)
@SuppressWarnings
(
"unused"
)
...
@@ -334,25 +350,20 @@ public class SeleniumExecutor {
...
@@ -334,25 +350,20 @@ public class SeleniumExecutor {
}
}
private
Map
<
Attributes
,
WebElement
>
getAllElements
(
WebDriver
driver
)
{
private
Map
<
Attributes
,
WebElement
>
getAllElements
(
WebDriver
driver
)
{
try
{
List
<
WebElement
>
webElements
=
Lists
.
newArrayList
();
List
<
WebElement
>
webElements
=
Lists
.
newArrayList
();
driver
.
findElements
(
By
.
xpath
(
"*"
))
driver
.
findElements
(
By
.
xpath
(
"*"
))
.
forEach
(
element
->
getSubElements
(
element
,
webElements
));
.
forEach
(
element
->
getSubElements
(
element
,
webElements
));
Document
document
=
Jsoup
.
parse
(
driver
.
getPageSource
());
Document
document
=
Jsoup
.
parse
(
driver
.
getPageSource
());
List
<
Element
>
jElements
=
document
.
getAllElements
().
stream
()
List
<
Element
>
jElements
=
document
.
getAllElements
().
stream
()
.
filter
(
element
->
element
.
childrenSize
()
==
0
)
.
filter
(
element
->
element
.
childrenSize
()
==
0
)
.
collect
(
Collectors
.
toList
());
.
collect
(
Collectors
.
toList
());
Map
<
Attributes
,
WebElement
>
result
=
Maps
.
newHashMap
();
Map
<
Attributes
,
WebElement
>
result
=
Maps
.
newHashMap
();
for
(
int
i
=
0
;
i
<
webElements
.
size
();
i
++)
{
for
(
int
i
=
0
;
i
<
webElements
.
size
();
i
++)
{
WebElement
webElement
=
webElements
.
get
(
i
);
WebElement
webElement
=
webElements
.
get
(
i
);
Element
jElement
=
jElements
.
get
(
i
);
Element
jElement
=
jElements
.
get
(
i
);
result
.
put
(
jElement
.
attributes
(),
webElement
);
result
.
put
(
jElement
.
attributes
(),
webElement
);
}
return
result
;
}
catch
(
StaleElementReferenceException
e
)
{
log
.
error
(
"获取元素过程中遇到元素过期 重新开始获取"
);
return
getAllElements
(
driver
);
}
}
return
result
;
}
}
private
void
getSubElements
(
WebElement
element
,
List
<
WebElement
>
elements
)
{
private
void
getSubElements
(
WebElement
element
,
List
<
WebElement
>
elements
)
{
...
@@ -434,10 +445,10 @@ public class SeleniumExecutor {
...
@@ -434,10 +445,10 @@ public class SeleniumExecutor {
boolean
yMatch
=
(
elementLocation
.
y
)
>=
0
&&
(
elementLocation
.
y
<=
size
.
height
);
boolean
yMatch
=
(
elementLocation
.
y
)
>=
0
&&
(
elementLocation
.
y
<=
size
.
height
);
return
xMatch
&&
yMatch
;
return
xMatch
&&
yMatch
;
}
catch
(
StaleElementReferenceException
e
)
{
}
catch
(
StaleElementReferenceException
e
)
{
log
.
error
(
"判断位置
布局时
元素过期"
);
log
.
error
(
"判断位置
时出现
元素过期"
);
return
tru
e
;
return
fals
e
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
"
error
:"
+
e
);
log
.
error
(
"
判断位置时出现错误
:"
+
e
);
return
false
;
return
false
;
}
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论