前言
前面我们已经搭建好了wordpress网站,如果需要查看运行效果可以看我前面的搭建文章,下面我们来进行自动化测试的练习。
示例
首先我们测试自动登陆
import unittestfrom selenium import webdriverimport uuidfrom time import sleepusername = 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_urlassert \'wp-admin\' in title_url, \'登陆不成功或者断言错误\'if __name__ == \'__main__\':unittest.main()
登陆之后,我们来测试写文章功能
import unittestfrom selenium import webdriverimport uuidfrom time import sleepusername = 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_urlassert \'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\').textassert \'已被发布\' in blog_status, \'文章未发布或断言错误\'if __name__ == \'__main__\':unittest.main()
写文章测试完了,该把文章删除掉了,于是我们再来进行删除的测试用例,并且优化了登陆的代码
import unittestfrom selenium import webdriverimport uuidfrom time import sleepfrom selenium.webdriver.common.action_chains import ActionChainsusername = 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)56cself.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_urlassert \'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-pad0ublish-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].textself.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].textassert blog_title_old != blog_title_new # 判断删除前和删除后的同一元素位置的文本不相等if __name__ == \'__main__\':unittest.main()
感觉还是有很多重复的代码,我们可以在进行优化
import unittestfrom selenium import webdriverimport uuidfrom time import sleepfrom selenium.webdriver.common.action_chains import ActionChainsusername = 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\').send15b0_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_urlassert \'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].textassert blog_title_old != blog_title_new # 判断删除前和删除后的同一元素位置的文本不相等if __name__ == \'__main__\':unittest.main()