跟着我们一起学 Python 30天课程-第29天-自动化测试
今天,我通过创建一个基本的自动化测试项目,探索了使用Python进行的端到端浏览器自动化测试。
在实现浏览器自动化方面,Selenium是最受欢迎和使用最广泛的库之一。硒python库非常容易将其与Python结合使用,并运行脚本以在网站上执行自动化测试。
就像在通常情况下用户将如何使用该应用程序一样,自动化测试对于测试Web应用程序至关重要。它还用于执行跨浏览器测试,以确保该应用程序可在所有目标浏览器上正常工作。
我创建了一个基本项目,以了解和实现Selenium浏览器自动化测试的基础。
- 建立专案
我创建了一个新目录python-selenium
,并向项目添加了虚拟环境。
python -m venv venv
- 安装Selenium
下一步是安装Selenium软件包,可以使用以下命令将其安装在项目环境中
pip install selenium
- 安装驱动程序
Selenium使用驱动程序与浏览器进行交互。因此,需要安装特定于浏览器的驱动程序,以便Selenium能够创建用于运行自动化测试的浏览器实例。
| Chrome:| https://sites.google.com/a/chromium.org/chromedriver/downloads |
| Edge:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ |
| Firefox:https://github.com/mozilla/geckodriver/releases |
| Safari:https://webkit.org/blog/6900/webdriver-support-in-safari-10/ |
我为此特定项目下载了Firefox驱动程序。驱动程序必须是可执行路径。
(在Windows 10中,将驱动程序文件放置在python应用程序的安装路径或环境变量中存在其路径的任何其他位置)
- 创建示例自动化脚本
我automation.py
在根项目目录中创建了一个python脚本文件。
这是使用Selenium打开网页的简单代码
automation.py
from selenium import web-driver
browser = webdriver.Firefox()
browser.get('https://tabandspace.com')
硒备忘单
这是常用硒方法的备忘单
# import selenium
from selenium import web-driver
#create a chrome driver
chromedriver = "C:/tests/chromedriver.exe"
driver = webdriver.Chrome(executable_path = chromedriver)
#create a firefox driver
geckodriver = "C:/tests/geckodriver.exe"
driver = webdriver.Firefox(executable_path = geckodriver)
#create an IE driver
iedriver = "C:/tests/IEDriverServer.exe"
driver = webdriver.Firefox(executable_path = iedriver)
#open a website
the_url = "https://dev.to"
driver.get(the_url)
#finding an element by id
the_id = 'register'
element = driver.find_element_by_id(the_id)
#finding an element by name
the_name = 'register'
element = driver.find_element_by_id(the_name)
#findding an element by class name
the_class_name = 'nav-link'
element = driver.find_element_by_class_name(the_class_name)
#finding an element by tag name
the_tag_name = 'a'
element = driver.find_element_by_tag_name(the_tag_name)
#finding an element by link text ( anchor elements)
the_link_text = 'Sign Up'
element = driver.find_element_by_link_text(the_link_text)
#finding an element by partial link text (anchor elements)
the_partial_link_text = 'Sign'
element = driver.find_element_by_partial_link_text(the_partial_link_text)
#finding element using css selectors
the_css_selector = 'a[href="/sign-up"]'
element = driver.find_element_by_css_selector(the_css_selector)
#finding element using x-path
the_xpath = '//a[@href = "/sign-up"]'
element = driver.find_element_by_xpath(the_xpath)
#clicking an element
the_id = 'register'
element = driver.find_element_by_id(the_id)
element.click()
#type inside an element
the_id = 'email'
the_email = 'klaus@werner.de'
element = driver.find_element_by_id(the_id)
element.send_keys(the_email)
#select an option from select elements
the_id = 'country'
element = driver.find_element_by_id(the_id)
select_element = Select(element)
select_element.select_by_visible_text('Canada')
#taking screenshots
the_path = 'C:/tests/screenshots/1.png'
driver.save_screenshot(the_path)
#uploading a file
the_file_path = 'C:/tests/files/example.pdf'
the_id = 'upload_button'
element = driver.find_element_by_id(the_id)
element.send_keys(the_file_path)
#execute javascript
js_code = 'document.getElementById("pop-up").remove()'
driver = execute_script(js_code)
#switch to iframe
the_iframe_id = 'payment_section'
the_element_id = 'card_number'
the_iframe = driver.find_element_by_id(the_iframe_id)
driver.switch_to.frame(the_iframe)
element = driver.find_element_by_id(the_element_id)
element.send_keys('41111111111111')
driver.switch_to.default_content()
#switch to next tab
global nextTab
global currentTab
nextTab = currentTab + 1
driver.switch_to_window(driver.window_handles[nextTab])
currentTab = currentTab + 1
#swtich to previous tab
global previousTab
global currentTab
previousTab = currentTab - 1
driver.switch_to_window(driver.window_handles[previousTab])
currentTab = currentTab - 1
#close tab
driver.close()
#close alert
driver.switch_to.alert.accept()
#refresh
driver.refresh()
#hover
the_id = "register"
the_element = driver.find_element_by_id(the_id)
hover = ActionChains(driver).move_to_element(the_element)
hover.perform()
#right click
the_id = "register"
the_element = driver.find_element_by_id(the_id)
right_click = ActionChains(driver).context_click(the_element)
right_click.perform()
#set window size
driver.set_window_size(1600, 1200)
#press key
the_id = 'register'
element = driver.find_element_by_id(the_id)
element.send_keys(Keys.RETURN)
#configure element load timeout
from selenium.webdriver.support.ui import WebDriverWait
the_id = 'register'
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, the_id)))
#emulate mobile device
google_pixel_3_xl_user_agent = 'Mozilla/5.0 (Linux; Android 9.0; Pixel 3 XL Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.98 Mobile Safari/537.36'
pixel_3_xl_emulation = {
"deviceMetrics": {
"width": 411,
"height": 731,
"pixelRatio": 3
},
"userAgent": google_pixel_3_xl_user_agent
}
options = webdriver.ChromeOptions()
options.add_experimental_option("mobileEmulation", pixel_3_xl_emulation)
driver = webdriver.Chrome(
executable_path = chromedriver,
chrome_options = options)
在构建管道中配置自动化测试
我找到了一篇很棒的文章,描述了在构建管道(如Github Workflow)中实现Web自动化脚本的步骤。Selenium Base https://github.com/seleniumbase/SeleniumBase是selenium的一个很好的包装,它使安装驱动程序和将自动化脚本添加到构建管道变得容易。
跟着我们一起学 Python 30天课程目录:
- 跟着我们一起学 Python 30天课程-第30天-免费Python资源
- 跟着我们一起学 Python 30天课程-第29天-自动化测试
- 跟着我们一起学 Python 30天课程-第28天-ML和数据科学II
- 跟着我们一起学 Python 30天课程-第27天-ML和数据科学I
- 跟着我们一起学 Python 30天课程-第26天-机器学习基础
- 跟着我们一起学 Python 30天课程-第25天-Web 开发进阶
- 跟着我们一起学 Python 30天课程-第24天-Web开发基础
- 跟着我们一起学 Python 30天课程-第23天-网页爬虫
- 跟着我们一起学 Python 30天课程-第22天-脚本额外功能Scripting Extras
- 跟着我们一起学 Python 30天课程-第21天-脚本编写基础
- 跟着我们一起学 Python 30天课程-第20天-调试和测试
- 跟着我们一起学 Python 30天课程-第19天-正则表达式
- 跟着我们一起学 Python 30天课程-第18天-文件I / O
- 跟着我们一起学 Python 30天课程-第17天-外部模块External Modules
- 跟着我们一起学 Python 30天课程-第16天-模块基础Module Basics
- 跟着我们一起学 Python 30天课程-第15天-生成器Generators
- 跟着我们一起学 Python 30天课程-第14天-错误处理Error Handling
- 跟着我们一起学 Python 30天课程-第13天-Decorators
- 跟着我们一起学 Python 30天课程-第12天-Lambda Expressions & Comprehensions
- 跟着我们一起学 Python 30天课程-第11天-函数编程Functional Programming基础
- 跟着我们一起学 Python 30天课程-第10天-OOP Missing Pieces
- 跟着我们一起学 Python 30天课程-第9天-OOP Pillars
- 跟着我们一起学 Python 30天课程-第8天-OOP基础知识
- 跟着我们一起学 Python 30天课程-第7天-开发环境搭建(Developer Environment)
- 跟着我们一起学 Python 30天课程-第6天-循环II和函数(Loops II & Functions)
- 跟着我们一起学 Python 30天课程-第5天-条件和循环I(Conditions & Loops I)
- 跟着我们一起学 Python 30天课程-第4天-数据类型III(Data Types III)
- 跟着我们一起学 Python 30天课程-第3天-数据类型II(Data Types II)
- 跟着我们一起学 Python 30天课程-第2天-数据类型I(Data Types I)
- 跟着我们一起学 Python 30天课程-第1天-简介
1. 本站资源转自互联网,源码资源分享仅供交流学习,下载后切勿用于商业用途,否则开发者追究责任与本站无关!
2. 本站使用「署名 4.0 国际」创作协议,可自由转载、引用,但需署名原版权作者且注明文章出处
3. 未登录无法下载,登录使用金币下载所有资源。
IT小站 » 跟着我们一起学 Python 30天课程-第29天-自动化测试
常见问题FAQ
- 没有金币/金币不足 怎么办?
- 本站已开通每日签到送金币,每日签到赠送五枚金币,金币可累积。
- 所有资源普通会员都能下载吗?
- 本站所有资源普通会员都可以下载,需要消耗金币下载的白金会员资源,通过每日签到,即可获取免费金币,金币可累积使用。