Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
auto-test
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄承天
auto-test
Commits
7cb8a4b1
提交
7cb8a4b1
authored
4月 03, 2020
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修正测试执行机制 取消jsoup使用
上级
866f299f
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
82 行增加
和
55 行删除
+82
-55
ElementFeature.java
...main/java/com/zjty/autotest/pojo/test/ElementFeature.java
+20
-0
SeleniumExecutor.java
...java/com/zjty/autotest/service/impl/SeleniumExecutor.java
+62
-55
没有找到文件。
src/main/java/com/zjty/autotest/pojo/test/ElementFeature.java
0 → 100644
浏览文件 @
7cb8a4b1
package
com
.
zjty
.
autotest
.
pojo
.
test
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
org.openqa.selenium.Dimension
;
import
org.openqa.selenium.Point
;
/**
* @author C
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public
class
ElementFeature
{
private
Point
location
;
private
Dimension
size
;
private
String
tag
;
private
String
text
;
}
src/main/java/com/zjty/autotest/service/impl/SeleniumExecutor.java
浏览文件 @
7cb8a4b1
...
@@ -9,14 +9,12 @@ import com.zjty.autotest.pojo.report.ElementDetail;
...
@@ -9,14 +9,12 @@ 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.ActResult
;
import
com.zjty.autotest.pojo.test.ElementFeature
;
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
;
import
com.zjty.autotest.util.WebDriverUtil
;
import
com.zjty.autotest.util.WebDriverUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.jsoup.Jsoup
;
import
org.jsoup.nodes.Attributes
;
import
org.jsoup.nodes.Document
;
import
org.jsoup.nodes.Element
;
import
org.jsoup.nodes.Element
;
import
org.openqa.selenium.*
;
import
org.openqa.selenium.*
;
import
org.openqa.selenium.logging.LogEntry
;
import
org.openqa.selenium.logging.LogEntry
;
...
@@ -55,15 +53,15 @@ public class SeleniumExecutor {
...
@@ -55,15 +53,15 @@ public class SeleniumExecutor {
private
Set
<
String
>
historyUrls
=
Sets
.
newHashSet
();
private
Set
<
String
>
historyUrls
=
Sets
.
newHashSet
();
private
Set
<
Attributes
>
historyAttribut
es
=
Sets
.
newHashSet
();
private
Set
<
ElementFeature
>
historyFeatur
es
=
Sets
.
newHashSet
();
private
Set
<
Attributes
>
currentHistoryAttribut
es
=
Sets
.
newHashSet
();
private
Set
<
ElementFeature
>
currentHistoryFeatur
es
=
Sets
.
newHashSet
();
private
Queue
<
String
>
windowQueue
=
Queues
.
newLinkedBlockingDeque
();
private
Queue
<
String
>
windowQueue
=
Queues
.
newLinkedBlockingDeque
();
private
Queue
<
String
>
urlQueue
=
Queues
.
newLinkedBlockingDeque
();
private
Queue
<
String
>
urlQueue
=
Queues
.
newLinkedBlockingDeque
();
private
Map
<
Attributes
,
WebElement
>
elements
=
Maps
.
newHashMap
();
private
Map
<
ElementFeature
,
WebElement
>
elements
=
Maps
.
newHashMap
();
/**
/**
* 当前进行的窗口页
* 当前进行的窗口页
*/
*/
...
@@ -112,7 +110,8 @@ public class SeleniumExecutor {
...
@@ -112,7 +110,8 @@ public class SeleniumExecutor {
}
}
private
Measure
testUrl
(
String
currentUrl
)
{
private
Measure
testUrl
(
String
currentUrl
)
{
Measure
measure
=
null
;
Measure
measure
=
new
Measure
();
measure
.
setUrl
(
currentUrl
);
//如果该网页是未曾进入过的网页则加入历史记录并进行遍历测试
//如果该网页是未曾进入过的网页则加入历史记录并进行遍历测试
if
(!
historyUrls
.
contains
(
currentUrl
))
{
if
(!
historyUrls
.
contains
(
currentUrl
))
{
historyUrls
.
add
(
driver
.
getCurrentUrl
());
historyUrls
.
add
(
driver
.
getCurrentUrl
());
...
@@ -137,10 +136,13 @@ public class SeleniumExecutor {
...
@@ -137,10 +136,13 @@ public class SeleniumExecutor {
elements
=
getAllElements
(
driver
);
elements
=
getAllElements
(
driver
);
log
.
info
(
"获取完毕...共{}个元素..."
,
elements
.
size
());
log
.
info
(
"获取完毕...共{}个元素..."
,
elements
.
size
());
//当前页面的历史元素记录重置
//当前页面的历史元素记录重置
currentHistory
Attribut
es
.
clear
();
currentHistory
Featur
es
.
clear
();
//开始遍历操作元素队列中的元素 返回各个元素测试信息
//开始遍历操作元素队列中的元素 返回各个元素测试信息
List
<
ElementDetail
>
elementDetails
=
Lists
.
newArrayList
();
List
<
ElementDetail
>
elementDetails
=
Lists
.
newArrayList
();
traversal
(
elementDetails
);
traversal
(
elementDetails
);
elementDetails
=
elementDetails
.
stream
()
.
filter
(
Objects:
:
nonNull
)
.
collect
(
Collectors
.
toList
());
//如果全部元素通过则是通过
//如果全部元素通过则是通过
Boolean
success
=
elementDetails
.
stream
()
Boolean
success
=
elementDetails
.
stream
()
.
filter
(
Objects:
:
nonNull
)
.
filter
(
Objects:
:
nonNull
)
...
@@ -176,18 +178,18 @@ public class SeleniumExecutor {
...
@@ -176,18 +178,18 @@ public class SeleniumExecutor {
}
}
private
void
traversal
(
List
<
ElementDetail
>
elementDetails
)
{
private
void
traversal
(
List
<
ElementDetail
>
elementDetails
)
{
for
(
Attributes
attributes
:
elements
.
keySet
())
{
for
(
ElementFeature
feature
:
elements
.
keySet
())
{
WebElement
element
=
elements
.
get
(
attributes
);
WebElement
element
=
elements
.
get
(
feature
);
if
(!
currentHistory
Attributes
.
contains
(
attributes
)
&&
isInputAble
(
element
))
{
if
(!
currentHistory
Features
.
contains
(
feature
)
&&
isInputAble
(
element
))
{
ActResult
act
=
act
(
attributes
,
element
,
INPUT
);
ActResult
act
=
act
(
feature
,
element
,
INPUT
);
ElementDetail
elementDetail
=
act
.
getElementDetail
();
ElementDetail
elementDetail
=
act
.
getElementDetail
();
elementDetails
.
add
(
elementDetail
);
elementDetails
.
add
(
elementDetail
);
}
}
}
}
for
(
Attributes
attributes
:
elements
.
keySet
())
{
for
(
ElementFeature
feature
:
elements
.
keySet
())
{
WebElement
element
=
elements
.
get
(
attributes
);
WebElement
element
=
elements
.
get
(
feature
);
if
(!
currentHistory
Attributes
.
contains
(
attributes
)
&&
isClickAble
(
element
))
{
if
(!
currentHistory
Features
.
contains
(
feature
)
&&
isClickAble
(
element
))
{
ActResult
act
=
act
(
attributes
,
element
,
CLICK
);
ActResult
act
=
act
(
feature
,
element
,
CLICK
);
ElementDetail
elementDetail
=
act
.
getElementDetail
();
ElementDetail
elementDetail
=
act
.
getElementDetail
();
elementDetails
.
add
(
elementDetail
);
elementDetails
.
add
(
elementDetail
);
if
(
act
.
getStaleness
())
{
if
(
act
.
getStaleness
())
{
...
@@ -199,8 +201,7 @@ public class SeleniumExecutor {
...
@@ -199,8 +201,7 @@ public class SeleniumExecutor {
}
}
}
}
@SuppressWarnings
(
"AlibabaMethodTooLong"
)
private
ActResult
act
(
ElementFeature
feature
,
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
;
...
@@ -208,7 +209,7 @@ public class SeleniumExecutor {
...
@@ -208,7 +209,7 @@ public class SeleniumExecutor {
boolean
staleness
=
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:[{}] ----
feature:[{}] ------ notStale:{}"
,
element
.
getTagName
(),
element
.
getText
(),
feature
,
!
ExpectedConditions
.
stalenessOf
(
element
).
apply
(
driver
));
long
startTime
=
System
.
currentTimeMillis
();
long
startTime
=
System
.
currentTimeMillis
();
if
(
Objects
.
equals
(
actType
,
INPUT
))
{
if
(
Objects
.
equals
(
actType
,
INPUT
))
{
log
.
info
(
"Input操作"
);
log
.
info
(
"Input操作"
);
...
@@ -222,23 +223,6 @@ public class SeleniumExecutor {
...
@@ -222,23 +223,6 @@ 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
;
...
@@ -260,13 +244,14 @@ public class SeleniumExecutor {
...
@@ -260,13 +244,14 @@ public class SeleniumExecutor {
success
=
false
;
success
=
false
;
log
.
error
(
"error:出现js异常:{}"
,
jsMsg
);
log
.
error
(
"error:出现js异常:{}"
,
jsMsg
);
}
}
staleness
=
checkPage
();
}
}
}
catch
(
ElementNotInteractableException
e
)
{
}
catch
(
ElementNotInteractableException
e
)
{
log
.
error
(
"error:可操作范围之外的元素"
);
log
.
error
(
"error:可操作范围之外的元素"
);
return
new
ActResult
(
staleness
,
null
);
return
new
ActResult
(
false
,
null
);
}
catch
(
StaleElementReferenceException
e
)
{
}
catch
(
StaleElementReferenceException
e
)
{
log
.
error
(
"元素过期"
);
log
.
error
(
"元素过期"
);
return
new
ActResult
(
staleness
,
null
);
return
new
ActResult
(
false
,
null
);
}
catch
(
TimeoutException
e
)
{
}
catch
(
TimeoutException
e
)
{
message
=
"页面超时"
;
message
=
"页面超时"
;
success
=
false
;
success
=
false
;
...
@@ -284,8 +269,8 @@ public class SeleniumExecutor {
...
@@ -284,8 +269,8 @@ public class SeleniumExecutor {
success
=
false
;
success
=
false
;
log
.
error
(
"error:"
+
e
);
log
.
error
(
"error:"
+
e
);
}
}
history
Attributes
.
add
(
attributes
);
history
Features
.
add
(
feature
);
currentHistory
Attributes
.
add
(
attributes
);
currentHistory
Features
.
add
(
feature
);
return
new
ActResult
(
return
new
ActResult
(
staleness
,
staleness
,
new
ElementDetail
(
new
ElementDetail
(
...
@@ -297,6 +282,25 @@ public class SeleniumExecutor {
...
@@ -297,6 +282,25 @@ public class SeleniumExecutor {
);
);
}
}
private
Boolean
checkPage
(){
boolean
staleness
=
false
;
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
);
staleness
=
true
;
}
reload
();
}
return
staleness
;
}
private
Boolean
checkURl
()
{
private
Boolean
checkURl
()
{
String
practiseUrl
=
driver
.
getCurrentUrl
();
String
practiseUrl
=
driver
.
getCurrentUrl
();
return
!
Objects
.
equals
(
currentUrl
,
practiseUrl
);
return
!
Objects
.
equals
(
currentUrl
,
practiseUrl
);
...
@@ -350,19 +354,13 @@ public class SeleniumExecutor {
...
@@ -350,19 +354,13 @@ public class SeleniumExecutor {
}
}
}
}
private
Map
<
Attributes
,
WebElement
>
getAllElements
(
WebDriver
driver
)
{
private
Map
<
ElementFeature
,
WebElement
>
getAllElements
(
WebDriver
driver
)
{
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
());
Map
<
ElementFeature
,
WebElement
>
result
=
Maps
.
newHashMap
();
List
<
Element
>
jElements
=
document
.
getAllElements
().
stream
()
for
(
WebElement
webElement
:
webElements
)
{
.
filter
(
element
->
element
.
childrenSize
()
==
0
)
result
.
put
(
toFeature
(
webElement
),
webElement
);
.
collect
(
Collectors
.
toList
());
Map
<
Attributes
,
WebElement
>
result
=
Maps
.
newHashMap
();
for
(
int
i
=
0
;
i
<
webElements
.
size
();
i
++)
{
WebElement
webElement
=
webElements
.
get
(
i
);
Element
jElement
=
jElements
.
get
(
i
);
result
.
put
(
jElement
.
attributes
(),
webElement
);
}
}
return
result
;
return
result
;
}
}
...
@@ -430,11 +428,11 @@ public class SeleniumExecutor {
...
@@ -430,11 +428,11 @@ public class SeleniumExecutor {
}
}
@SuppressWarnings
(
"unused"
)
@SuppressWarnings
(
"unused"
)
private
Map
<
Attributes
,
WebElement
>
removeHistoryElements
(
Map
<
Attributes
,
WebElement
>
elements
)
{
private
Map
<
ElementFeature
,
WebElement
>
removeHistoryElements
(
Map
<
ElementFeature
,
WebElement
>
elements
)
{
Map
<
Attributes
,
WebElement
>
result
=
Maps
.
newHashMap
(
elements
);
Map
<
ElementFeature
,
WebElement
>
result
=
Maps
.
newHashMap
(
elements
);
elements
.
forEach
((
attributes
,
element
)
->
{
elements
.
forEach
((
feature
,
element
)
->
{
if
(
history
Attributes
.
contains
(
attributes
))
{
if
(
history
Features
.
contains
(
feature
))
{
result
.
remove
(
attributes
,
element
);
result
.
remove
(
feature
,
element
);
}
}
});
});
return
result
;
return
result
;
...
@@ -485,4 +483,13 @@ public class SeleniumExecutor {
...
@@ -485,4 +483,13 @@ public class SeleniumExecutor {
return
null
;
return
null
;
}
}
private
ElementFeature
toFeature
(
WebElement
element
)
{
return
new
ElementFeature
(
element
.
getLocation
(),
element
.
getSize
(),
element
.
getTagName
(),
element
.
getText
()
);
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论