UI自动化测试实战
前言
前面我们已经搭建好了wordpress网站,如果需要查看运行效果可以看我前面的搭建文章,下面我们来进行自动化测试的练习。
示例
首先我们测试自动登陆
import unittest
from selenium import webdriver
import uuid
from time import sleep
username = passwd = '***'
uid = str(uuid.uuid1())
suid = ''.join(uid.split('-'))
url = 'http://139.199.192.100:8000/wp-login.php'
class Test_Blog(unittest.TestCase):
'''博客测试用例初始化配置'''
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get(url)
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def tearDown(self):
self.driver.quit()
class test_login(Test_Blog):
'''博客登陆测试用例'''
def test_login(self):
self.driver.find_element_by_id('user_login').send_keys(username)
self.driver.find_element_by_id('user_pass').send_keys(passwd)
self.driver.find_element_by_id('rememberme').click()
self.driver.find_element_by_id('wp-submit').click()
sleep(1)
title_url = self.driver.current_url
assert 'wp-admin' in title_url, '登陆不成功或者断言错误'
if __name__ == '__main__':
unittest.main()
登陆之后,我们来测试写文章功能
import unittest
from selenium import webdriver
import uuid
from time import sleep
username = passwd = '***'
uid = str(uuid.uuid1())
suid = ''.join(uid.split('-'))
url = 'http://139.199.192.100:8000/wp-login.php'
class Test_Blog(unittest.TestCase):
'''博客测试用例初始化配置'''
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get(url)
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def tearDown(self):
self.driver.quit()
class test_login(Test_Blog):
'''博客登陆测试用例'''
def test_login(self):
self.driver.find_element_by_id('user_login').send_keys(username)
self.driver.find_element_by_id('user_pass').send_keys(passwd)
self.driver.find_element_by_id('rememberme').click()
self.driver.find_element_by_id('wp-submit').click()
sleep(1)
title_url = self.driver.current_url
assert 'wp-admin' in title_url, '登陆不成功或者断言错误'
class test_write_Blog(Test_Blog):
'''博客写文章测试用例'''
def test_write_blog(self):
self.driver.find_element_by_id('user_login').send_keys(username)
self.driver.find_element_by_id('user_pass').send_keys(passwd)
self.driver.find_element_by_id('rememberme').click()
self.driver.find_element_by_id('wp-submit').click()
sleep(1)
self.driver.find_element_by_css_selector(
'#menu-posts > a >.wp-menu-name').click()
self.driver.find_element_by_css_selector('.page-title-action').click()
self.driver.find_element_by_css_selector(
'div.components-modal__header > button > svg').click()
sleep(1)
self.driver.find_element_by_css_selector(
'#post-title-0').send_keys(suid)
self.driver.find_element_by_css_selector(
'#post-content-0').send_keys(suid)
self.driver.find_element_by_css_selector(
'button.components-button.editor-post-publish-panel__toggle.editor-post-publish-button__button.is-primary').click()
self.driver.find_element_by_css_selector(
'div.editor-post-publish-panel__header-publish-button > button').click()
blog_status = self.driver.find_element_by_css_selector(
'div.components-panel__body.post-publish-panel__postpublish-header.is-opened').text
assert '已被发布' in blog_status, '文章未发布或断言错误'
if __name__ == '__main__':
unittest.main()
写文章测试完了,该把文章删除掉了,于是我们再来进行删除的测试用例,并且优化了登陆的代码
import unittest
from selenium import webdriver
import uuid
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
username = passwd = 'pyse_24'
uid = str(uuid.uuid1())
suid = ''.join(uid.split('-'))
url = 'http://139.199.192.100:8000/wp-login.php'
class Test_Blog(unittest.TestCase):
'''博客测试用例前置和后置'''
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get(url)
self.driver.implicitly_wait(10)
self.driver.maximize_window()
self.driver.find_element_by_id('user_login').send_keys(username)
self.driver.find_element_by_id('user_pass').send_keys(passwd)
self.driver.find_element_by_id('rememberme').click()
self.driver.find_element_by_id('wp-submit').click()
sleep(1)
def tearDown(self):
self.driver.quit()
class test_login(Test_Blog):
'''博客登陆测试用例'''
def test_login(self):
title_url = self.driver.current_url
assert 'wp-admin' in title_url, '登陆不成功或者断言错误'
class test_write_Blog(Test_Blog):
'''博客写文章测试用例'''
def test_write_blog(self):
self.driver.find_element_by_css_selector(
'#menu-posts > a >.wp-menu-name').click()
self.driver.find_element_by_css_selector('.page-title-action').click() #点击写文章
sleep(1)
self.driver.find_element_by_css_selector(
'div.components-modal__header > button > svg').click() #关闭弹窗
self.driver.find_element_by_css_selector(
'#post-title-0').send_keys(suid) #写标题
self.driver.find_element_by_css_selector(
'#post-content-0').send_keys(suid) #写文本
self.driver.find_element_by_css_selector(
'button.components-button.editor-post-publish-panel__toggle.editor-post-publish-button__button.is-primary').click() #点击发布
self.driver.find_element_by_css_selector(
'div.editor-post-publish-panel__header-publish-button > button').click()
blog_status = self.driver.find_element_by_css_selector(
'div.components-panel__body.post-publish-panel__postpublish-header.is-opened').text #获取发布状态
assert '已被发布' in blog_status, '文章未发布或断言错误'
class Test_Delete_Blog(Test_Blog):
'''博客删除测试用例'''
def test_delete_blog(self):
self.driver.find_element_by_css_selector(
'#menu-posts > a >.wp-menu-name').click()
mouse = self.driver.find_elements_by_css_selector(
'td.author.column-author > a')[0] # 定位第一行作者元素
ActionChains(self.driver).move_to_element(mouse).perform() # 鼠标悬停到第一行
blog_title_old = self.driver.find_elements_by_css_selector(
'td.title.column-title.has-row-actions.column-primary.page-title > div.row-actions > span.trash > a')[0].text
self.driver.find_elements_by_css_selector(
'td.title.column-title.has-row-actions.column-primary.page-title > div.row-actions > span.trash > a')[0].click()
blog_title_new = self.driver.find_elements_by_css_selector(
'td.title.column-title.has-row-actions.column-primary.page-title > div.row-actions > span.trash > a')[0].text
assert blog_title_old != blog_title_new # 判断删除前和删除后的同一元素位置的文本不相等
if __name__ == '__main__':
unittest.main()

