Requests 是 Python 的第三方库,主要用于发送 http 请求,常用于接口自动化测试等。
Selenium 是一个用于 Web 应用程序的自动化测试工具。Selenium 测试直接运行在浏览器中,就像真正的用户在操作一样。
本篇介绍一款将 Requests 和 Selenium 结合在一起的自动化测试工具 - Requestium
Requestium 是一个 Python 库,它将 Requests、Selenium 和 Parsel 的功能合并为一个用于自动化 web 操作的集成工具。
该库是为编写 web 自动化脚本而创建的,这些脚本主要使用请求编写,但能够在维护会话的同时,无缝切换到网站中 JavaScript 密集部分的 Selenium。
Requestium 为 Requests 和 Selenium 添加了独立的改进,并且每一个新功能都经过了延迟评估,因此即使编写只使用 Requests 或 Selenium 的脚本,它也很有用。
特点:
安装:
pip install requestium
如果你使用 Requestium 的 Selenium 部分,例如 Chromedriver,那么你应该下载 Selenium Web 驱动程序。
首先,像处理请求一样创建一个会话,如果使用 web 驱动程序,可以选择添加参数。
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试from requestium import Session, Keysoptions = {'arguments': ['headless']}s = Session(webdriver_path='./chromedriver', default_timeout=15, webdriver_options=options)
由于无头模式很常见,因此有一个快捷方式可以指定 headless=True。
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试from requestium import Session, Keyss = Session(webdriver_path='./chromedriver' headless=True)
你也可以在 Requestium 之外创建一个 Selenium 网络驱动程序,并使用它:
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试from selenium import webdriverfrom requestium import Session, Keysfirefox_driver = webdriver.Firefox()s = Session(driver=firefox_driver)
你不需要解析响应,当调用 xpath,css 或 re 时,它会自动完成。
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试title = s.get('http://samplesite.com').xpath('//title/text()').extract_first(default='Default Title')
与 Python 的标准 re 模块相比,正则表达式需要更少的代码。
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试response = s.get('http://samplesite.com/sample_path')# Extracts the first matchidentifier = response.re_first(r'ID_/d/w/d', default='ID_1A1')# Extracts all matches as a listusers = response.re(r'user_/d/d/d')
你可以切换到使用 Selenium Webdriver 来运行任何 js 代码。
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试s.transfer_session_cookies_to_driver()s.driver.get('http://www.samplesite.com/sample/process')
最后,你可以切换回使用 Requests。
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试s.transfer_driver_cookies_to_session()s.post('http://www.samplesite.com/sample2', data={'key1': 'value1'})
ensure_element_by_ 方法等待元素在浏览器中加载,并在加载后立即返回。它以 Selenium的 find_element_by_ 方法命名(如果找不到元素,它们会立即引发异常)。
Requestium 可以等待一个元素处于以下任何状态:
这些方法对于单页面 Web 应用程序非常有用,其中站点动态地更改其元素。我们通常最终完全用 ensure_element_by_ 调用替换我们的 find_element_by_ 调用,因为它们更灵活。使用这些方法获取的元素具有新的 ensure_click 方法,这使得点击不太容易失败。这有助于解决 Selenium 点击的许多问题。
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试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_idensure_element_by_nameensure_element_by_link_textensure_element_by_partial_link_textensure_element_by_tag_nameensure_element_by_class_nameensure_element_by_css_selector
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 -*-# 公众号:AllTests软件测试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='')
#!/usr/bin/env python# -*- coding: utf-8 -*-# 公众号:AllTests软件测试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")
本文链接:http://www.28at.com/showinfo-26-60901-0.htmlRequestium - 将Requests和Selenium合并在一起的自动化测试工具
声明:本网页内容旨在传播知识,不代表本站观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。