Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
auto-test
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄承天
auto-test
Commits
3065a019
提交
3065a019
authored
3月 22, 2020
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加元素位置、截图、响应时间相关逻辑
上级
7c55db22
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
53 行增加
和
35 行删除
+53
-35
SeleniumExecutor.java
...main/java/com/zjty/autotest/service/SeleniumExecutor.java
+18
-9
FileUtil.java
src/main/java/com/zjty/autotest/util/FileUtil.java
+12
-1
AutotestApplicationTests.java
...test/java/com/zjty/autotest/AutotestApplicationTests.java
+23
-25
没有找到文件。
src/main/java/com/zjty/autotest/service/SeleniumExecutor.java
浏览文件 @
3065a019
...
...
@@ -4,11 +4,9 @@ import com.google.common.collect.Lists;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Queues
;
import
com.google.common.collect.Sets
;
import
com.zjty.autotest.util.FileUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.openqa.selenium.By
;
import
org.openqa.selenium.StaleElementReferenceException
;
import
org.openqa.selenium.WebDriver
;
import
org.openqa.selenium.WebElement
;
import
org.openqa.selenium.*
;
import
org.springframework.stereotype.Service
;
import
java.util.*
;
...
...
@@ -40,8 +38,7 @@ public class SeleniumExecutor {
private
String
currentUrl
;
public
List
<
String
>
execute
(
String
url
)
{
driver
.
get
(
url
);
currentUrl
=
driver
.
getCurrentUrl
();
currentUrl
=
url
;
currentWindow
=
driver
.
getWindowHandle
();
while
(
nonNull
(
currentWindow
))
{
driver
.
switchTo
().
window
(
currentWindow
);
...
...
@@ -59,8 +56,13 @@ public class SeleniumExecutor {
private
void
testUrl
(
String
currentUrl
)
{
if
(!
historyUrls
.
contains
(
currentUrl
))
{
historyUrls
.
add
(
driver
.
getCurrentUrl
());
log
.
info
(
"当前URL:{} 开始进行遍历..."
,
currentUrl
);
long
startTime
=
System
.
currentTimeMillis
();
driver
.
get
(
currentUrl
);
long
endTime
=
System
.
currentTimeMillis
();
long
costTime
=
endTime
-
startTime
;
log
.
info
(
"当前URL:{} 响应时间 {} ms 开始进行遍历..."
,
currentUrl
,
costTime
);
log
.
info
(
"正在获取当前网页所有元素..."
);
getAllElements
(
driver
);
log
.
info
(
"获取完毕...共{}个元素..."
,
elements
.
size
());
elementIndex
=
0
;
...
...
@@ -85,7 +87,7 @@ public class SeleniumExecutor {
try
{
log
.
info
(
"正在操作第{}个元素 ------ text:[{}] ----- tag:[{}]"
,
elementIndex
+
1
,
element
.
getText
(),
element
.
getTagName
());
elementIndex
++;
if
(
isEnabledAndDisplayed
(
element
)){
if
(
isEnabledAndDisplayed
(
element
))
{
if
(
isEnabledInput
(
element
))
{
String
id
=
element
.
getAttribute
(
"id"
);
String
inputValue
=
inputs
.
get
(
id
);
...
...
@@ -98,6 +100,8 @@ public class SeleniumExecutor {
sleep
(
2000L
);
}
catch
(
StaleElementReferenceException
e
)
{
reload
();
}
catch
(
ElementNotInteractableException
e
)
{
log
.
error
(
"error:在可操作范围之外的元素"
);
}
catch
(
Exception
e
)
{
log
.
error
(
"error:"
+
e
.
getMessage
());
}
...
...
@@ -148,7 +152,7 @@ public class SeleniumExecutor {
}
else
{
results
.
forEach
(
this
::
getSubElements
);
}
}
catch
(
StaleElementReferenceException
e
){
}
catch
(
StaleElementReferenceException
e
)
{
}
}
...
...
@@ -166,6 +170,11 @@ public class SeleniumExecutor {
}
private
void
screenShot
(
String
name
)
{
byte
[]
bytes
=
((
TakesScreenshot
)
driver
).
getScreenshotAs
(
OutputType
.
BYTES
);
FileUtil
.
output
(
bytes
,
FileUtil
.
WORK_PATH
+
name
+
".png"
);
}
//
// Setter
//
...
...
src/main/java/com/zjty/autotest/util/FileUtil.java
浏览文件 @
3065a019
...
...
@@ -14,7 +14,18 @@ import java.nio.charset.StandardCharsets;
@Slf4j
public
class
FileUtil
{
private
final
static
String
WORK_PATH
=
System
.
getProperty
(
"user.dir"
)
+
"\\"
;
public
final
static
String
WORK_PATH
=
System
.
getProperty
(
"user.dir"
)
+
"\\"
;
public
static
void
output
(
byte
[]
bytes
,
String
path
)
{
File
file
=
new
File
(
path
);
try
{
OutputStream
output
=
new
FileOutputStream
(
file
);
output
.
write
(
bytes
);
output
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
static
void
output
(
String
text
,
OutputStream
os
)
{
byte
[]
buffer
=
new
byte
[
1024
];
...
...
src/test/java/com/zjty/autotest/AutotestApplicationTests.java
浏览文件 @
3065a019
...
...
@@ -3,52 +3,50 @@ package com.zjty.autotest;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Queues
;
import
com.zjty.autotest.service.SeleniumExecutor
;
import
com.zjty.autotest.util.FileUtil
;
import
com.zjty.autotest.util.WebDriverUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.junit.jupiter.api.Test
;
import
org.openqa.selenium.By
;
import
org.openqa.selenium.WebDriver
;
import
org.openqa.selenium.WebElement
;
import
org.openqa.selenium.*
;
import
org.openqa.selenium.chrome.ChromeDriver
;
import
org.openqa.selenium.firefox.FirefoxDriver
;
import
org.openqa.selenium.logging.LogEntries
;
import
org.openqa.selenium.logging.LogEntry
;
import
org.openqa.selenium.logging.LogType
;
import
org.openqa.selenium.logging.LoggingPreferences
;
import
org.openqa.selenium.remote.CapabilityType
;
import
org.openqa.selenium.remote.DesiredCapabilities
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
java.util.*
;
import
java.util.logging.Level
;
@Slf4j
@SpringBootTest
class
AutotestApplicationTests
{
@Autowired
SeleniumExecutor
seleniumExecutor
;
@Test
public
void
execute
()
{
Map
<
String
,
String
>
inputs
=
Maps
.
newHashMap
();
inputs
.
put
(
"name"
,
"root"
);
inputs
.
put
(
"password"
,
"root"
);
System
.
setProperty
(
"webdriver.firefox.driver"
,
WebDriverUtil
.
FIRE_FOX_EXE
);
FirefoxDriver
driver
=
new
FirefoxDriver
();
String
url
=
"https://qxmugen.com/"
;
DesiredCapabilities
caps
=
DesiredCapabilities
.
firefox
();
LoggingPreferences
logPrefs
=
new
LoggingPreferences
();
logPrefs
.
enable
(
LogType
.
BROWSER
,
Level
.
ALL
);
caps
.
setCapability
(
CapabilityType
.
LOGGING_PREFS
,
logPrefs
);
WebDriver
driver
=
new
FirefoxDriver
();
String
url
=
"file:///C:/Users/C/Documents/WeChat%20Files/c18042003295/FileStorage/File/2020-03/test1.html"
;
long
startTime
=
System
.
currentTimeMillis
();
driver
.
get
(
url
);
seleniumExecutor
.
setDriver
(
driver
);
seleniumExecutor
.
setInputs
(
inputs
);
seleniumExecutor
.
execute
(
url
);
// String currentwindowHandle;
// Queue<String> windowHandles = Queues.newLinkedBlockingDeque(driver.getWindowHandles());
// System.out.println(windowHandles);
// driver.findElement(By.xpath("/html/body/div[5]/div[3]/div[1]/div/div/div[2]/div/div[4]/a")).click();
// System.out.println("Current Url:"+driver.getCurrentUrl());
// System.out.println("has Window:"+driver.getWindowHandles().size());
//
//
// windowHandles = Queues.newLinkedBlockingDeque(driver.getWindowHandles());
// System.out.println(windowHandles);
//
// driver.findElement(By.xpath("/html/body/div[5]/div[3]/div[1]/div/div/div[2]/div/div[4]/a")).click();
//
// windowHandles = Queues.newLinkedBlockingDeque(driver.getWindowHandles());
// System.out.println(windowHandles);
long
endTime
=
System
.
currentTimeMillis
();
long
costTime
=
endTime
-
startTime
;
log
.
info
(
"当前URL:{} 响应时间 {} ms 开始进行遍历..."
,
url
,
costTime
);
driver
.
findElement
(
By
.
xpath
(
"/html/body/div/span"
)).
click
();
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论