UI自动化测试实战的更多相关文章
- Pytest UI自动化测试实战实例
前言 明天就放假了,4天小长假,是不是很开心!也许很多人要回老家帮家里种地,干农活.其实能陪陪家里人,帮忙干点农活还是挺开心的,希望大家有个愉快的假期!废话不多说哈,今天再来说说pytest吧,经过几 ...
- UI自动化测试框架(项目实战)python、Selenium(日志、邮件、pageobject)
其实百度UI自动化测试框架,会出来很多相关的信息,不过就没有找到纯项目的,无法拿来使用的:所以我最近就写了一个简单,不过可以拿来在真正项目中可以使用的测试框架. 项目的地址:https://githu ...
- selenium2 Webdriver + Java 自动化测试实战和完全教程
selenium2 Webdriver + Java 自动化测试实战和完全教程一.快速开始 博客分类: Selenium-webdriverselenium webdriver 学习selenium ...
- selenium2 python自动化测试实战(回归测试)
selenium2 python自动化测试实战 最近接手商城的项目,针对后台测试,功能比较简单,但是流程比较繁多,涉及到前后台的交叉测试.在对整个项目进行第一轮测试完成之后,考虑以后回归测试任务比较重 ...
- Selenium2自动化测试实战序言
记得很久之前接触自动化的时候看了一本关于某早期自动化测试工具的书,书名已经记不得了,内容却一直印象深刻.因为那本书根本就是把官方文档有选择性的翻译一遍,对于实际应用来说其作用几乎是零.因此从那时候起我 ...
- UI自动化测试元素定位思想
2014年的最后一天,以一篇短文纪念一下. 经常看到有同学说UI自动化测试定位难,找不到北.这话是不错的,定位是难,灵活且复杂,需要经验加技术,但是有写东西是可以提炼出来作为思想去推而广之的. 简单来 ...
- Ui自动化测试上传文件方法都在这里了
前言 实施UI自动化测试的时候,经常会遇见上传文件的操作,那么对于上传文件你知道几种方法呢?今天我们就总结一下几种常用的上传文件的方法,并分析一下每个方法的优点和缺点以及哪种方法效率,稳定性更高 被测 ...
- 关于《Selenium3自动化测试实战--基于python语言》
2016年1月,机缘巧合下我出版了<Selenium2自动化测试实战--基于python语言>这本书,当时写书的原因是,大部分讲Selenium的书并不讲编程语言和单元测试框,如果想在项目 ...
- Selenium 与自动化测试 —— 《Selenium 2 自动化测试实战》读书笔记
背景 最近在弄 appium,然后顺便发现了 Selenium 框架和这本书,恰好这本书也介绍了一些软件测试&自动化测试的理论知识,遂拿过来学习学习.所以本文几乎没有实践内容,大多都是概念和工 ...
随机推荐
- Eclipse中,No compiler is provided in this environment. Perhaps you are running on a JRE rather than a
问题说明 Eclipse导入Maven项目后,执行 mvn clean install后,出现如下错误: [INFO] ---------------------------------------- ...
- Solon 1.2.13 发布,开启与 Springboot 的互通
Solon 一个类似Springboot的微型开发框架.项目从2018年启动以来,参考过大量前人作品:历时两年,3500多次的commit:内核保持0.1m的身材,超高的Web跑分,良好的使用体验. ...
- Java学习日报7.16
void StudentManage::Sort() //排序功能{ StudentInfo *h,*curr,*temp,*last; h=head; for(int j=0;j<n;j++) ...
- 阿里技术专家详解 Dubbo 实践,演进及未来规划
Dubbo 整体介绍 Dubbo 是一款高性能,轻量级的 Java RPC 框架.虽然它是以 Java 语言来出名的,但是现在我们生态里面已经有 Go.Python.PHP.Node.JS 等等语言. ...
- ASP.Net Core 3.1 使用实时应用SignalR入门
参考文章:微软官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/signalr/introduction?view=aspnetcore-3.1 和 ...
- 神经网络中的降维和升维方法 (tensorflow & pytorch)
大名鼎鼎的UNet和我们经常看到的编解码器模型,他们的模型都是先将数据下采样,也称为特征提取,然后再将下采样后的特征恢复回原来的维度.这个特征提取的过程我们称为"下采样",这个恢复 ...
- Macbook 安装Windows的完美教程
[原文](http://www.melodydance.top/mac-win.html) 1. 背景 Windows相对于Mac市场占有率更高,对很多人来说Windows使用起来更方便,以至于很多人 ...
- dubbo配置启动时检查
启动检查设置 Dubbo缺省会在启动时检查依赖的服务是否可用,不可用会抛出异常,阻止Spring初始化完成,默认check="true":是开启检查. 比如测试的时候,有些服务并不 ...
- Kubernetes学习笔记之认识Kubernetes组件
前言:笔记知识点来源于Kubernetes官方文档说明,链接:https://kubernetes.io/docs/concepts/overview/components/ ,本记录仅仅是学习笔记记 ...
- Xshell与Xftp免费下载安装及步骤
Xshell与Xftp免费下载安装及步骤 1.进入Xshell的官网:https://www.netsarang.com/zh/ 加粗样式 2.选择你需要的软件进行下载如:Xshell 3.选择家庭和 ...