Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
auto-test
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄承天
auto-test
Commits
17b82071
提交
17b82071
authored
3月 29, 2020
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加对几个JS异常的检测
上级
9cffede2
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
62 行增加
和
62 行删除
+62
-62
SeleniumExecutor.java
...java/com/zjty/autotest/service/impl/SeleniumExecutor.java
+62
-62
没有找到文件。
src/main/java/com/zjty/autotest/service/impl/SeleniumExecutor.java
浏览文件 @
17b82071
...
@@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
...
@@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Queues
;
import
com.google.common.collect.Queues
;
import
com.google.common.collect.Sets
;
import
com.google.common.collect.Sets
;
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
;
...
@@ -17,6 +18,8 @@ import org.jsoup.nodes.Attributes;
...
@@ -17,6 +18,8 @@ import org.jsoup.nodes.Attributes;
import
org.jsoup.nodes.Document
;
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.LogType
;
import
org.openqa.selenium.support.ui.ExpectedConditions
;
import
org.openqa.selenium.support.ui.ExpectedConditions
;
import
org.openqa.selenium.support.ui.Sleeper
;
import
org.openqa.selenium.support.ui.Sleeper
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
...
@@ -24,10 +27,11 @@ import org.springframework.stereotype.Service;
...
@@ -24,10 +27,11 @@ import org.springframework.stereotype.Service;
import
java.time.Duration
;
import
java.time.Duration
;
import
java.util.*
;
import
java.util.*
;
import
java.util.NoSuchElementException
;
import
java.util.function.Predicate
;
import
java.util.function.Predicate
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
static
com
.
zjty
.
autotest
.
common
.
action
.
LabelType
.
INPUT
;
import
static
com
.
zjty
.
autotest
.
common
.
action
.
Action
.*
;
import
static
java
.
util
.
Objects
.
nonNull
;
import
static
java
.
util
.
Objects
.
nonNull
;
/**
/**
...
@@ -87,7 +91,7 @@ public class SeleniumExecutor {
...
@@ -87,7 +91,7 @@ public class SeleniumExecutor {
driver
.
switchTo
().
window
(
currentWindow
);
driver
.
switchTo
().
window
(
currentWindow
);
while
(
nonNull
(
currentUrl
))
{
while
(
nonNull
(
currentUrl
))
{
Measure
measure
=
testUrl
(
currentUrl
);
Measure
measure
=
testUrl
(
currentUrl
);
if
(
nonNull
(
measure
)){
if
(
nonNull
(
measure
))
{
measures
.
add
(
measure
);
measures
.
add
(
measure
);
}
}
currentUrl
=
urlQueue
.
poll
();
currentUrl
=
urlQueue
.
poll
();
...
@@ -124,7 +128,7 @@ public class SeleniumExecutor {
...
@@ -124,7 +128,7 @@ public class SeleniumExecutor {
//获取当前网页的所有元素并放入元素队列
//获取当前网页的所有元素并放入元素队列
Map
<
Attributes
,
WebElement
>
elements
=
getAllElements
(
driver
);
Map
<
Attributes
,
WebElement
>
elements
=
getAllElements
(
driver
);
log
.
info
(
"获取完毕...共{}个元素..."
,
elements
.
size
());
log
.
info
(
"获取完毕...共{}个元素..."
,
elements
.
size
());
//
元素序号
重置
//
当前页面的历史元素记录
重置
currentHistoryAttributes
.
clear
();
currentHistoryAttributes
.
clear
();
//开始遍历操作元素队列中的元素 返回各个元素测试信息
//开始遍历操作元素队列中的元素 返回各个元素测试信息
List
<
ElementDetail
>
elementDetails
=
Lists
.
newArrayList
();
List
<
ElementDetail
>
elementDetails
=
Lists
.
newArrayList
();
...
@@ -139,13 +143,15 @@ public class SeleniumExecutor {
...
@@ -139,13 +143,15 @@ public class SeleniumExecutor {
screenshot
=
String
.
format
(
"http://%s:%s/screenshots/%s"
,
screenshotPrefixHost
,
screenshotPrefixPort
,
screenshotName
);
screenshot
=
String
.
format
(
"http://%s:%s/screenshots/%s"
,
screenshotPrefixHost
,
screenshotPrefixPort
,
screenshotName
);
}
}
//总合信息
//总合信息
HashSet
<
String
>
messages
=
Sets
.
newHashSet
();
Set
<
String
>
messages
=
elementDetails
.
stream
()
elementDetails
.
forEach
(
elementDetail
->
messages
.
add
(
elementDetail
.
getMessage
()));
.
map
(
ElementDetail:
:
getMessage
)
.
filter
(
Objects:
:
nonNull
)
.
collect
(
Collectors
.
toSet
());
StringBuilder
message
=
new
StringBuilder
();
StringBuilder
message
=
new
StringBuilder
();
messages
.
forEach
(
message:
:
append
);
for
(
String
msg
:
messages
)
{
for
(
String
msg
:
messages
)
{
message
.
append
(
msg
);
message
.
append
(
msg
);
message
.
append
(
';'
);
message
.
append
(
";"
);
}
}
log
.
info
(
"遍历完毕..."
);
log
.
info
(
"遍历完毕..."
);
measure
=
new
Measure
(
measure
=
new
Measure
(
...
@@ -165,14 +171,14 @@ public class SeleniumExecutor {
...
@@ -165,14 +171,14 @@ public class SeleniumExecutor {
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
Input
(
attributes
,
element
);
ElementDetail
elementDetail
=
act
(
attributes
,
element
,
INPUT
);
elementDetails
.
add
(
elementDetail
);
elementDetails
.
add
(
elementDetail
);
}
}
}
}
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
Click
(
attributes
,
element
);
ElementDetail
elementDetail
=
act
(
attributes
,
element
,
CLICK
);
boolean
urlChanged
=
checkURl
();
boolean
urlChanged
=
checkURl
();
boolean
sourceChanged
=
checkSource
();
boolean
sourceChanged
=
checkSource
();
if
(
urlChanged
)
{
if
(
urlChanged
)
{
...
@@ -192,82 +198,56 @@ public class SeleniumExecutor {
...
@@ -192,82 +198,56 @@ public class SeleniumExecutor {
}
}
}
}
private
ElementDetail
act
Input
(
Attributes
attributes
,
WebElement
element
)
{
private
ElementDetail
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
=
""
;
String
message
=
null
;
Long
responseTime
=
-
1
L
;
long
responseTime
=
0
L
;
boolean
success
=
false
;
boolean
success
=
false
;
try
{
try
{
if
(
isEnabled
(
element
))
{
if
(
isEnabled
(
element
))
{
log
.
info
(
"Input元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}]"
,
element
.
getTagName
(),
element
.
getText
(),
attributes
);
long
startTime
=
System
.
currentTimeMillis
();
long
startTime
=
System
.
currentTimeMillis
();
if
(!
inputs
.
isEmpty
())
{
if
(
Objects
.
equals
(
actType
,
INPUT
))
{
Input
input
=
inputs
.
remove
(
0
);
log
.
info
(
"Input操作 元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}]"
,
element
.
getTagName
(),
element
.
getText
(),
attributes
);
element
.
sendKeys
(
input
.
getValue
());
if
(!
inputs
.
isEmpty
())
{
Input
input
=
inputs
.
remove
(
0
);
element
.
sendKeys
(
input
.
getValue
());
}
success
=
true
;
}
else
if
(
Objects
.
equals
(
actType
,
CLICK
))
{
log
.
info
(
"Click操作 元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}]"
,
element
.
getTagName
(),
element
.
getText
(),
attributes
);
element
.
click
();
sleep
();
success
=
true
;
}
}
long
endTime
=
System
.
currentTimeMillis
();
long
endTime
=
System
.
currentTimeMillis
();
responseTime
=
endTime
-
startTime
;
responseTime
=
endTime
-
startTime
;
success
=
true
;
Alert
alert
=
ExpectedConditions
.
alertIsPresent
().
apply
(
driver
);
Alert
alert
=
ExpectedConditions
.
alertIsPresent
().
apply
(
driver
);
if
(
nonNull
(
alert
))
{
if
(
nonNull
(
alert
))
{
message
=
"出现警告窗:"
+
alert
.
getText
();
message
=
"出现警告窗:"
+
alert
.
getText
();
alert
.
accept
();
alert
.
accept
();
success
=
false
;
success
=
false
;
}
}
}
}
catch
(
ElementNotInteractableException
e
)
{
message
=
"异常的布局"
;
success
=
false
;
log
.
error
(
"error:存在可操作范围之外的元素"
);
}
catch
(
TimeoutException
e
)
{
message
=
"页面超时"
;
success
=
false
;
log
.
error
(
"error:存在可操作范围之外的元素"
);
}
catch
(
WebDriverException
e
)
{
message
=
"无法访问的地址"
;
success
=
false
;
log
.
error
(
"error:无法访问的地址"
);
}
catch
(
Exception
e
)
{
message
=
"出现预料之外的错误"
;
success
=
false
;
log
.
error
(
"error:"
+
e
);
}
historyAttributes
.
add
(
attributes
);
currentHistoryAttributes
.
add
(
attributes
);
return
new
ElementDetail
(
type
,
responseTime
.
intValue
(),
success
,
message
);
}
private
ElementDetail
actClick
(
Attributes
attributes
,
WebElement
element
)
{
String
jsMsg
=
checkJsError
();
String
type
=
String
.
format
(
"<%s>%s"
,
element
.
getTagName
(),
element
.
getText
());
if
(
nonNull
(
jsMsg
))
{
String
message
=
""
;
message
=
jsMsg
;
Long
responseTIme
=
0L
;
boolean
success
=
false
;
try
{
if
(
isEnabled
(
element
))
{
log
.
info
(
"Input元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}]"
,
element
.
getTagName
(),
element
.
getText
(),
attributes
);
element
.
click
();
sleep
();
success
=
true
;
Alert
alert
=
ExpectedConditions
.
alertIsPresent
().
apply
(
driver
);
if
(
nonNull
(
alert
))
{
message
=
"出现警告窗:"
+
alert
.
getText
();
alert
.
accept
();
success
=
false
;
success
=
false
;
log
.
error
(
"error:出现js异常:{}"
,
jsMsg
);
}
}
}
}
}
catch
(
ElementNotInteractableException
e
)
{
}
catch
(
ElementNotInteractableException
e
)
{
message
=
"
异常的布局
"
;
message
=
"
布局错误
"
;
success
=
false
;
success
=
false
;
log
.
error
(
"error:存在可操作范围之外的元素"
);
log
.
error
(
"error:存在可操作范围之外的元素"
);
}
catch
(
TimeoutException
e
)
{
}
catch
(
TimeoutException
e
)
{
message
=
"页面超时"
;
message
=
"页面超时"
;
success
=
false
;
success
=
false
;
log
.
error
(
"error:存在可操作范围之外的元素"
);
log
.
error
(
"error:存在可操作范围之外的元素"
);
}
catch
(
NoSuchElementException
e
)
{
message
=
"依赖缺失"
;
success
=
false
;
log
.
error
(
"error:依赖缺失"
);
}
catch
(
WebDriverException
e
)
{
}
catch
(
WebDriverException
e
)
{
message
=
"无法访问的地址"
;
message
=
"无法访问的地址"
;
success
=
false
;
success
=
false
;
...
@@ -281,7 +261,7 @@ public class SeleniumExecutor {
...
@@ -281,7 +261,7 @@ public class SeleniumExecutor {
currentHistoryAttributes
.
add
(
attributes
);
currentHistoryAttributes
.
add
(
attributes
);
return
new
ElementDetail
(
return
new
ElementDetail
(
type
,
type
,
responseTIme
.
intValue
()
,
(
int
)
responseTime
,
success
,
success
,
message
message
);
);
...
@@ -372,7 +352,7 @@ public class SeleniumExecutor {
...
@@ -372,7 +352,7 @@ public class SeleniumExecutor {
}
}
private
Boolean
isInputAble
(
WebElement
element
)
{
private
Boolean
isInputAble
(
WebElement
element
)
{
return
Objects
.
equals
(
element
.
getTagName
(),
INPUT
);
return
Objects
.
equals
(
element
.
getTagName
(),
LabelType
.
INPUT
);
}
}
@SuppressWarnings
(
"unused"
)
@SuppressWarnings
(
"unused"
)
...
@@ -445,4 +425,24 @@ public class SeleniumExecutor {
...
@@ -445,4 +425,24 @@ public class SeleniumExecutor {
return
result
;
return
result
;
}
}
@SuppressWarnings
(
"OptionalGetWithoutIsPresent"
)
private
String
checkJsError
()
{
try
{
return
analyseLogError
(
driver
.
manage
().
logs
().
get
(
LogType
.
BROWSER
).
getAll
().
stream
().
findFirst
().
get
());
}
catch
(
Exception
e
){
return
null
;
}
}
private
String
analyseLogError
(
LogEntry
logEntry
)
{
if
(
logEntry
.
getMessage
().
contains
(
"Uncaught TypeError:"
))
{
return
"JS类型错误"
;
}
else
if
(
logEntry
.
getMessage
().
contains
(
"Failed to load resource: net::ERR_FILE_NOT_FOUND"
))
{
return
"资源加载错误"
;
}
else
if
(
logEntry
.
getMessage
().
contains
(
"Failed to load resource: net::ERR_CONNECTION_RESET"
))
{
return
"数据请求错误"
;
}
return
null
;
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论