selenium+pytest进行自动化测试(项目:白羽黑月SMS)

这篇具有很好参考价值的文章主要介绍了selenium+pytest进行自动化测试(项目:白羽黑月SMS)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

最近在学习selenium和pytest,刚好找到一个合适的项目学习,根据bysms给出来的测试用例进行自动化测试编写

首先先写了登录的测试用例,因为后续测试都是在登陆的基础上测试的,所以把这单独写出来

创建一个login_success.py文件

from selenium.webdriver.common.by import By

def login_success(driver,username,password):
    element_username=driver.find_element(By.ID,'username')
    element_username.clear()
    element_username.send_keys(username)


    element_password=driver.find_element(By.ID,'password')
    element_password.clear()
    element_password.send_keys(password)



    button_submit=driver.find_element(By.TAG_NAME,'button')
    button_submit.click()

随后新建一个text_ui.py文件,用来存放测试用例

import pytest
from  login_success import login_success
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select
driver=webdriver.Chrome()
driver.get('http://127.0.0.1/mgr/sign.html')
driver.implicitly_wait(10)

login_success(driver,'byhy','88888888')

class TestClass:

    def test_0101(self):
        # login_success(driver,'byhy','88888888')
        actual_result=''
        elements=driver.find_elements(By.CSS_SELECTOR,'.sidebar-menu span')
        for element in elements[0:3]:
            actual_result+=''.join(element.text)
        print('页面前三项菜单名称分别为',actual_result)
        expected_result='客户药品订单'
        print('预期结果',expected_result)
        assert expected_result==actual_result

        time.sleep(1)
        # driver.quit()   
     

    def test_0102(self):
        customer_button=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>button')
        customer_button.click()
        
        customer_name=driver.find_element(By.CSS_SELECTOR,'.col-lg-8 div:nth-child(1) input')
        customer_name.send_keys('南京中医院')

        customer_mobile=driver.find_element(By.CSS_SELECTOR,'.col-lg-8 div:nth-child(2) input')
        customer_mobile.send_keys('13370082190')

        customer_address=driver.find_element(By.CSS_SELECTOR,'.col-lg-8 div:nth-child(3) textarea')
        customer_address.send_keys('南京中医院110号')

        customer_create=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-12 button:nth-child(1)')
        customer_create.click()

        # 点击取消按钮
        time.sleep(1)
        create_button = driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-12 button:nth-child(2)')
        create_button.click()


        actual_result=''
        customer_info=driver.find_elements(By.CSS_SELECTOR,'.content>div:nth-of-type(3) .search-result-item-field>span:nth-child(2)')
        for element in customer_info:
            actual_result+=''.join(element.text)
        print('列表中显示的客户信息为: ', actual_result)
        expected_result = '南京中医院13370082190南京中医院110号'
        assert expected_result==actual_result
        time.sleep(1)
    #     driver.quit()


    def test_0103(self):
        customer_edit=driver.find_element(By.CSS_SELECTOR,'.content>div:nth-of-type(3) .search-result-item-actionbar label:nth-of-type(1)')
        customer_edit.click()

        customer_name=driver.find_element(By.CSS_SELECTOR,'.content>div:nth-of-type(3)>div>div:nth-of-type(1)>input')
        customer_name.clear()
        customer_name.send_keys('南京市中医院')
        time.sleep(1)
        confirm_driver=driver.find_element(By.CSS_SELECTOR,'.content>div:nth-of-type(3) .search-result-item-actionbar label:nth-of-type(1)')
        confirm_driver.click()

        time.sleep(5)

        actual_result=''
        customer_info=driver.find_elements(By.CSS_SELECTOR,'.content>div:nth-of-type(3) .search-result-item-field>span:nth-child(2)')
        for element in customer_info:
            actual_result+=''.join(element.text)
      
        print('列表中显示的客户信息为:', actual_result)
        expected_result = '南京市中医院13370082190南京中医院110号'
        assert expected_result==actual_result
        time.sleep(1)
    
    
    def test_0105(self):
        medicines_menu=driver.find_element(By.CSS_SELECTOR,'.sidebar>ul>li:nth-of-type(3)>a')
        medicines_menu.click()

        medicines_add=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>button')
        medicines_add.click()

        medicines_name=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(1)>input')
        medicines_name.send_keys('板蓝根')

        medicines_num=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(2)>input')
        medicines_num.send_keys('100')

        medicines_word=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(3)>textarea')
        medicines_word.send_keys('')

        create_medicines=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-12>button:nth-child(1)')
        create_medicines.click()

        cancel_medicines=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-12>button:nth-child(2)')
        cancel_medicines.click()

        actual_result=''
        time.sleep(1)

        medicines_info=driver.find_elements(By.CSS_SELECTOR,'.content>div:nth-of-type(3) div>span:nth-child(2)')
        for element in medicines_info:
            actual_result+=''.join(element.text)
        print('列表中显示的药品信息为:', actual_result)
        expected_result = '板蓝根100无'
        assert expected_result==actual_result
        time.sleep(1)


    def test_0106(self):
        driver.maximize_window()
        mainWindow=driver.current_window_handle
        url1=driver.current_url

        click_footer=driver.find_element(By.CSS_SELECTOR,'.main-footer>.pull-right>a')
        click_footer.click()

        for handle in driver.window_handles:
            driver.switch_to.window(handle)
            if u'教Python' in driver.title:
                break

        actual_result=''
        time.sleep(5)

        header_menu=driver.find_elements(By.CSS_SELECTOR,'.navbar-collapse>.navbar-nav>.nav-item>a>span')
        for element in header_menu:
            actual_result+=''.join(element.text)
        print('导航栏信息为',actual_result)
        expected_result = 'Python基础Python进阶Qt图形界面Django自动化测试性能测试JS语言JSWeb'
        print('期望值信息为',expected_result)
        actual_result_new=actual_result.replace(" ", "")
        assert expected_result==actual_result_new

        driver.switch_to.window(mainWindow)
        url2=driver.current_url
        assert url1==url2
        time.sleep(1)


    def test_0107(self):
       
       #添加药品
        medicines_menu=driver.find_element(By.CSS_SELECTOR,'.sidebar>ul>li:nth-of-type(3)>a')
        medicines_menu.click()

        medicines_add=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>button')
        medicines_add.click()

        #将药品的数据放在列表中
        medicines_data=[['青霉素盒装1','YP-32342341','青霉素注射液,每支15ml,20支装'],
                        ['青霉素盒装2','YP-32342342','青霉素注射液,每支15ml,30支装'],
                        ['青霉素盒装3','YP-32342343','青霉素注射液,每支15ml,40支装']]


        for i in range(len(medicines_data)):
            medicines_name=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(1)>input')
            medicines_name.send_keys(medicines_data[i][0])

            medicines_num=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(2)>input')
            medicines_num.send_keys(medicines_data[i][1])

            medicines_word=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(3)>textarea')
            medicines_word.send_keys(medicines_data[i][2])

            create_medicines=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-child(1)')
            create_medicines.click()
            time.sleep(1)


        cancel_medicines=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-child(2)')
        cancel_medicines.click()



         #添加客户
        customer_menu=driver.find_element(By.CSS_SELECTOR,'.sidebar>ul>li:nth-of-type(2)>a')
        customer_menu.click()

        customer_add=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>button')
        customer_add.click()

        #将客户的数据放在列表中
        customer_data=[['南京中医院1','2551867851','江苏省-南京市-秦淮区-汉中路-501'],
                        ['南京中医院2','2551867852','江苏省-南京市-秦淮区-汉中路-502'],
                        ['南京中医院3','2551867853','江苏省-南京市-秦淮区-汉中路-503']]


        for i in range(len(customer_data)):
            customer_name=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-of-type(1)>input')
            customer_name.send_keys(medicines_data[i][0])

            customer_num=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-of-type(2)>input')
            customer_num.send_keys(medicines_data[i][1])

            customer_address=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-of-type(3)>textarea')
            customer_address.send_keys(medicines_data[i][2])

            create_customer=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-of-type(1)')
            create_customer.click()

            time.sleep(1)


        cancel_customer=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-of-type(2)')
        cancel_customer.click()




        #添加订单
        order_menu=driver.find_element(By.CSS_SELECTOR,'.sidebar>ul>li:nth-of-type(4)>a')
        order_menu.click()

        order_add=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>button')
        order_add.click()


        order_info={'order_name':'测试订单','customer':'南京市中医院','medicine':'板蓝根','number':'100'}

    
        order_name=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-8>div:nth-of-type(1)>input')
        order_name.send_keys(order_info['order_name'])

        customer=Select(driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-8>div:nth-of-type(2)>select'))
        customer.select_by_visible_text(order_info['customer'])

        medicine=Select(driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-8>div:nth-of-type(3)>select'))
        medicine.select_by_visible_text(order_info['medicine'])

        time.sleep(1)

        number=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-8>div:nth-of-type(3)>div>input')
        number.send_keys(order_info['number'])
        time.sleep(1)



        create_order=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-of-type(1)')
        create_order.click()

        cancel_order=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-of-type(2)')
        cancel_order.click()

        order_name=driver.find_element(By.CSS_SELECTOR,'.search-result-item>div:nth-child(1)>span:nth-child(2)')
        actual_result=order_name.text
        expected_result='测试订单'
        assert actual_result==expected_result
        time.sleep(1)




    def test_0108(self):

        del_exist_info()
       #添加药品
        medicines_menu=driver.find_element(By.CSS_SELECTOR,'.sidebar>ul>li:nth-of-type(3)>a')
        medicines_menu.click()

        medicines_add=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>button')
        medicines_add.click()

        #将药品的数据放在列表中
        medicines_data=[['青霉素盒装1','YP-32342341','青霉素注射液,每支15ml,20支装'],
                        ['青霉素盒装2','YP-32342342','青霉素注射液,每支15ml,30支装'],
                        ['青霉素盒装3','YP-32342343','青霉素注射液,每支15ml,40支装']]


        for i in range(len(medicines_data)):
            medicines_name=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(1)>input')
            medicines_name.send_keys(medicines_data[i][0])

            medicines_num=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(2)>input')
            medicines_num.send_keys(medicines_data[i][1])

            medicines_word=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-child(3)>textarea')
            medicines_word.send_keys(medicines_data[i][2])

            create_medicines=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-child(1)')
            create_medicines.click()
            time.sleep(1)


        cancel_medicines=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-child(2)')
        cancel_medicines.click()



         #添加客户
        customer_menu=driver.find_element(By.CSS_SELECTOR,'.sidebar>ul>li:nth-of-type(2)>a')
        customer_menu.click()

        customer_add=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>button')
        customer_add.click()

        #将客户的数据放在列表中
        customer_data=[['南京中医院1','2551867851','江苏省-南京市-秦淮区-汉中路-501'],
                        ['南京中医院2','2551867852','江苏省-南京市-秦淮区-汉中路-502'],
                        ['南京中医院3','2551867853','江苏省-南京市-秦淮区-汉中路-503']]


        for i in range(len(customer_data)):
            customer_name=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-of-type(1)>input')
            customer_name.send_keys(medicines_data[i][0])

            customer_num=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-of-type(2)>input')
            customer_num.send_keys(medicines_data[i][1])

            customer_address=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-8>div:nth-of-type(3)>textarea')
            customer_address.send_keys(medicines_data[i][2])

            create_customer=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-of-type(1)')
            create_customer.click()

            time.sleep(1)


        cancel_customer=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-of-type(2)')
        cancel_customer.click()




        #添加订单
        order_menu=driver.find_element(By.CSS_SELECTOR,'.sidebar>ul>li:nth-of-type(4)>a')
        order_menu.click()

        order_add=driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>button')
        order_add.click()


        order_info={'order_name':'测试订单','customer':'南京市中医院','medicine':'板蓝根','number':'100'}

    
        order_name=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-8>div:nth-of-type(1)>input')
        order_name.send_keys(order_info['order_name'])

        customer=Select(driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-8>div:nth-of-type(2)>select'))
        customer.select_by_visible_text(order_info['customer'])

        medicine=Select(driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-8>div:nth-of-type(3)>select'))
        medicine.select_by_visible_text(order_info['medicine'])

        time.sleep(1)

        number=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-8>div:nth-of-type(3)>div>input')
        number.send_keys(order_info['number'])
        time.sleep(1)



        create_order=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-of-type(1)')
        create_order.click()

        cancel_order=driver.find_element(By.CSS_SELECTOR,'.col-lg-12>.col-lg-12>button:nth-of-type(2)')
        cancel_order.click()


        order_name=driver.find_element(By.CSS_SELECTOR,'.search-result-item>div:nth-child(1)>span:nth-child(2)')
        actual_result=order_name.text
        expected_result='测试订单'
        assert actual_result==expected_result
        time.sleep(1)

def del_exist_info():
    def del_info():
        elements=driver.find_elements(By.CSS_SELECTOR,'.search-result-item-actionbar label:nth-last-of-type(1)')
        if elements:
            for element in elements:
                        element.click()
                        driver.switch_to.alert.accept()
                        time.sleep(1)

    order_menu=driver.find_element(By.CSS_SELECTOR,'[href="#/orders"]')
    order_menu.click()
    del_info()

    medicines_menu=driver.find_element(By.CSS_SELECTOR,'[href="#/medicines"]')
    medicines_menu.click()
    del_info()

    customer_menu=driver.find_element(By.CSS_SELECTOR,'[href="#/customers"]')
    customer_menu.click()
    del_info()

if __name__ == '__main__':
    pytest.main("-V -S")

 以上代码参考了以下两篇博文:

https://blog.csdn.net/weixin_44518506/article/details/108183033

https://blog.csdn.net/weixin_45497242/article/details/108538966

本人也是初学自动化测试,还在学习阶段,写博客主要是做学习记录,方便自己以后回顾。

 文章来源地址https://www.toymoban.com/news/detail-746793.html

到了这里,关于selenium+pytest进行自动化测试(项目:白羽黑月SMS)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 自动化测试笔记(selenium+pytest+Allure)

    自动化测试:所有采用程序或代码来替代或辅助人工测试的行为称为自动化测试。 自动化测试好处:提升工作效率 主要应用环节:回归测试、兼容性测试、冒烟测试 适合自动化测试的项目特点: 1、项目需求稳定 2、项目周期较长 3、脚本可以重复使用 selenium:是由thoughtwo

    2024年02月15日
    浏览(48)
  • Pytest插件pytest-selenium-让自动化测试更简洁

    2024软件测试面试刷题,这个小程序(永久刷题),靠它快速找到工作了!(刷题APP的天花板) 在现代Web应用的开发中,自动化测试成为确保网站质量的重要手段之一。而Pytest插件 pytest-selenium 则为开发者提供了简单而强大的工具,以便于使用Python进行Web应用的自动化测试。本

    2024年01月19日
    浏览(37)
  • Pytest插件“pytest-selenium” - 让自动化测试更简洁

    在现代Web应用的开发中,自动化测试成为确保网站质量的重要手段之一。而Pytest插件 pytest-selenium 则为开发者提供了简单而强大的工具,以便于使用Python进行Web应用的自动化测试。本文将深入介绍 pytest-selenium 插件的基本用法和实际案例,助你轻松进入无忧的Web应用测试之旅。

    2024年01月20日
    浏览(33)
  • Pytest+selenium UI自动化测试实战实例

    今天来说说pytest吧,经过几周的时间学习,有收获也有疑惑,总之最后还是搞个小项目出来证明自己的努力不没有白费。 1    确保您已经安装了 python3.x 2    配置 python3+pycharm+selenium2 开发环境     3    安装pytest库 pip install pytest 4    安装pytest -html 报告插件 pip install pytest

    2024年02月05日
    浏览(41)
  • 拥抱自动化测试,快速升职加薪丄Selenium+Pytest自动化测试框架教你如何做到

    引言 Selenium+Pytest自动化测试框架是目前最流行的自动化测试工具之一,其强大的功能和易用性援助许多开发人员和测试人员。 selenium自动化+ pytest测试框架禅道实战 选用的测试网址为我电脑本地搭建的禅道 conftest.py更改 config.ini更改 conf.py更改 page更改 page_element更改 page_obje

    2023年04月24日
    浏览(35)
  • Pytest+Selenium UI自动化测试实战实例(全)

    🍅 视频学习: 文末有免费的配套视频可观看 🍅 关注公众号【互联网杂货铺】,回复 1 , 免费获取软件测试全套资料,资料在手,涨薪更快 今天来说说pytest吧,经过几周的时间学习,有收获也有疑惑,总之最后还是搞个小项目出来证明自己的努力不没有白费 1    确保您

    2024年03月19日
    浏览(54)
  • 2023最全最细的Selenium+Pytest自动化测试框架实战

                                      选前言#   selenium自动化+ pytest测试框架 本章你需要 一定的python基础——至少明白类与对象,封装继承 一定的selenium基础——本篇不讲selenium,不会的可以自己去看selenium中文翻译网 测试框架简介# 测试框架有什么优点呢: 代码复用率高,

    2024年02月06日
    浏览(43)
  • 使用pytest+selenium+allure实现web页面自动化测试

    测试文件 base 基本方法 data 测试数据 page web页面相关操作 image 测试截图 log 日志文件 report 测试报告文件 temp 临时文件 tool 文件读取,发邮件文件 TestCases 测试用例 在page下的__init__.py文件下配置 在base下创建一个webpage.py文件 在base下创建一个driver.py文件 在base下创建一个logger

    2024年02月03日
    浏览(40)
  • UI自动化测试:Selenium+PO模式+Pytest+Allure整合

    本人目前工作中未涉及到WebUI自动化测试,但为了提升自己的技术,多学习一点还是没有坏处的,废话不多说了,目前主流的webUI测试框架应该还是selenium,考虑到可维护性、拓展性、复用性等,我们采用PO模式去写我们的脚本,本文档也主要整合了Selenium+PO模式+Pytest+Allure,下

    2024年02月08日
    浏览(33)
  • Pytest+selenium+allure+Jenkins自动化测试框架搭建及使用

    一、    环境搭建 1.    Python下载及安装 Python可应用于多平台包括windows, Linux 和 Mac OS X, 本文主要介绍windows环境下。你可以通过终端窗口输入 \\\"python\\\" 命令来查看本地是否已经安装Python以及Python的安装版本。     如未安装python, 推荐下载python 3.8.3以上版本,本文主要介绍window

    2024年01月18日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包