前言
前面我們已經搭建好了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()