• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

UI自动化测试实战

其他 Huny 1604次浏览 0个评论

前言

前面我们已经搭建好了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()

UI自动化测试实战

感觉还是有很多重复的代码,我们可以在进行优化

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()


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明UI自动化测试实战
喜欢 (0)

您必须 登录 才能发表评论!

加载中……