自动化测试中,等待时间的运用占据了举足轻重的地位,平常我们需要处理很多和时间息息相关的场景,例如:
- 打开新页面,只要特定元素出现而不用等待页面全部加载完成就对其进行操作
- 设置等待某元素出现的时间,超时则抛出异常
- 设置页面加载的时间
- …..
webdriver类中有三个和时间相关的方法:
1.pageLoadTimeout
2.setScriptTimeout
3.implicitlyWait
我们就从这里开始,慢慢揭开他神秘的面纱。
pageLoadTimeout
pageLoadTimeout方法用来设置页面完全加载的超时时间,完全加载即页面全部渲染,异步同步脚本都执行完成。前面的文章都是使用get 方法登录安居客网站,大家应该能感觉到每次打开网页后要等很长一段时间才会进行下一步的操作,那是因为没有设置超时时间而get方法默认是等待页面全部加 载完成才会进入下一步骤,加入将超时时间设置为3S就会中断操作抛出异常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import
java
.
util
.
concurrent
.
TimeUnit
;
import
org
.
openqa
.
selenium
.
By
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
import
org
.
openqa
.
selenium
.
WebElement
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
throws
InterruptedException
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
try
{
//设置超时时间为3S
driver
.
manage
(
)
.
timeouts
(
)
.
pageLoadTimeout
(
3
,
TimeUnit
.
SECONDS
)
;
driver
.
get
(
"http://shanghai.anjuke.com"
)
;
WebElement
input
=
driver
.
findElement
(
By
.
xpath
(
"//input[@id='glb_search0']"
)
)
;
input
.
sendKeys
(
"selenium"
)
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
}
finally
{
Thread
.
sleep
(
3000
)
;
driver
.
quit
(
)
;
}
}
|
ps:如果时间参数为负数,效果没有使用这个方法是一样的,接口注释中有相关说明:”If the timeout is negative, page loads can be indefinite”.
如果想在抛出异常后并不中断而是继续执行下面的操作那该怎么办呢?可以使用try,catch的组合来实现这个功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import
java
.
util
.
concurrent
.
TimeUnit
;
import
org
.
openqa
.
selenium
.
By
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
import
org
.
openqa
.
selenium
.
WebElement
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
throws
InterruptedException
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
try
{
//设置超时时间为3S
driver
.
manage
(
)
.
timeouts
(
)
.
pageLoadTimeout
(
3
,
TimeUnit
.
SECONDS
)
;
driver
.
get
(
"http://shanghai.anjuke.com"
)
;
}
catch
(
Exception
e
)
{
}
finally
{
WebElement
input
=
driver
.
findElement
(
By
.
xpath
(
"//input[@id='glb_search0']"
)
)
;
if
(
input
.
isDisplayed
(
)
)
input
.
sendKeys
(
"selenium"
)
;
}
}
|
这样,当页面加载3S后就会执行下面的操作了。
setScriptTimeout
设置异步脚本的超时时间,用法同pageLoadTimeout一样就不再写了,异步脚本也就是有async属性的JS脚本,可以在页面解析的同时执行。
implicitlyWait
识别对象的超时时间,如果在设置的时间类没有找到就抛出一个NoSuchElement异常,用法参数也是和pageLoadTimeout一样,大家可以自己试验试验。
上文中介绍了如何才能在使用pageLoadTimeout抛出异常的同时继续执行,但是现有的方法还是不完美,如果输入框在3S后没有出现还是会 报错,怎么办呢?机智的同学可以想到用sleep方法来实现,为了方便演示换个更明显的需求来说明。安居客的首页上有个切换城市的链接,当点击城市的时候 就会显示全部的城市以供选择这时display属性就会变化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import
java
.
util
.
concurrent
.
TimeUnit
;
import
org
.
openqa
.
selenium
.
By
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
import
org
.
openqa
.
selenium
.
WebElement
;
import
org
.
openqa
.
selenium
.
interactions
.
Actions
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
throws
InterruptedException
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
try
{
//设置超时时间为3S
driver
.
manage
(
)
.
timeouts
(
)
.
pageLoadTimeout
(
3
,
TimeUnit
.
SECONDS
)
;
driver
.
get
(
"http://shanghai.anjuke.com"
)
;
}
catch
(
Exception
e
)
{
}
finally
{
WebElement
city
=
driver
.
findElement
(
By
.
xpath
(
"//a[@id='switch_apf_id_8']"
)
)
;
WebElement
citys
=
driver
.
findElement
(
By
.
xpath
(
"//div[@id='city-panel']"
)
)
;
Actions
actions
=
new
Actions
(
driver
)
;
actions
.
clickAndHold
(
city
)
.
perform
(
)
;
while
(
citys
.
isDisplayed
(
)
)
{
System
.
out
.
println
(
"sleep"
)
;
Thread
.
sleep
(
3000
)
;
}
Thread
.
sleep
(
3000
)
;
driver
.
quit
(
)
;
}
}
|
执行后会每过3S就会输出一次sleep,但是这样的代码显然不够高大上,而且没有限制会一直无限的等待下去,下面介绍一种高大上的方法,就是until,先看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import
org
.
openqa
.
selenium
.
By
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
import
org
.
openqa
.
selenium
.
WebElement
;
import
org
.
openqa
.
selenium
.
interactions
.
Actions
;
import
org
.
openqa
.
selenium
.
support
.
ui
.
WebDriverWait
;
import
org
.
openqa
.
selenium
.
support
.
ui
.
ExpectedCondition
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
throws
InterruptedException
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
try
{
//设置超时时间为3S
driver
.
manage
(
)
.
timeouts
(
)
.
pageLoadTimeout
(
3
,
TimeUnit
.
SECONDS
)
;
driver
.
get
(
"http://shanghai.anjuke.com"
)
;
}
catch
(
Exception
e
)
{
}
finally
{
WebElement
city
=
driver
.
findElement
(
By
.
xpath
(
"//a[@id='switch_apf_id_8']"
)
)
;
Actions
actions
=
new
Actions
(
driver
)
;
actions
.
clickAndHold
(
city
)
.
perform
(
)
;
//最多等待10S,每2S检查一次
WebDriverWait
wait
=
new
WebDriverWait
(
driver
,
10
,
2000
)
;
wait
.
until
(
new
ExpectedCondition
<Boolean>
(
)
{
public
Boolean
apply
(
WebDriver
driver
)
{
System
.
out
.
println
(
"sleep"
)
;
return
!
driver
.
findElement
(
By
.
xpath
(
"//div[@id='city-panel']"
)
)
.
isDisplayed
(
)
;
}
}
)
;
Thread
.
sleep
(
3000
)
;
driver
.
quit
(
)
;
}
|
转载:http://www.360doc.com/content/14/0913/10/13497042_409106015.shtml