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

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

创建一个login_success.py文件

from selenium.webdriver.common.by import Bydef 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 pytestfrom  login_success import login_successfrom selenium import webdriverfrom selenium.webdriver.common.by import Byimport timefrom selenium.webdriver.support.ui import Selectdriver=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

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