这篇文章主要为大家介绍了python自动化测试selenium的核心技术三种等待方式示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
UI自动化测试过程中,可能会出现因测试环境不稳定、网络慢等情况,如果不做任何处理的话,会出现无法定位到特定元素而报错,导致自动化测试无法顺利执行。
slenium自动化测试中,主要涉及三种等待方式:
1 使用python自带模块time的sleep方式
缺点:即使网络条件较好时,依旧按照预定固定时间等待,一般不建议使用,脚本调试可使用。
示例脚本:
|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from
selenium ``import
webdriver
from
time ``import
sleep
class
TestWait(``object``):
``driver ``=
webdriver.Chrome()
``driver.get(http:``/``/``www.baidu.com)
``def
test_sleep(``self``):
``self``.driver.find_element_by_id(``"kw"``).send_keys(``"sleep test"``)
``# sleep(2) #等待固定时间
``self``.driver.implicitly_wait(``2``) ``# 隐式等待
``self``.driver.find_element_by_id(``"su"``).click()
``self``.driver.quit()
if
__name__ ``=``=
'__main__'``:
``wait``=``TestWait()
``wait.test_sleep()
|
2 隐式等待(implicitly_wait)
隐式等待设置的时间是最长的时间,如果在规定时间内网页加载完成,则执行下一步,否则一直等到时间结束,然后执行下一步。
注意:隐式等待对driver整个周期都起作用,一般在最开始设置一次就可以了。不要当做固定等待,哪里都设置隐式等待。
示例脚本:
|-------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | from
selenium ``import
webdriver
from
time ``import
sleep
class
TestWait(``object``):
``driver ``=
webdriver.Chrome()
``driver.get(http:``/``/``www.baidu.com)
``def
test_sleep(``self``):
``self``.driver.find_element_by_id(``"kw"``).send_keys(``"sleep test"``)
``self``.driver.implicitly_wait(``2``) ``# 隐式等待
``self``.driver.find_element_by_id(``"su"``).click()
``self``.driver.quit()
if
__name__ ``=``=
'__main__'``:
``wait``=``TestWait()
``wait.test_sleep()
|
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036【暗号:csdn999】
3 显示等待(WebDriverWait)
显式等待允许等待条件的发生,所以非常适合在浏览器及其DOM和WebDriver脚本之间同步状态。
需要引入包:from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait参数说明:
WebDriverWait(driver, timeout=3).until(some_condition)
两种方法:until和util_not
场景:
打开百度首页,等待页面标题出现:百度一下,你就知道,再执行输入搜索关键词,点击"百度一下"按钮。
示例脚本:
|-------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from
selenium ``import
webdriver
from
selenium.webdriver.support.wait ``import
WebDriverWait
from
selenium.webdriver.support ``import
expected_conditions as ec
class
TestWait(``object``):
``def
setup(``self``):
``self``.driver ``=
webdriver.Chrome()
``self``.driver.get(http:``/``/``www.baidu.com)
``def
test_webdreiverwait(``self``):
``webdreiverwaits ``=``WebDriverWait(``self``.driver,``2``)
``webdreiverwaits.until(ec.title_is(``"百度一下,你就知道"``))
``self``.driver.find_element_by_id(``"kw"``).send_keys(``"test_webdreiverwait test"``)
``self``.driver.find_element_by_id(``"su"``).click()
``def
teardown(``self``):
``self``.driver.quit()
if
__name__ ``=``=
'__main__'``:
``wait``=``TestWait()
``wait.test_webdreiverwait()
|
三种等待完整示例脚本:
|----------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 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 | from
selenium ``import
webdriver
from
time ``import
sleep
from
selenium.webdriver.support.wait ``import
WebDriverWait
from
selenium.webdriver.support ``import
expected_conditions as ec
class
TestWait(``object``):
``def
setup(``self``):
``self``.driver ``=
webdriver.Chrome()
``self``.driver.get(``"http://www.baidu.com"``)
``def
test_sleep(``self``):
``self``.driver.find_element_by_id(``"kw"``).send_keys(``"sleep test"``)
``sleep(``2``) ``#等待固定时间
``self``.driver.find_element_by_id(``"su"``).click()
``def
test_implicitly(``self``):
``self``.driver.find_element_by_id(``"kw"``).send_keys(``"implicitly test"``)
``self``.driver.implicitly_wait(``2``) ``# 隐式等待
``self``.driver.find_element_by_id(``"su"``).click()
``def
test_webdreiverwait(``self``):
``webdreiverwaits ``=``WebDriverWait(``self``.driver,``2``)
``webdreiverwaits.until(ec.title_is(``"百度一下,你就知道"``))
``self``.driver.find_element_by_id(``"kw"``).send_keys(``"test_webdreiverwait test"``)
``self``.driver.find_element_by_id(``"su"``).click()
``def
teardown(``self``):
``self``.driver.quit()
if
__name__ ``=``=
'__main__'``:
``wait``=``TestWait()
``# wait.test_sleep()
``# wait.test_implicitly()
``wait.test_webdreiverwait()
|
【常见问题】:运行脚本报empty suite:
|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from
selenium ``import
webdriver
from
time ``import
sleep
class
TestWait(``object``):
``def
__init__(``self``):
``self``.driver ``=
webdriver.Chrome()
``self``.driver.get(``'http://www.baidu.com'``)
``def
test_sleep(``self``):
``self``.driver.find_element_by_id(``"kw"``).send_keys(``"sleep test"``)
``# sleep(2) #等待固定时间
``self``.driver.implicitly_wait(``2``) ``# 隐式等待
``self``.driver.find_element_by_id(``"su"``).click()
``self``.driver.quit()
if
__name__ ``=``=
'__main__'``:
``wait``=``TestWait()
``wait.test_sleep()
|
【解决方法】:脚本修改
|----------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 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 | from
selenium ``import
webdriver
from
time ``import
sleep
from
selenium.webdriver.support ``import
expected_conditions as EC
from
selenium.webdriver.support.wait ``import
WebDriverWait
class
TestCase(``object``):
``def
setup(``self``):
``self``.driver ``=
webdriver.Chrome()
``self``.driver.get(``'http://www.baidu.com'``)
``# sleep(2)
``def
test_sleep(``self``):
``self``.driver.find_element_by_id(``'kw'``).send_keys(``'selenium'``)
``# sleep(2) # 线程阻塞 blocking wait
``self``.driver.find_element_by_id(``'su'``).click()
``# sleep(3)
``def
test_implicitly(``self``):
``self``.driver.implicitly_wait(``10``)
``self``.driver.find_element_by_id(``'kw'``).send_keys(``'selenium'``)
``# sleep(2) # 线程阻塞 blocking wait
``self``.driver.find_element_by_id(``'su'``).click()
``# sleep(3)
``def
test_wait(``self``):
``wait ``=
WebDriverWait(``self``.driver,``2``)
``wait.until(EC.title_is(``'百度一下,你就知道'``))
``self``.driver.find_element_by_id(``'kw'``).send_keys(``'selenium'``)
``# sleep(2) # 线程阻塞 blocking wait
``self``.driver.find_element_by_id(``'su'``).click()
``# sleep(3)
``def
teardown(``self``):
``self``.driver.quit()
if
__name__ ``=``=
'__main__'``:
``case ``=
TestCase()
``# case.test_sleep()
``# case.test_implicitly()
``case.test_wait()
|
以:selenium自动化测试学习总结!
以上就是python自动化测试selenium核心技术三种等待方式详解的详细内容,更多关于selenium三种等待方式的资料请关注小编相关文章!end如果文章对你有帮助,记得点赞,收藏,加关注。会不定期分享一些干货哦......