将Requests和Selenium合并在一起的自动化测试工具

1、前言

Requests 是 Python 的第三方库,主要用于发送 http 请求,常用于接口自动化测试等。

Selenium 是一个用于 Web 应用程序的自动化测试工具。Selenium 测试直接运行在浏览器中,就像真正的用户在操作一样。

2、简介

Requestium 是一个 Python 库,它将 Requests、Selenium 和 Parsel 的功能合并为一个用于自动化 web 操作的集成工具。

该库是为编写 web 自动化脚本而创建的,这些脚本主要使用请求编写,但能够在维护会话的同时,无缝切换到网站中 JavaScript 密集部分的 Selenium。

Requestium 为 Requests 和 Selenium 添加了独立的改进,并且每一个新功能都经过了延迟评估,因此即使编写只使用 Requests 或 Selenium 的脚本,它也很有用。

特点:

·在维护当前 web 会话的同时,启用请求会话和 Selenium web 驱动程序之间的切换。

· 将 Parsel 的解析器集成到库中,使 xpath、css 和 regex 的编写更加简洁。

· 改进了 Selenium 对动态加载元素的处理。

· 使 Selenium 中的 cookie 处理更加灵活。

· 使 Selenium 中的点击元素更加可靠。

· 本机支持 Chromedriver,并添加自定义网络驱动程序。

安装:

pip install requestium

如果你使用 Requestium 的 Selenium 部分,例如 Chromedriver,那么你应该下载 Selenium Web 驱动程序。

3、快速上手

首先,像处理请求一样创建一个会话,如果使用 web 驱动程序,可以选择添加参数。

 #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  from requestium import Session, Keys
  options = {'arguments': ['headless']}
  s = Session(webdriver_path='./chromedriver', default_timeout=15, webdriver_options=options)

由于无头模式很常见,因此有一个快捷方式可以指定 headless=True。

#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  from requestium import Session, Keys
  s = Session(webdriver_path='./chromedriver' headless=True)

你也可以在 Requestium 之外创建一个 Selenium 网络驱动程序,并使用它: 

#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  from selenium import webdriver
  from requestium import Session, Keys
  firefox_driver = webdriver.Firefox()
  s = Session(driver=firefox_driver)

你不需要解析响应,当调用 xpath,css 或 re 时,它会自动完成。

#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  title = s.get('http://samplesite.com').xpath('//title/text()').extract_first(default='Default Title')

与 Python 的标准 re 模块相比,正则表达式需要更少的代码。

 #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  response = s.get('http://samplesite.com/sample_path')
  # Extracts the first match
  identifier = response.re_first(r'ID_\d\w\d', default='ID_1A1')
  # Extracts all matches as a list
  users = response.re(r'user_\d\d\d')

你可以切换到使用 Selenium Webdriver 来运行任何 js 代码。

 #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  s.transfer_session_cookies_to_driver()
  s.driver.get('http://www.samplesite.com/sample/process')

最后,你可以切换回使用 Requests。

 #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  s.transfer_driver_cookies_to_session()
  s.post('http://www.samplesite.com/sample2', data={'key1': 'value1'})

等待元素

ensure_element_by_ 方法等待元素在浏览器中加载,并在加载后立即返回。它以 Selenium的 find_element_by_ 方法命名(如果找不到元素,它们会立即引发异常)。

Requestium 可以等待一个元素处于以下任何状态:

·存在(默认)

· 可点击

· 看得见的

· 不可见(可用于等待加载... GIF 消失等)

这些方法对于单页面 Web 应用程序非常有用,其中站点动态地更改其元素。我们通常最终完全用 ensure_element_by_ 调用替换我们的 find_element_by_ 调用,因为它们更灵活。使用这些方法获取的元素具有新的 ensure_click 方法,这使得点击不太容易失败。这有助于解决 Selenium 点击的许多问题。

#!/usr/bin/env python

 #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  s.driver.ensure_element_by_xpath("//li[@class='b1']", state='clickable', timeout=5).ensure_click()
  # === We also added these methods named in accordance to Selenium's api design ===
  ensure_element_by_id
  ensure_element_by_name
  ensure_element_by_link_text
  ensure_element_by_partial_link_text
  ensure_element_by_tag_name
  ensure_element_by_class_name
  ensure_element_by_css_selector

添加 Cookie

ensure_add_cookie 方法使得添加 Cookie 更加稳健。Selenium 需要浏览器在能够添加 Cookie 之前处于 Cookie 的域中,此方法为此提供了几种解决方法。如果浏览器不在 Cookie 域中,它会先获取域然后再添加 Cookie。它还允许你在添加 Cookie 之前覆盖域,并避免执行此 GET。域可以被覆盖为 ’’,这将把 Cookie 的域设置为驱动程序当前所在的任何域。如果无法添加 cookie,它会尝试使用限制性较小的域(例如:home.site.com -> site.com)进行添加,然后在失败之前。

 #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  cookie = {"domain": "www.site.com",
            "secure": false,
            "value": "sd2451dgd13",
            "expiry": 1516824855.759154,
            "path": "/",
            "httpOnly": true,
            "name": "sessionid"}
  s.driver.ensure_add_cookie(cookie, override_domain='')

使用 Requestium 示例

 #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # 公众号:软件测试小dao
  from requestium import Session, Keys
  # If you want requestium to type your username in the browser for you, write it in here:
  reddit_user_name = ''
  s = Session('./chromedriver', default_timeout=15)
  s.driver.get('http://reddit.com')
  s.driver.find_element_by_xpath("//a[@href='https://www.reddit.com/login']").click()
  print('Waiting for elements to load...')
  s.driver.ensure_element_by_class_name("desktop-onboarding-sign-up__form-toggler",
                state='visible').click()
  if reddit_user_name:
      s.driver.ensure_element_by_id('user_login').send_keys(reddit_user_name)
      s.driver.ensure_element_by_id('passwd_login').send_keys(Keys.BACKSPACE)
  print('Please log-in in the chrome browser')
  s.driver.ensure_element_by_class_name("desktop-onboarding__title", timeout=60, state='invisible')
  print('Thanks!')
  if not reddit_user_name:
      reddit_user_name = s.driver.xpath("//span[@class='user']//text()").extract_first()
  if reddit_user_name:
      s.transfer_driver_cookies_to_session()
      response = s.get("https://www.reddit.com/user/{}/".format(reddit_user_name))
      cmnt_karma = response.xpath("//span[@class='karma comment-karma']//text()").extract_first()
      reddit_golds_given = response.re_first(r"(\d+) gildings given out")
      print("Comment karma: {}".format(cmnt_karma))
      print("Reddit golds given: {}".format(reddit_golds_given))
  else:
      print("Couldn't get user name")
​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值