Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
T
TestSpider
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Bugzilla
Bugzilla
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
尚立鹏
TestSpider
Commits
c8184bc2
Commit
c8184bc2
authored
Feb 21, 2023
by
ShangLiPeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify rpa
parent
974120dc
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
511 additions
and
71 deletions
+511
-71
LoginFunction.java
src/main/java/com/greatchn/etax/tianjin/LoginFunction.java
+2
-2
SessionFunction.java
src/main/java/com/greatchn/etax/tianjin/SessionFunction.java
+1
-1
SwitchTaxpayerFunction.java
...ava/com/greatchn/etax/tianjin/SwitchTaxpayerFunction.java
+1
-1
ChromeKits.java
src/main/java/com/greatchn/kits/ChromeKits.java
+25
-0
RpaCore.java
src/main/java/com/greatchn/rpa/RpaCore.java
+373
-55
rpa.yaml
src/main/resources/rpa.yaml
+8
-2
DriverTest.java
src/test/java/DriverTest.java
+101
-10
No files found.
src/main/java/com/greatchn/etax/tianjin/LoginFunction.java
View file @
c8184bc2
...
@@ -69,7 +69,7 @@ public class LoginFunction extends BaseFunction {
...
@@ -69,7 +69,7 @@ public class LoginFunction extends BaseFunction {
return
;
return
;
}
}
try
{
try
{
rpa
().
waitElement
(
By
.
cssSelector
(
"input.el-input__inner[placeholder=\"请设置密码\"]"
),
WAIT_TIME
);
rpa
().
waitElement
LoadAndVisible
(
By
.
cssSelector
(
"input.el-input__inner[placeholder=\"请设置密码\"]"
),
WAIT_TIME
);
rpa
().
refresh
();
rpa
().
refresh
();
}
catch
(
NoSuchElementException
ignore
)
{
}
catch
(
NoSuchElementException
ignore
)
{
}
}
...
@@ -194,7 +194,7 @@ public class LoginFunction extends BaseFunction {
...
@@ -194,7 +194,7 @@ public class LoginFunction extends BaseFunction {
protected
void
tryLogout
()
{
protected
void
tryLogout
()
{
// 2. 尝试进行退出操作
// 2. 尝试进行退出操作
try
{
try
{
rpa
().
waitElement
(
By
.
cssSelector
(
"div.user span.userName span.userNameInfo"
),
WAIT_TIME
);
rpa
().
waitElement
LoadAndVisible
(
By
.
cssSelector
(
"div.user span.userName span.userNameInfo"
),
WAIT_TIME
);
// 通过脚本进行退出操作
// 通过脚本进行退出操作
rpa
().
newHar
();
rpa
().
newHar
();
rpa
().
executeJavaScript
(
"""
rpa
().
executeJavaScript
(
"""
...
...
src/main/java/com/greatchn/etax/tianjin/SessionFunction.java
View file @
c8184bc2
...
@@ -29,7 +29,7 @@ public class SessionFunction extends BaseFunction {
...
@@ -29,7 +29,7 @@ public class SessionFunction extends BaseFunction {
rpa
().
refresh
();
rpa
().
refresh
();
rpa
().
sleep
(
SLEEP_TIME
);
rpa
().
sleep
(
SLEEP_TIME
);
}
}
rpa
().
waitElement
(
By
.
cssSelector
(
"div.user span.userName span.userNameInfo"
),
WAIT_TIME
);
rpa
().
waitElement
LoadAndVisible
(
By
.
cssSelector
(
"div.user span.userName span.userNameInfo"
),
WAIT_TIME
);
var
val
=
rpa
().
executeJavaScript
(
"""
var
val
=
rpa
().
executeJavaScript
(
"""
return (function() {
return (function() {
try {
try {
...
...
src/main/java/com/greatchn/etax/tianjin/SwitchTaxpayerFunction.java
View file @
c8184bc2
...
@@ -101,7 +101,7 @@ public class SwitchTaxpayerFunction extends BaseFunction {
...
@@ -101,7 +101,7 @@ public class SwitchTaxpayerFunction extends BaseFunction {
}
}
}
}
// 8. 等待返回电子税务局首页
// 8. 等待返回电子税务局首页
rpa
().
waitElement
(
By
.
cssSelector
(
"div.user span.userName span.userNameInfo"
),
WAIT_TIME
);
rpa
().
waitElement
LoadAndVisible
(
By
.
cssSelector
(
"div.user span.userName span.userNameInfo"
),
WAIT_TIME
);
// 8.1 补充……因为电子税务局的 bug,需要清理电子税务局内部网上办税子系统的 Cookies
// 8.1 补充……因为电子税务局的 bug,需要清理电子税务局内部网上办税子系统的 Cookies
try
{
try
{
rpa
().
removeCookies
(
"WSBSSESSION"
);
rpa
().
removeCookies
(
"WSBSSESSION"
);
...
...
src/main/java/com/greatchn/kits/ChromeKits.java
View file @
c8184bc2
...
@@ -7,6 +7,7 @@ import org.apache.commons.lang3.StringUtils;
...
@@ -7,6 +7,7 @@ import org.apache.commons.lang3.StringUtils;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.File
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.net.Socket
;
import
java.nio.charset.Charset
;
import
java.nio.charset.Charset
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.Date
;
...
@@ -16,9 +17,33 @@ public class ChromeKits {
...
@@ -16,9 +17,33 @@ public class ChromeKits {
public
static
void
killBrowser
(){
public
static
void
killBrowser
(){
ProcessKits
.
killProcess
(
Arrays
.
asList
(
"firefox"
,
"geckodriver"
,
"crashreporter"
,
"chromedriver"
,
"chrome"
));
ProcessKits
.
killProcess
(
Arrays
.
asList
(
"firefox"
,
"geckodriver"
,
"crashreporter"
,
"chromedriver"
,
"chrome"
));
}
}
public
static
boolean
chromeIsRunning
(){
Socket
socket
=
null
;
try
{
socket
=
new
Socket
(
"127.0.0.1"
,
9222
);
return
true
;
}
catch
(
Exception
ex
){
return
false
;
}
finally
{
try
{
socket
.
close
();
}
catch
(
Exception
ex
){
}
}
}
//创建浏览器
//创建浏览器
public
static
String
utilBrowserStart
(
String
cmd
)
public
static
String
utilBrowserStart
(
String
cmd
)
{
{
if
(
chromeIsRunning
()==
true
){
return
""
;
}
Runtime
rt
=
Runtime
.
getRuntime
();
Runtime
rt
=
Runtime
.
getRuntime
();
try
try
{
{
...
...
src/main/java/com/greatchn/rpa/RpaCore.java
View file @
c8184bc2
...
@@ -16,10 +16,8 @@ import net.lightbody.bmp.client.ClientUtil;
...
@@ -16,10 +16,8 @@ import net.lightbody.bmp.client.ClientUtil;
import
net.lightbody.bmp.core.har.Har
;
import
net.lightbody.bmp.core.har.Har
;
import
net.lightbody.bmp.proxy.CaptureType
;
import
net.lightbody.bmp.proxy.CaptureType
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.openqa.selenium.Alert
;
import
org.openqa.selenium.*
;
import
org.openqa.selenium.By
;
import
org.openqa.selenium.NoSuchElementException
;
import
org.openqa.selenium.NoSuchElementException
;
import
org.openqa.selenium.WebElement
;
import
org.openqa.selenium.chrome.ChromeDriver
;
import
org.openqa.selenium.chrome.ChromeDriver
;
import
org.openqa.selenium.chrome.ChromeDriverService
;
import
org.openqa.selenium.chrome.ChromeDriverService
;
import
org.openqa.selenium.chrome.ChromeOptions
;
import
org.openqa.selenium.chrome.ChromeOptions
;
...
@@ -35,10 +33,11 @@ import java.text.MessageFormat;
...
@@ -35,10 +33,11 @@ import java.text.MessageFormat;
import
java.time.Duration
;
import
java.time.Duration
;
import
java.util.*
;
import
java.util.*
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentLinkedQueue
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.concurrent.atomic.AtomicReference
;
import
java.util.concurrent.atomic.AtomicReference
;
import
java.util.function.BooleanSupplier
;
import
java.util.function.Supplier
;
/**
/**
...
@@ -277,7 +276,7 @@ public class RpaCore {
...
@@ -277,7 +276,7 @@ public class RpaCore {
*/
*/
public
Har
newHar
()
{
public
Har
newHar
()
{
//Mobo模式开启
//Mobo模式开启
if
(
this
.
config
.
isNetworkProxy
()
==
false
)
{
if
(
this
.
config
.
isNetworkProxy
()
==
false
)
{
if
(!
this
.
config
.
getRpa
().
getProxy
().
isEnable
())
{
if
(!
this
.
config
.
getRpa
().
getProxy
().
isEnable
())
{
throw
new
IllegalStateException
(
"当前 Robot 对象没有开启网络代理!"
);
throw
new
IllegalStateException
(
"当前 Robot 对象没有开启网络代理!"
);
}
}
...
@@ -295,11 +294,10 @@ public class RpaCore {
...
@@ -295,11 +294,10 @@ public class RpaCore {
* 结束当前网络监听集合
* 结束当前网络监听集合
*/
*/
public
void
stopHar
()
{
public
void
stopHar
()
{
if
(
this
.
config
.
isNetworkProxy
()
==
false
)
{
if
(
this
.
config
.
isNetworkProxy
()
==
false
)
{
this
.
proxy
.
waitForQuiescence
(
QUIT_PERIOD
,
QUIT_PERIOD
,
TimeUnit
.
SECONDS
);
this
.
proxy
.
waitForQuiescence
(
QUIT_PERIOD
,
QUIT_PERIOD
,
TimeUnit
.
SECONDS
);
return
;
return
;
}
}
else
{
else
{
this
.
closeNetWork
();
this
.
closeNetWork
();
}
}
}
}
...
@@ -323,7 +321,7 @@ public class RpaCore {
...
@@ -323,7 +321,7 @@ public class RpaCore {
*/
*/
public
String
listen
(
String
uri
,
long
timeout
)
{
public
String
listen
(
String
uri
,
long
timeout
)
{
//Mobo模式的network
//Mobo模式的network
if
(
this
.
config
.
isNetworkProxy
()
==
false
)
{
if
(
this
.
config
.
isNetworkProxy
()
==
false
)
{
final
var
outTime
=
System
.
currentTimeMillis
()
+
timeout
*
1000
;
final
var
outTime
=
System
.
currentTimeMillis
()
+
timeout
*
1000
;
try
{
try
{
while
(
true
)
{
while
(
true
)
{
...
@@ -346,8 +344,8 @@ public class RpaCore {
...
@@ -346,8 +344,8 @@ public class RpaCore {
}
}
}
}
//chrome的network
//chrome的network
else
{
else
{
return
this
.
listenNetWorkCenter
(
uri
,
timeout
*
1000L
);
return
this
.
listenNetWorkCenter
(
uri
,
timeout
*
1000L
);
}
}
}
}
...
@@ -474,40 +472,8 @@ public class RpaCore {
...
@@ -474,40 +472,8 @@ public class RpaCore {
/**
/**
* 默认元素等待时间 (60 秒)
* 默认元素等待时间 (60 秒)
*/
*/
private
static
final
long
DEFAULT_WAIT_TIME
=
6
0
;
private
static
final
long
DEFAULT_WAIT_TIME
=
1
0
;
/**
* 判断元素是否可以点击
*
* @param element 要判断的元素
* @return 点击状态
*/
private
boolean
canClick
(
WebElement
element
)
{
try
{
var
wait
=
new
WebDriverWait
(
this
.
driver
,
Duration
.
of
(
5
,
TimeUnit
.
SECONDS
.
toChronoUnit
()));
wait
.
until
(
ExpectedConditions
.
elementToBeClickable
(
element
));
return
true
;
}
catch
(
Exception
ignored
)
{
}
return
false
;
}
/**
* 等待元素加载并可见
*
* @param by 查询元素
* @param waitTime 等待时间(单位:秒)
*/
public
void
waitElement
(
By
by
,
long
waitTime
)
{
try
{
var
wait
=
new
WebDriverWait
(
this
.
driver
,
Duration
.
of
(
waitTime
,
TimeUnit
.
SECONDS
.
toChronoUnit
()));
wait
.
until
(
ExpectedConditions
.
presenceOfElementLocated
(
by
));
wait
.
until
(
ExpectedConditions
.
visibilityOfElementLocated
(
by
));
}
catch
(
Exception
ex
)
{
// 如果等待元素超时,改为抛出元素不存在异常
throw
new
NoSuchElementException
(
by
.
toString
());
}
}
/**
/**
* 查找元素
* 查找元素
...
@@ -529,7 +495,7 @@ public class RpaCore {
...
@@ -529,7 +495,7 @@ public class RpaCore {
* @throws NoSuchElementException,元素不存在或可用超时抛出
* @throws NoSuchElementException,元素不存在或可用超时抛出
*/
*/
public
WebElement
findElement
(
By
by
,
long
waitTime
)
{
public
WebElement
findElement
(
By
by
,
long
waitTime
)
{
this
.
waitElement
(
by
,
waitTime
);
this
.
waitElement
LoadAndVisible
(
by
,
waitTime
);
return
this
.
driver
.
findElement
(
by
);
return
this
.
driver
.
findElement
(
by
);
}
}
...
@@ -566,7 +532,7 @@ public class RpaCore {
...
@@ -566,7 +532,7 @@ public class RpaCore {
* @throws NoSuchElementException,元素不存在或可用超时抛出
* @throws NoSuchElementException,元素不存在或可用超时抛出
*/
*/
public
List
<
WebElement
>
findElements
(
By
by
,
long
waitTime
)
{
public
List
<
WebElement
>
findElements
(
By
by
,
long
waitTime
)
{
this
.
waitElement
(
by
,
waitTime
);
this
.
waitElement
LoadAndVisible
(
by
,
waitTime
);
return
this
.
driver
.
findElements
(
by
);
return
this
.
driver
.
findElements
(
by
);
}
}
...
@@ -608,7 +574,6 @@ public class RpaCore {
...
@@ -608,7 +574,6 @@ public class RpaCore {
return
this
.
findElementWithInnerText
(
innerText
,
this
.
findElements
(
element
,
by
));
return
this
.
findElementWithInnerText
(
innerText
,
this
.
findElements
(
element
,
by
));
}
}
/**
/**
* 在元素列表中查找内部文本符合条件的元素
* 在元素列表中查找内部文本符合条件的元素
*
*
...
@@ -627,6 +592,312 @@ public class RpaCore {
...
@@ -627,6 +592,312 @@ public class RpaCore {
}
}
throw
new
NoSuchElementException
(
"inner text: "
+
innerText
);
throw
new
NoSuchElementException
(
"inner text: "
+
innerText
);
}
}
//==============findElementsLoad begin=====================
/**
* @param parentElement 父元素
* @param childBy 在父元素的范围内查找子元素
* @param waitTime
* @return
*/
public
List
<
WebElement
>
findElementsLoadRoot
(
WebElement
parentElement
,
By
childBy
,
long
waitTime
,
String
throwEx
)
{
try
{
//忽略可见
var
wait
=
new
WebDriverWait
(
this
.
driver
,
Duration
.
of
(
waitTime
,
TimeUnit
.
SECONDS
.
toChronoUnit
()));
wait
.
until
(
ExpectedConditions
.
presenceOfNestedElementLocatedBy
(
parentElement
,
childBy
));
return
parentElement
.
findElements
(
childBy
);
}
catch
(
Exception
e
)
{
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
e
.
getMessage
());
}
}
return
null
;
}
public
List
<
WebElement
>
findElementsLoad
(
WebElement
parentElement
,
By
childBy
,
String
throwEx
)
{
//忽略可见
return
this
.
findElementsLoadRoot
(
parentElement
,
childBy
,
DEFAULT_WAIT_TIME
,
throwEx
);
}
public
List
<
WebElement
>
findElementsLoadRoot
(
By
by
,
long
waitTime
,
String
throwEx
)
{
try
{
//忽略可见
this
.
waitElementLoad
(
by
,
waitTime
,
throwEx
);
return
this
.
driver
.
findElements
(
by
);
}
catch
(
Exception
e
)
{
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
e
.
getMessage
());
}
}
return
null
;
}
/**
* @param by
* @return
*/
public
List
<
WebElement
>
findElementsLoad
(
By
by
,
String
throwEx
)
{
return
findElementsLoadRoot
(
by
,
DEFAULT_WAIT_TIME
,
throwEx
);
}
//==============findElementsLoad end=====================
//==================findElementAndIgnoreVisible begin=====================
public
WebElement
findElementLoadRoot
(
By
by
,
long
waitTime
,
String
throwEx
)
{
try
{
//忽略可见
this
.
waitElementLoad
(
by
,
waitTime
,
throwEx
);
return
this
.
driver
.
findElement
(
by
);
}
catch
(
Exception
e
)
{
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
e
.
getMessage
());
}
}
return
null
;
}
public
WebElement
findElementLoadRoot
(
WebElement
parentElement
,
By
childBy
,
long
waitTime
,
String
throwEx
)
{
try
{
//忽略可见
var
wait
=
new
WebDriverWait
(
this
.
driver
,
Duration
.
of
(
waitTime
,
TimeUnit
.
SECONDS
.
toChronoUnit
()));
wait
.
until
(
ExpectedConditions
.
presenceOfNestedElementLocatedBy
(
parentElement
,
childBy
));
return
parentElement
.
findElement
(
childBy
);
}
catch
(
Exception
e
)
{
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
e
.
getMessage
());
}
}
return
null
;
}
public
WebElement
findElementLoad
(
WebElement
parentElement
,
By
childBy
,
String
throwEx
)
{
return
findElementLoadRoot
(
parentElement
,
childBy
,
DEFAULT_WAIT_TIME
,
throwEx
);
}
//==================findElementAndIgnoreVisible end=====================
//===============================waitElement begin===========================
/**
* 判断元素是否可以点击
*
* @param element 要判断的元素
* @return 点击状态
*/
public
boolean
waitElementCanClick
(
WebElement
element
)
{
try
{
var
wait
=
new
WebDriverWait
(
this
.
driver
,
Duration
.
of
(
5
,
TimeUnit
.
SECONDS
.
toChronoUnit
()));
wait
.
until
(
ExpectedConditions
.
elementToBeClickable
(
element
));
return
true
;
}
catch
(
Exception
ignored
)
{
System
.
out
.
println
(
ignored
.
getMessage
());
}
return
false
;
}
/**
* 等待元素加载并可见
*
* @param by 查询元素
* @param waitTime 等待时间(单位:秒)
*/
public
void
waitElementLoadAndVisible
(
By
by
,
long
waitTime
)
{
try
{
var
wait
=
new
WebDriverWait
(
this
.
driver
,
Duration
.
of
(
waitTime
,
TimeUnit
.
SECONDS
.
toChronoUnit
()));
wait
.
until
(
ExpectedConditions
.
presenceOfElementLocated
(
by
));
wait
.
until
(
ExpectedConditions
.
visibilityOfElementLocated
(
by
));
}
catch
(
Exception
ex
)
{
// 如果等待元素超时,改为抛出元素不存在异常
throw
new
NoSuchElementException
(
by
.
toString
());
}
}
/**
* 等待元素是否被加载
*
* @param by
* @param waitTime
*/
public
boolean
waitElementLoad
(
By
by
,
long
waitTime
,
String
throwEx
)
{
try
{
var
wait
=
new
WebDriverWait
(
this
.
driver
,
Duration
.
of
(
waitTime
,
TimeUnit
.
SECONDS
.
toChronoUnit
()));
wait
.
until
(
ExpectedConditions
.
presenceOfElementLocated
(
by
));
return
true
;
}
catch
(
Exception
ex
)
{
// 如果等待元素超时,改为抛出元素不存在异常
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
by
.
toString
());
}
}
return
false
;
}
public
WebElement
waitElementLoad
(
Supplier
<
WebElement
>
function
,
long
timeout
,
String
throwEx
)
{
Date
start
=
new
Date
();
while
(
true
)
{
WebElement
webElement
=
function
.
get
();
if
(
webElement
!=
null
)
{
return
webElement
;
}
this
.
sleep
(
10
);
if
(
System
.
currentTimeMillis
()
-
start
.
getTime
()
>
timeout
)
{
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
"waitElementLoad timeout"
);
}
return
null
;
}
}
}
/**
* @param waitTime
*/
public
boolean
waitElementVisible
(
WebElement
element
,
long
waitTime
,
String
throwEx
)
{
try
{
var
wait
=
new
WebDriverWait
(
this
.
driver
,
Duration
.
of
(
waitTime
,
TimeUnit
.
SECONDS
.
toChronoUnit
()));
wait
.
until
(
ExpectedConditions
.
visibilityOf
(
element
));
return
true
;
}
catch
(
Exception
ex
)
{
// 如果等待元素超时,改为抛出元素不存在异常
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
ex
.
getMessage
());
}
}
return
false
;
}
//===============================waitElement end===========================
//====================clickAndExcept begin=========================
/**
* 点某个元素之后期望exceptFunction返回true
*
* @param clickElement
* @param exceptFunction 期望的匿名函数
* @param exceptFunctionTimeout 表达式超时时间
*/
public
boolean
clickAndExceptRoot
(
WebElement
clickElement
,
BooleanSupplier
exceptFunction
,
Long
exceptFunctionTimeout
,
String
throwEx
)
{
//点击
this
.
click
(
clickElement
);
//等待期望元素出现
Date
start
=
new
Date
();
while
(
true
)
{
boolean
asBoolean
=
exceptFunction
.
getAsBoolean
();
if
(
Objects
.
equals
(
asBoolean
,
true
))
{
return
true
;
}
if
(
Objects
.
equals
(
asBoolean
,
false
))
{
continue
;
}
if
(
System
.
currentTimeMillis
()
-
start
.
getTime
()
>
exceptFunctionTimeout
)
{
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
"clickAndExceptRoot timeout"
);
}
return
false
;
}
this
.
sleep
(
10
);
}
}
//====================clickAndExcept end=========================
public
boolean
clickAndExcept
(
By
clickBy
,
BooleanSupplier
exceptFunction
,
String
throwEx
)
{
WebElement
clickElement
=
this
.
findElementLoadRoot
(
clickBy
,
DEFAULT_WAIT_TIME
,
throwEx
);
return
this
.
clickAndExceptRoot
(
clickElement
,
exceptFunction
,
DEFAULT_WAIT_TIME
,
throwEx
);
}
public
boolean
clickAndExcept
(
WebElement
clickElement
,
BooleanSupplier
exceptFunction
,
String
throwEx
)
{
return
this
.
clickAndExceptRoot
(
clickElement
,
exceptFunction
,
DEFAULT_WAIT_TIME
,
throwEx
);
}
//========================clickByFunc begin==========================
/**
* 通过function进行点击元素
*
* @param function 如何点击元素的行为的匿名函数
* @param timeout
* @return
*/
public
boolean
clickByFuncRoot
(
Supplier
<
WebElement
>
function
,
long
timeout
,
String
throwEx
)
{
try
{
WebElement
webElement
=
this
.
waitElementLoad
(
function
,
timeout
,
throwEx
);
this
.
click
(
webElement
);
return
true
;
}
catch
(
Exception
e
)
{
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
e
.
getMessage
());
}
return
false
;
}
}
public
boolean
clickByFunc
(
Supplier
<
WebElement
>
function
,
String
throwEx
)
{
return
this
.
clickByFuncRoot
(
function
,
DEFAULT_WAIT_TIME
,
throwEx
);
}
/**
* 通过function进行点击元素 之后 期望 exceptFunction为true
*
* @param function 如何点击元素的行为的匿名函数
* @param timeout
* @param exceptFunction 点击之后的结果
* @param exceptFunctionTimeout
* @return
*/
public
boolean
clickByFunc
(
Supplier
<
WebElement
>
function
,
long
timeout
,
BooleanSupplier
exceptFunction
,
Long
exceptFunctionTimeout
,
String
throwEx
)
{
try
{
//点击
this
.
clickByFuncRoot
(
function
,
timeout
,
throwEx
);
//等待期望结果出现
Date
start
=
new
Date
();
while
(
true
)
{
boolean
asBoolean
=
exceptFunction
.
getAsBoolean
();
//期望结果出现
if
(
Objects
.
equals
(
asBoolean
,
true
))
{
return
true
;
}
if
(
Objects
.
equals
(
asBoolean
,
false
))
{
continue
;
}
//期望结果超时
if
(
System
.
currentTimeMillis
()
-
start
.
getTime
()
>
exceptFunctionTimeout
)
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
"clickByFunc timeout"
);
}
this
.
sleep
(
10
);
}
}
catch
(
Exception
e
)
{
if
(
StringUtils
.
isNotBlank
(
throwEx
))
{
throw
new
NoSuchElementException
(
throwEx
+
":"
+
e
.
getMessage
());
}
return
false
;
}
}
public
boolean
clickByFunc
(
Supplier
<
WebElement
>
function
,
BooleanSupplier
exceptFunction
,
String
throwEx
)
{
return
this
.
clickByFunc
(
function
,
DEFAULT_WAIT_TIME
,
exceptFunction
,
DEFAULT_WAIT_TIME
,
throwEx
);
}
//========================clickByFunc end==========================
public
boolean
jsClick
(
WebElement
element
)
{
try
{
this
.
executeJavaScript
(
"arguements[0].click()"
,
Object
.
class
,
element
);
return
true
;
}
catch
(
Exception
e
)
{
return
false
;
}
}
/* ------------------------- 页面元素交互 ------------------------- */
/* ------------------------- 页面元素交互 ------------------------- */
/**
/**
...
@@ -714,13 +985,36 @@ public class RpaCore {
...
@@ -714,13 +985,36 @@ public class RpaCore {
return
;
return
;
}
}
this
.
scrollTo
(
element
);
this
.
scrollTo
(
element
);
if
(!
this
.
config
.
getRpa
().
isSimulate
()
&&
this
.
canClick
(
element
))
{
this
.
addBorder
(
element
);
this
.
sleep
(
2000
);
if
(!
this
.
config
.
getRpa
().
isSimulate
()
&&
this
.
waitElementCanClick
(
element
))
{
element
.
click
();
element
.
click
();
}
else
{
}
else
{
this
.
moveTo
(
element
);
this
.
moveTo
(
element
);
WinApi
.
leftClick
();
WinApi
.
leftClick
();
}
}
}
this
.
clearBorder
(
element
);
}
//public void clickWhile(WebElement element) {
// if (Objects.isNull(element)) {
// return;
// }
// Date start = new Date();
// while (true) {
// this.scrollTo(element);
// this.sleep(100);
// this.addBorder(element);
// if (this.waitElementVisible(element, 2L)) {
// element.click();
// break;
// }
// if (System.currentTimeMillis() - start.getTime() > 2000) {
// this.jsClick(element);
// }
// }
// this.clearBorder(element);
//}
/**
/**
* 向指定元素输出文本
* 向指定元素输出文本
...
@@ -924,6 +1218,7 @@ public class RpaCore {
...
@@ -924,6 +1218,7 @@ public class RpaCore {
/**
/**
* 使用selenium方式拖拽元素一定的偏移距离
* 使用selenium方式拖拽元素一定的偏移距离
*
* @param dragHandler
* @param dragHandler
* @param xoffset
* @param xoffset
*/
*/
...
@@ -946,10 +1241,10 @@ public class RpaCore {
...
@@ -946,10 +1241,10 @@ public class RpaCore {
action
.
release
().
build
().
perform
();
action
.
release
().
build
().
perform
();
}
}
public
void
drag
(
WebElement
dragHandler
,
WebElement
dragHandlerContiner
){
public
void
drag
(
WebElement
dragHandler
,
WebElement
dragHandlerContiner
)
{
// Rectangle handlerRect = this.sumElementRectangle(dragHandlerContiner);
// Rectangle handlerRect = this.sumElementRectangle(dragHandlerContiner);
int
xoffset
=
dragHandlerContiner
.
getRect
().
width
;
int
xoffset
=
dragHandlerContiner
.
getRect
().
width
;
this
.
dragCenter
(
dragHandler
,
xoffset
);
this
.
dragCenter
(
dragHandler
,
xoffset
);
}
}
/* ------------------------- 辅助方法 ------------------------- */
/* ------------------------- 辅助方法 ------------------------- */
...
@@ -1091,8 +1386,7 @@ public class RpaCore {
...
@@ -1091,8 +1386,7 @@ public class RpaCore {
devTools
.
addListener
(
Network
.
responseReceived
(),
res
->
{
devTools
.
addListener
(
Network
.
responseReceived
(),
res
->
{
try
{
try
{
networkMap
.
put
(
res
.
getResponse
().
getUrl
(),
devTools
.
send
(
Network
.
getResponseBody
(
res
.
getRequestId
())).
getBody
());
networkMap
.
put
(
res
.
getResponse
().
getUrl
(),
devTools
.
send
(
Network
.
getResponseBody
(
res
.
getRequestId
())).
getBody
());
}
}
catch
(
Exception
ex
)
{
catch
(
Exception
ex
){
}
}
});
});
...
@@ -1188,5 +1482,29 @@ public class RpaCore {
...
@@ -1188,5 +1482,29 @@ public class RpaCore {
public
void
resetInputState
()
{
public
void
resetInputState
()
{
this
.
driver
.
resetInputState
();
this
.
driver
.
resetInputState
();
}
}
public
void
addBorder
(
WebElement
element
)
{
try
{
this
.
addAttribute
(
element
,
"style"
,
"border:1px solid red"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
public
void
clearBorder
(
WebElement
element
)
{
try
{
this
.
addAttribute
(
element
,
"style"
,
"border:0px solid red"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
public
void
addAttribute
(
WebElement
element
,
String
attributeName
,
String
atributeValue
)
{
if
(
element
==
null
)
{
return
;
}
String
script
=
MessageFormat
.
format
(
"arguments[0].setAttribute(''{0}'', ''{1}'')"
,
attributeName
,
atributeValue
);
this
.
executeJavaScript
(
script
,
Object
.
class
,
element
);
}
}
}
src/main/resources/rpa.yaml
View file @
c8184bc2
...
@@ -21,13 +21,19 @@ useSeleniumDrag: true
...
@@ -21,13 +21,19 @@ useSeleniumDrag: true
networkProxy
:
true
networkProxy
:
true
#是否使用selenium来自动启动浏览器
#是否使用selenium来自动启动浏览器
remoteDebugging
:
fals
e
remoteDebugging
:
tru
e
remoteDebuggingPort
:
9222
remoteDebuggingPort
:
9222
remoteDebuggingIP
:
"
127.0.0.1"
remoteDebuggingIP
:
"
127.0.0.1"
# cmd.exe /c cmd 是执行完cmd命令后关闭命令窗口;
# cmd.exe /k cmd 是执行完cmd命令后不关闭命令窗口;
# cmd.exe /c start cmd 会打开一个新窗口后执行cmd指令,原窗口会关闭;
# cmd.exe /k start cmd 会打开一个新窗口后执行cmd指令,原窗口不会关闭;
# cmd.exe /k start /b cmd 会打开一个隐藏的窗口执行cmd指令,原窗口不会关闭
# cmd /b /Google/Chrome/Application/chrome.exe --remote-debugging-port=9222 --user-data-dir=/selenium/ChromeProfile --allow-pre-commit-input --disable-background-networking --disable-backgrounding-occluded-windows --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-infobars --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-blink-features=ShadowDOMV0 --enable-logging --ignore-certificate-errors --log-level=3 --no-service-autorun --password-store=basic --start-maximized --test-type --use-mock-keychain --flag-switches-begin --flag-switches-end
# cmd /b /Google/Chrome/Application/chrome.exe --remote-debugging-port=9222 --user-data-dir=/selenium/ChromeProfile --allow-pre-commit-input --disable-background-networking --disable-backgrounding-occluded-windows --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-infobars --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-blink-features=ShadowDOMV0 --enable-logging --ignore-certificate-errors --log-level=3 --no-service-autorun --password-store=basic --start-maximized --test-type --use-mock-keychain --flag-switches-begin --flag-switches-end
#remoteDebuggingCmd: "/Google/Chrome/Application/chrome.exe --remote-debugging-port=9222 --user-data-dir=/selenium/ChromeProfile --allow-pre-commit-input --disable-background-networking --disable-backgrounding-occluded-windows --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-infobars --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-blink-features=ShadowDOMV0 --enable-logging --ignore-certificate-errors --log-level=3 --no-service-autorun --password-store=basic --start-maximized --test-type --use-mock-keychain --flag-switches-begin --flag-switches-end"
#remoteDebuggingCmd: "/Google/Chrome/Application/chrome.exe --remote-debugging-port=9222 --user-data-dir=/selenium/ChromeProfile --allow-pre-commit-input --disable-background-networking --disable-backgrounding-occluded-windows --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-infobars --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-blink-features=ShadowDOMV0 --enable-logging --ignore-certificate-errors --log-level=3 --no-service-autorun --password-store=basic --start-maximized --test-type --use-mock-keychain --flag-switches-begin --flag-switches-end"
#remoteDebuggingCmd: "cmd.exe /c start /b C:\\Google\\Chrome\\Application\\chrome.bat"
#remoteDebuggingCmd: "cmd.exe /c start /b C:\\Google\\Chrome\\Application\\chrome.bat"
remoteDebuggingCmd
:
"
cmd.exe
/c
start
/b
C
:
\\
Google
\\
Chrome
\\
Application
\\
chrome.bat"
remoteDebuggingCmd
:
"
cmd.exe
/c
start
/b
D
:
\\
Google
\\
Chrome
\\
Application
\\
chrome.bat"
...
...
src/test/java/DriverTest.java
View file @
c8184bc2
import
cn.hutool.core.collection.CollectionUtil
;
import
com.greatchn.rpa.RpaCore
;
import
com.greatchn.rpa.RpaCore
;
import
com.greatchn.rpa.config.RpaConfig
;
import
com.greatchn.rpa.config.RpaConfig
;
import
com.greatchn.yzh.TaskExecutor
;
import
org.openqa.selenium.By
;
import
org.openqa.selenium.By
;
import
org.openqa.selenium.WebElement
;
import
org.openqa.selenium.WebElement
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Objects
;
public
class
DriverTest
{
public
class
DriverTest
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
var
config
=
RpaConfig
.
build
();
var
config
=
RpaConfig
.
build
();
RpaCore
rpa
=
new
RpaCore
(
config
);
RpaCore
rpa
=
new
RpaCore
(
config
);
rpa
.
visit
(
"chrome://version/"
);
//
rpa.visit("chrome://version/");
//network
//network
//rpa.enableNetWork();
//rpa.enableNetWork();
rpa
.
visit
(
"https://www.baidu.com"
);
//
rpa.visit("https://www.baidu.com");
System
.
out
.
println
(
rpa
.
listenNetWorkCenter
(
"sugrec"
,
10000L
));
//
System.out.println(rpa.listenNetWorkCenter("sugrec", 10000L));
//drag
//drag
rpa
.
visit
(
"https://tpass.tianjin.chinatax.gov.cn:8443/#/login?redirect_uri=https%3A%2F%2Fetax.tianjin.chinatax.gov.cn%2Foutsider%2Fsso%2FmainPage.do&client_id=x3ef652ax3294xz6beefnf5zadbf66cc&response_type=code&state=test"
);
//rpa.visit("https://tpass.tianjin.chinatax.gov.cn:8443/#/login?redirect_uri=https%3A%2F%2Fetax.tianjin.chinatax.gov.cn%2Foutsider%2Fsso%2FmainPage.do&client_id=x3ef652ax3294xz6beefnf5zadbf66cc&response_type=code&state=test");
var
dragHandler
=
rpa
.
findElement
(
By
.
cssSelector
(
".handler.animate"
),
1
);
//var dragHandler = rpa.findElement(By.cssSelector(".handler.animate"), 1);
//
//WebElement continer=rpa.findElement(dragHandler,By.xpath("./.."));
//System.out.println(continer.getText());
//rpa.drag(dragHandler,continer);
djKprq
(
rpa
,
0
,
"2020"
,
"6"
,
"1"
);
djKprq
(
rpa
,
1
,
"2021"
,
"2"
,
"15"
);
}
public
static
void
djKprq
(
RpaCore
rpa
,
Integer
type
,
String
year
,
String
month
,
String
day
)
{
//点击的日期面板(点击年,月,日等控件的父元素)
String
parentPath
=
".//div[@class='t-popup t-date-picker__panel-container'][@data-popper-placement]"
;
//点击年下拉框
String
header_controller_year
=
".//div[@class='t-select__wrap t-date-picker__header-controller-year']//input"
;
//点击年份的xpath
String
click_year_xpath
=
String
.
format
(
".//div[@class='t-popup t-select__dropdown t-date-picker__header-controller-year-popup']//ul/li//span[text()='%s']"
,
year
);
//点击月下拉框
String
header_controller_month
=
".//div[@class='t-select__wrap t-date-picker__header-controller-month']//input"
;
//点击确定的月
String
click_month_xpath
=
String
.
format
(
".//div[@class='t-popup t-select__dropdown t-date-picker__header-controller-month-popup']//ul/li//span[text()='%s 月']"
,
month
);
//点击确定的日
String
click_day_xpath
=
String
.
format
(
".//div[@class='t-date-picker__table']//div[@class='t-date-picker__cell-inner'][text()=%s]"
,
day
);
List
<
WebElement
>
parent
=
new
ArrayList
<>();
if
(
Objects
.
equals
(
0
,
type
))
{
//点击开票日期 起
rpa
.
clickAndExcept
(
By
.
cssSelector
(
"input[placeholder='开票日期起']"
),
()
->
{
List
<
WebElement
>
tmpList
=
rpa
.
findElementsLoad
(
By
.
xpath
(
parentPath
),
"未找到开票日期起"
);
if
(
CollectionUtil
.
isEmpty
(
tmpList
))
{
return
false
;
}
parent
.
add
(
tmpList
.
get
(
0
));
return
true
;
},
"未找到开票日期起"
);
}
else
{
//点击开票日期 止
rpa
.
clickAndExcept
(
By
.
cssSelector
(
"input[placeholder='开票日期止']"
),
()
->
{
List
<
WebElement
>
tmpList
=
rpa
.
findElementsLoad
(
By
.
xpath
(
parentPath
),
"未找到开票日期止"
);
if
(
CollectionUtil
.
isEmpty
(
tmpList
))
{
return
false
;
}
parent
.
add
(
tmpList
.
get
(
0
));
return
true
;
},
"未找到开票日期止"
);
}
System
.
out
.
println
(
"点击年下拉框"
);
//点击年下拉框
rpa
.
clickByFunc
(()
->
{
WebElement
yearDropDownList
=
rpa
.
findElementLoad
(
parent
.
get
(
0
),
By
.
xpath
(
header_controller_year
),
"点击年下拉框失败"
);
return
yearDropDownList
;
},
"点击年下拉框失败"
);
//rpa.sleep(10000);
System
.
out
.
println
(
"点击确定的年"
);
//点击确定的年
rpa
.
clickByFunc
(()
->
{
WebElement
clickYear
=
rpa
.
findElementLoad
(
parent
.
get
(
0
),
By
.
xpath
(
click_year_xpath
),
"点击确定的年失败"
);
return
clickYear
;
},
"点击确定的年失败"
);
//rpa.sleep(1000);
//点击月下拉框
rpa
.
clickByFunc
(()
->
{
WebElement
monthDropDownList
=
rpa
.
findElementLoad
(
parent
.
get
(
0
),
By
.
xpath
(
header_controller_month
),
"点击月下拉框失败"
);
return
monthDropDownList
;
},
"点击月下拉框失败"
);
//rpa.sleep(1000);
//点击确定的月
rpa
.
clickByFunc
(()
->
{
WebElement
clickMonth
=
rpa
.
findElementLoad
(
parent
.
get
(
0
),
By
.
xpath
(
click_month_xpath
),
"点击确定的月失败"
);
return
clickMonth
;
},
"点击确定的月失败"
);
//rpa.sleep(1000);
WebElement
continer
=
rpa
.
findElement
(
dragHandler
,
By
.
xpath
(
"./.."
));
//点击日
System
.
out
.
println
(
continer
.
getText
());
rpa
.
drag
(
dragHandler
,
continer
);
rpa
.
clickByFunc
(()
->
{
WebElement
clickDay
=
rpa
.
findElementLoad
(
parent
.
get
(
0
),
By
.
xpath
(
click_day_xpath
),
"点击日失败"
);
return
clickDay
;
},
"点击日失败"
);
//rpa.sleep(1000);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment