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 = '***'
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()
感觉还是有很多重复的代码,我们可以在进行优化
import unittest
from selenium import webdriver
import uuid
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
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 element_find_func(self,e):
return self.driver.find_element_by_css_selector(e)
def elements_find_func(self,e):
return self.driver.find_elements_by_css_selector(e)
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get(url)
self.driver.implicitly_wait(10)
self.driver.maximize_window()
self.element_find_func('#user_login').send_keys(username)
self.element_find_func('#user_pass').send_keys(passwd)
self.element_find_func('#rememberme').click()
self.element_find_func('#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.element_find_func(
'#menu-posts > a >.wp-menu-name').click()
self.element_find_func(
'.page-title-action').click() # 点击写文章
sleep(1)
self.element_find_func(
'div.components-modal__header > button > svg').click() # 关闭弹窗
self.element_find_func(
'#post-title-0').send_keys(suid) # 写标题
self.element_find_func(
'#post-content-0').send_keys(suid) # 写文本
self.element_find_func(
'button.components-button.editor-post-publish-panel__toggle.editor-post-publish-button__button.is-primary').click() # 点击发布
self.element_find_func(
'div.editor-post-publish-panel__header-publish-button > button').click()
blog_status = self.element_find_func(
'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.element_find_func(
'#menu-posts > a >.wp-menu-name').click()
mouse = self.elements_find_func(
'td.author.column-author > a')[0] # 定位第一行作者元素
ActionChains(self.driver).move_to_element(mouse).perform() # 鼠标悬停到第一行
blog_title_old = self.elements_find_func(
'td.title.column-title.has-row-actions.column-primary.page-title > div.row-actions > span.trash > a')[0].text #定位多个元素中取第一个
self.elements_find_func(
'td.title.column-title.has-row-actions.column-primary.page-title > div.row-actions > span.trash > a')[0].click()
blog_title_new = self.elements_find_func(
'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()
本文来自博客园,作者:Harry_666,转载请注明原文链接:https://www.cnblogs.com/harry66/p/14349879.html