class TestAdress(): # setup def setup_class(self): # 实例化统一入口类 self.page = PageIn() # 调用 封装的page_login登录方法 self.page.page_get_login().page_login_address() # 实例化 地址管理页面 self.address = self.page.page_get_address() # 点击地址管理 self.address.page_address_manage() # teardown def teardown_class(self): self.page.driver.quit() # test_new_address @pytest.mark.run(order=1) @pytest.mark.parametrize("name,phone,sheng,shi,qu,addressinfo,postcode", get_data("add")) def test_new_address(self, name, phone, sheng, shi, qu, addressinfo, postcode): # 点击新增地址 self.address.page_address_add_new_btn() # 输入收件人 self.address.page_address_receipt_name(name) # 输入手机号 self.address.page_address_add_phone(phone) # 点击选择区域 self.address.page_address_province(sheng, shi, qu) # 输入详细地址 self.address.page_address_detail_addr_info(addressinfo) # 输入邮编 self.address.page_address_post_code(postcode) # 点击默认 self.address.page_address_default() # 点击保存 self.address.page_click_save() # 断言 try: # 组装字符串 格式:"张三 18610000000" name_phone = name + " " + phone print("组装后的字符串格式:", name_phone) # 获取所有收件人 电话 text_list = self.address.page_get_list_text() print("所有收件人电话为:", text_list) assert name_phone in text_list except: # 截图 self.address.base_get_screenshot() # 失败图片写入报告 with open("./Image/faild.png", "rb") as f: allure.attach("失败原因请查看附加图:", f.read(), allure.attach_type.PNG) # 抛异常 raise @pytest.mark.run(order=2) @pytest.mark.parametrize("name,phone,sheng,shi,qu,addressinfo,postcode", get_data("update")) def test_update_address(self, name, phone, sheng, shi, qu, addressinfo, postcode): # 点击编辑 self.address.page_click_edit() # 点击修改 self.address.page_click_update() # 输入收件人 self.address.page_address_receipt_name(name) # 输入手机号 self.address.page_address_add_phone(phone) # 点击选择区域 self.address.page_address_province(sheng, shi, qu) # 输入详细地址 self.address.page_address_detail_addr_info(addressinfo) # 输入邮编 self.address.page_address_post_code(postcode) # 保存 self.address.page_click_save() # 断言 try: # 组装字符串 格式:"张三 18610000000" name_phone = name + " " + phone # 获取所有收件人 电话 assert name_phone in self.address.page_get_list_text() except: # 截图 self.address.base_get_screenshot() # 失败图片写入报告 with open("./Image/faild.png", "rb") as f: allure.attach("失败原因请查看附加图:", f.read(), allure.attach_type.PNG) # 抛异常 raise @pytest.mark.run(order=3) def test_delete_address(self): # 调用删除方法 self.address.page_delete_address() try: # 断言 列表地址是否删除干净 assert self.address.page_is_delete_ok() except: # 截图 self.address.base_get_screenshot() # 失败图片写入报告 with open("./Image/faild.png", "rb") as f: allure.attach("失败原因请查看附加图:", f.read(), allure.attach_type.PNG) # 抛异常 raise
class TestLogin(): # setup @allure.step("开始执行初始化函数") def setup_class(self): # 实例化统一入口类 allure.attach("步骤描述", "实例化统一入口类") self.page = PageIn() allure.attach("步骤描述", "实例化login页面对象") self.login = self.page.page_get_login() """ 说明,使用统一入口类,调用页面对象的方法是匿名调用的好?还是实名实例化的好 1.如果此类只用一次,一定推荐匿名 2.如果此类使用多次,推荐使用实名 """ allure.attach("步骤描述:", "点击我") self.login.page_click_me() allure.attach("步骤描述:", "点击已有账号去登录") self.login.page_click_name_ok_link() # teardown def teardown_class(self): allure.attach("步骤描述:", "关闭驱动对象") self.login.driver.quit() # 测试方法 @allure.step("开始执行测试脚本") @pytest.mark.parametrize("username,password,expect_result,expect_toast", get_data()) def test_login(self, username, password, expect_result, expect_toast): if expect_result: try: # 输入用户名 self.login.page_input_username(username) # 输入密码 self.login.page_input_password(password) # 点击登录 self.login.page_click_login_btn() # 获取昵称,断言 nickname = self.login.page_get_nickname() allure.attach("开始判断是否登录成功", " ") assert expect_result in nickname allure.attach("登录成功!", " ") # 设置 self.login.page_click_setting() # 滑动 self.login.page_drag_and_drop() # 点击退出 self.login.page_click_exit_btn() # 确认退出,注意:要关注退出后的停留界面,因为循环遍历用例 self.login.page_click_exit_btn_ok() # 点击我 self.login.page_click_me() # 点击 已有账号去登录 self.login.page_click_name_ok_link() except: # 截图 self.login.base_get_screenshot() # 写入报告 with open("./Image/faild.png", "rb") as f: # 写入图片、视频用rb,写入二进制;另外和scripts同级是./,下级是../ # 使用attch参数,第一个为描述,第二个图片流,第三个图片格式 allure.attach("失败原因请看附件图", f.read(), allure.attach_type.PNG) # 抛异常 raise else: try: # 输入用户名 self.login.page_input_username(username) # 输入密码 self.login.page_input_password(password) # 点击登录 self.login.page_click_login_btn() # 断言 """ 说明: 1. 调用获取toast消息封装方法,传入全部匹配值:也就是预期结果,获取元素的文本 2. 让获取结果和预期结果做对比; 注意:这里的 expect_toast和传入的匹配参数是相同的,不要混淆 """ assert expect_toast in self.login.base_get_toast(expect_toast) except: # 截图 self.login.base_get_screenshot() # 失败图片写入报告 with open("./Image/faild.png", "rb") as f: allure.attach("失败原因请查看附加图:", f.read(), allure.attach_type.PNG)
class TestLogin(): # setup_class def setup_class(self): # 实例化 统一入口类 self.page = PageIn() self.login = self.page.page_get_login() """ 说明:使用统一入口类的时候,调用页面对象的方法是匿名调用的好,还是实名实例化好? 1. 如果此类只用一次,一定推荐匿名 2. 如果此类需要用多次,推荐实名 """ """说明:由于8条用例,任何一条都有一个共同点,就是必须先点击我,再点击、已有账号去登录""" # 点击我 self.login.page_click_me() # 点击 已有账号去登录 self.login.page_click_name_ok_link() # teardown_class def teardown_class(self): # 关闭driver 驱动对象 self.login.driver.quit() # test_login """ 注意事项 1). parametrize无法通过助写完成,需要手动编写或粘贴 2). 参数名:单个参数时为字符串;多个参数时,为一个字符串,在一个字符串中使用逗号分隔 如:"username,password,expect_result" 3). 参数值:必须为列表;多个参数时,值为:在表中嵌套元祖 如:[("参数1值","参数2值"),(第二轮)] 4). 应用:在引用函数内的参数名称,必须和参数化设置的名称一致! 5). 在项目中,参数值一定是通过一个自定义函数来获取,看起来不乱! """ @pytest.mark.parametrize("username,password,expect_result", get_data()) def test_login(self, username, password, expect_result): # 输入用户名 self.login.page_input_username(username) # 输入密码 self.login.page_input_pwd(password) # 点击登录 self.login.page_click_login_btn() # 获取昵称 nickname = self.login.page_get_nickname() try: assert expect_result in nickname # 设置 self.login.page_click_setting() # 滑动 推送消息-》修改密码 self.login.page_drag_and_drop() # 点击退出 self.login.page_click_exit_btn() # 确认退出 注意:在此我们要关注,退出后的停留界面,因为循环遍历用例 self.login.page_click_exit_btn_ok() # 下调用例的处理方式 # 点击我 self.login.page_click_me() # 点击 已有账号去登录 self.login.page_click_name_ok_link() except: # 截图 self.login.base_get_screenshot() # 失败图片写入报告 with open("./Image/faild.png", "rb") as f: allure.attach("失败原因请查看附加图:", f.read(), allure.attach_type.PNG) # 抛异常 raise
class TestAddress(): def setup_class(self): # 登录成功 self.page = PageIn() self.page.page_get_login().page_login("18610453007", "123456") # 点击设置 self.page.page_get_login().page_click_setting() # 实例化 PageAddress self.address = self.page.page_get_address() # 点击地址管理 self.address.page_click_address_manage() def teardown_class(self): # 关闭 驱动对象 self.page.driver.quit() # 新增地址 @pytest.mark.run(order=1) @pytest.mark.parametrize( "receipt_name,phone,sheng,shi,qu,address,postcode", get_data("add")) def test_add_address(self, receipt_name, phone, sheng, shi, qu, address, postcode): addr = self.address # 点击 新增地址 addr.page_click_new_address() # 输入 收件人 addr.page_input_receipt_name(receipt_name) # 输入 手机号 addr.page_input_phone(phone) # 选择 所在区域 addr.page_click_area(sheng, shi, qu) # 选择 所在区域2 # addr.page_click_area_zhixia("北京","海淀") # 输入 详细地址 addr.page_input_detail_address(address) # 输入 邮编 addr.page_input_post_code(postcode) # 点击 设为默认地址 addr.page_click_default_address() # 点击 保存 addr.page_click_save() try: # 断言 是否新增成功 # print("获取新增地址联系人和电话为:",addr.page_get_recipit_and_phone()) # assert receipt_name in addr.page_get_recipit_and_phone() """第二种实现断言方法:通过后去地址列表所有的收件和电话""" # 组装字符串,模拟获取文本后的格式: "张三 18600000000" name_phont = receipt_name + " " + phone assert name_phont in addr.page_get_elements_text() except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败描述:", f.read(), allure.attach_type.PNG) # 抛异常 raise # 更新地址 @pytest.mark.run(order=2) @pytest.mark.parametrize( "receipt_name,phone,sheng,shi,qu,address,postcode,toast_expect", get_data("update")) def test_update_address(self, receipt_name, phone, sheng, shi, qu, address, postcode, toast_expect): addr = self.address # 点击 编辑 self.address.page_click_edit_btn() # 点击 修改 self.address.page_click_update() """输入 修改信息""" # 输入 收件人 addr.page_input_receipt_name(receipt_name) # 输入 手机号 addr.page_input_phone(phone) # 选择 所在区域 addr.page_click_area(sheng, shi, qu) # 输入 详细地址 addr.page_input_detail_address(address) # 输入 邮编 addr.page_input_post_code(postcode) # 点击 保存 addr.page_click_save_btn() try: """第一种断言更新是否成功:断言更新后的用户名+电话,是否在地址列表中""" name_phont = receipt_name + " " + phone assert name_phont in addr.page_get_elements_text() """第二种断言更新是否成功:根据toast消息 保存成功""" assert toast_expect in self.address.base_get_toast(toast_expect) except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败描述:", f.read(), allure.attach_type.PNG) # 抛异常 raise # 删除地址 @pytest.mark.run(order=3) def test_delete_address(self): # 删除地址操作 默认删除第一个地址 self.address.page_delete_address() try: # 断言 assert self.address.page_is_delete() except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败描述:", f.read(), allure.attach_type.PNG) # 抛异常 raise
class TestAddress(): @allure.step("初始化操作") def setup_class(self): print("获取数据驱动的添加地址格式:", get_data("add")) print("获取数据驱动的编辑址格式:", get_data("edit")) # 获取driver self.driver = get_driver() # 获取统一入口类 self.page = PageIn(self.driver) # 获取设置页面对象 self.setting = self.page.page_get_setting() allure.attach("描述", "地址管理前登陆") # 登录 +点击设置 self.page.page_get_login().page_login_address() allure.attach("描述", "点击地址管理") # 点击地址管理 self.setting.page_click_address_manage() def teardown_class(self): # 关闭driver self.driver.quit() @pytest.mark.run(order=1) @pytest.mark.parametrize("receipt_name,phone,address_info,post", get_data("add")) def test_add_address(self, receipt_name, phone, address_info, post): allure.attach("描述", "点击新增地址") # 点击新增地址 self.setting.page_click_new_address() allure.attach("描述", "输入收件人") # 输入收件人 self.setting.page_input_receipt_name(receipt_name) allure.attach("描述", "输入手机号") # 输入手机号 self.setting.page_input_phone(phone) allure.attach("描述", "选择所在区域") # 所在区域 # self.setting.page_select_Area() self.setting.page_select_Area_xpath() allure.attach("描述", "输入详细地址") # 详细地址 self.setting.page_input_address_info(address_info) allure.attach("描述", "输入邮编") # 邮编 self.setting.page_input_post_code(post) allure.attach("描述", "设为默认地址") # 默认地址 self.setting.page_click_default_address() allure.attach("描述", "点击保存按钮") # 点击保存按钮 self.setting.page_click_save_btn() try: allure.attach("描述", "开始判断地址是否添加成功") # 断言地址是否添加成功 receipt_name_phone = receipt_name + ' ' + phone assert receipt_name_phone in self.setting.page_get_receipt_name_phone( ) allure.attach("描述", "地址添加成功") allure.attach("描述", "判断是否有默认按钮") # 判断是否有默认按钮 assert self.setting.page_if_default() except: allure.attach("描述", "添加地址或默认地址失败") allure.attach("描述", "开始截图") # 截图 self.setting.base_get_screenshot() img_path = os.getcwd( ) + os.sep + "Image" + os.sep + "faild_img.png" with open(img_path, "rb") as f: allure.attach("截图完成,写入报告", f.read(), allure.attach_type.PNG) allure.attach("描述", "截图成功,并导入报告") raise # 修改地址 @pytest.mark.run(order=2) @allure.step("修改地址") @pytest.mark.parametrize( "text,receipt_name,phone,sheng,shi,qu,address_info,post", get_data("edit")) def test_edit_address(self, text, receipt_name, phone, sheng, shi, qu, address_info, post): # 点击编辑 self.setting.page_click_edit_btn() # 点击修改按钮 self.setting.page_click_elements(text) # 修改地址 self.setting.page_edit_address(receipt_name, phone, sheng, shi, qu, address_info, post) try: # 断言 receipt_name_phone = receipt_name + " " + phone assert receipt_name_phone in self.setting.page_get_receipt_name_phone( ) allure.attach("描述:", "修改地址成功!") except: allure.attach("描述:", "修改地址失败!") # 截图 self.setting.base_get_screenshot() img_path = os.getcwd( ) + os.sep + "Image" + os.sep + "faild_img.png" with open(img_path, "rb") as f: allure.attach("截图完成,写入报告", f.read(), allure.attach_type.PNG) allure.attach("描述", "截图成功,并导入报告") raise # 删除地址 @pytest.mark.run(order=3) def test_delete_address(self): # 点击 编辑按钮 self.setting.page_click_edit_btn() # 删除 self.setting.page_delete_address() try: # 断言 assert self.setting.if_delete_none() allure.attach("描述:", "删除地址地址!") except: allure.attach("描述:", "删除地址失败!") # 截图 self.setting.base_get_screenshot() img_path = os.getcwd( ) + os.sep + "Image" + os.sep + "faild_img.png" with open(img_path, "rb") as f: allure.attach("截图完成,写入报告", f.read(), allure.attach_type.PNG) allure.attach("描述", "截图成功,并导入报告") raise
class TestAddress(): def setup_class(self): # 实例化 登录页面类 self.page = PageIn(get_driver()) self.page.page_get_login().page_login("zhangsan", "123456") # 实例化 PageAddress self.address = self.page.page_get_address() # 点击设置 self.address.page_address_btn_image() # 点击地址管理 self.address.page_address_manage() def teardown_class(self): # 退出driver驱动 self.address.driver.quit() # 地址管理 数据添加 @pytest.mark.run(order=1) @pytest.mark.parametrize("receipt_name,add_phone,sheng,shi,qu,detail_addr,post_code", get_data("add")) def test_add_address(self, receipt_name, add_phone, sheng, shi, qu, detail_addr, post_code): # 点击新增 self.address.page_address_add_new() # 输入 收件人 self.address.page_address_receipt_name(receipt_name) # 输入 手机号 self.address.page_address_add_phone(add_phone) # 选择 所在区域 self.address.page_address_province(sheng, shi, qu) # 输入 详细地址 self.address.page_address_detail_addr(detail_addr) # # 输入 邮编 self.address.page_address_post_code(post_code) # 点击 设置默认地址 self.address.page_address_default() # # 点击 保存 self.address.page_address_button_send() try: # 断言 是否新增成功 # print("获取新增地址联系人和电话为:",self.address.page_get_recipit_and_phone()) assert receipt_name in self.address.page_get_recipit_and_phone() except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败描述:", f.read(), allure.attach_type.PNG) # 抛异常 raise # 地址管理 数据修改 @pytest.mark.run(order=2) @pytest.mark.parametrize("receipt_name,add_phone,sheng,shi,qu,detail_addr,post_code", get_data("update")) def test_update_address(self, receipt_name, add_phone, sheng, shi, qu, detail_addr, post_code): # 点击编辑 self.address.page_address_right_btn() # 点击修改 self.address.page_address_modify() """ 修改内容""" # 输入 收件人 self.address.page_address_receipt_name(receipt_name) # 输入 手机号 self.address.page_address_add_phone(add_phone) # 选择 所在区域 self.address.page_address_province(sheng, shi, qu) # 输入 详细地址 self.address.page_address_detail_addr(detail_addr) # 输入 邮编 self.address.page_address_post_code(post_code) # 点击 保存 self.address.page_address_button_send() try: # 断言 是否修改成功 assert receipt_name in self.address.page_get_recipit_and_phone() except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败描述:", f.read(), allure.attach_type.PNG) # 抛异常 raise # 地址管理 数据删除 @pytest.mark.run(order=3) def test_delect_address(self): # 点击编辑 self.address.page_address_right_btn() # 点击删除 self.address.page_address_delete() # 确认删除 self.address.page_address_delete_ok() try: # 断言 是否修改成功 assert self.address.page_is_delete() except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败描述:", f.read(), allure.attach_type.PNG) # 抛异常 raise
class TestAddress(): def setup_class(self): # 登录成功 self.page = PageIn(get_driver()) self.page.page_get_login().page_login('18610453007', '123456') # 点击设置 self.page.page_get_login().page_click_setting() # 实例化 地址管理页面类 PageAddress self.address = self.page.page_get_address() # 点击 地址管理 self.address.page_click_address_massage() def teardown_class(self): # 退出driver驱动 self.page.driver.quit() '''【地址增加】''' @pytest.mark.run(order=1) @pytest.mark.parametrize( "receipt_name,add_phone,sheng,shi,qu,address,postcode", get_data03()) def test_add_address(self, receipt_name, add_phone, sheng, shi, qu, address, postcode): addr = self.address # 点击 新增地址 addr.page_click_new_address() # 输入 收件人 addr.page_input_receipt_name(receipt_name) # 输入 手机号 addr.page_input_add_phone(add_phone) # 选择 所在区域 addr.page_click_area(sheng, shi, qu) # 选择 所在区域 - -直辖市 # addr.page_click_area02("北京","海淀区") # 输入 详细地址 addr.page_input_detail_address(address) # 输入 邮编 addr.page_input_post_code(postcode) # 点击 设置默认地址 addr.page_click_default() # 点击 保存 addr.page_click_button_save() try: # print('新增地址联系人和电话为:',addr.page_get_receipt_and_phone()) # (断言新增 方法一:) # assert receipt_name in addr.page_get_receipt_and_phone() """方法二:通过后去地址列表所有的收件和电话""" name_phont = receipt_name + " " + add_phone assert name_phont in addr.page_get_elements_text() except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败:", f.read(), allure.attach_type.PNG) raise '''【地址修改】''' @pytest.mark.run(order=2) @pytest.mark.parametrize( "receipt_name,add_phone,sheng,shi,qu,address,postcode,toast_expect", get_data04()) def test_xiugai_address(self, receipt_name, add_phone, sheng, shi, qu, address, postcode, toast_expect): addr = self.address # 点击编辑 addr.page_click_cymtitlebar_right_btn() # 点击修改 addr.page_click_xiugai() # 输入 收件人 addr.page_input_receipt_name(receipt_name) # 输入 手机号 addr.page_input_add_phone(add_phone) # 选择 所在区域 addr.page_click_area(sheng, shi, qu) # 输入 详细地址 addr.page_input_detail_address(address) # 输入 邮编 addr.page_input_post_code(postcode) # 点击 保存 addr.page_click_address_save_btn() try: """方法一:更新后的用户名+电话,是否在地址列表中""" # name_phont=receipt_name+" "+add_phone # assert name_phont in addr.page_get_elements_text() """第二种断言更新是否成功:根据toast消息 保存成功""" assert toast_expect in self.address.base_get_toast(toast_expect) except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败:", f.read(), allure.attach_type.PNG) raise '''【地址删除】''' @pytest.mark.run(order=3) def test_del_address(self): addr = self.address # 删除 addr.page_click_del() try: assert addr.page_is_del() except: # 截图 self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("断言失败:", f.read(), allure.attach_type.PNG) raise
class TestAdress(): # setup def setup_class(self): # 实例化统一入口类 self.page = PageIn() # 调用 封装的page_login登录方法 self.page.page_get_login().page_login_address() # 实例化 地址管理页面 self.address = self.page.page_get_address() # 点击地址管理 self.address.page_address_manage() # teardown def teardown_class(self): self.page.driver.quit() # test_new_address # @pytest.mark.parametrize("name,phone,sheng,shi,qu,addressinfo,postcode",get_data("add")) # def test_new_address(self,name,phone,sheng,shi,qu,addressinfo,postcode): def test_new_address(self, name="张三", phone="18600000000", sheng="河南", shi="郑州", qu="二七", addressinfo="某某路梧桐街38号", postcode="472721"): 点击新增地址 self.address.page_address_add_new_btn() # 输入收件人 self.address.page_address_receipt_name(name) # 输入手机号 self.address.page_address_add_phone(phone) # 点击选择区域 self.address.page_address_province(sheng, shi, qu) # 输入详细地址 self.address.page_address_detail_addr_info(addressinfo) # 输入邮编 self.address.page_address_post_code(postcode) # 点击默认 self.address.page_address_default() # 点击保存 self.address.page_click_save() # # 断言 # try: # # 组装字符串 格式:"张三 18610000000" # name_phone=name+" "+phone # print("组装后的字符串格式:",name_phone) # # 获取所有收件人 电话 # text_list=self.address.page_get_list_text() # print("所有收件人电话为:",text_list) # assert name_phone in text_list # except: # # 截图 # self.login.base_get_screenshot() # # 失败图片写入报告 # with open("./Image/faild.png", "rb") as f: # allure.attach("失败原因请查看附加图:", f.read(), allure.attach_type.PNG) # # 抛异常 # raise @pytest.mark.parametrize("name,phone,sheng,shi,qu,addressinfo,postcode", get_data("update")) def test_update_address(self, name, phone, sheng, shi, qu, addressinfo, postcode): # 点击编辑 self.address.page_click_edit() # 点击修改 self.address.page_click_update() # 输入收件人 self.address.page_address_receipt_name(name) # 输入手机号 self.address.page_address_add_phone(phone) # 点击选择区域 self.address.page_address_province(sheng, shi, qu) # 输入详细地址 self.address.page_address_detail_addr_info(addressinfo) # 输入邮编 self.address.page_address_post_code(postcode) # 保存 self.address.page_click_save()
class TestAddress02(): def setup_class(self): self.page = PageIn(get_driver()) self.page.page_get_login().page_login("13805489414", "123456") #点击设置 self.page.page_get_login().page_click_setting() #点击地址管理 self.address = self.page.page_get_setting() self.address.page_address_manage() def teardown_class(self): self.page.driver.quit() """新增""" @pytest.mark.run(order=1) @pytest.mark.parametrize("name,phone,sheng,shi,qu,address,youbian", get_data("add")) def test02_address(self, name, phone, sheng, shi, qu, address, youbian): addr = self.address #点击新增地址 addr.page_new_address() #输入收件人 addr.page_receipt_name(name) #输入手机号 addr.page_add_phone(phone) #选择所在区域 addr.page_address_area(sheng, shi, qu) #输入详细地址 addr.page_detail_addr_info(address) #输入邮编 addr.page_address_post_code(youbian) #设置默认地址 addr.page_address_default() #点击保存 addr.page_address_save() #断言,是否新增成功 try: # print("获取新增地址联系人电话",addr.page_get_recipit_and_phone()) # assert name in addr.page_get_recipit_and_phone() """第二种实现断言方法:所有收件和电话""" #组装字符串 name_phone = name + " " + phone assert name_phone in addr.page_get_elements_text() except: self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("失败描述", f.read(), allure.attach_type.PNG) raise """更新地址""" @pytest.mark.run(order=2) @pytest.mark.parametrize("name,phone,sheng,shi,qu,address,youbian,toast", get_data("update")) def test_update_address(self, name, phone, sheng, shi, qu, address, youbian, toast): addr = self.address #点击编辑 self.address.page_click_edit_btn() #点击修改 self.address.page_click_xiugai() #点击信息 #输入收件人 addr.page_receipt_name(name) #输入手机号 addr.page_add_phone(phone) #选择所在区域 addr.page_address_area(sheng, shi, qu) #输入详细地址 addr.page_detail_addr_info(address) addr.page_address_post_code(youbian) #点击保存 addr.page_click_baocun_btn() try: name_phone = name + " " + phone assert name_phone in addr.page_get_elements_text() #第二种toast assert toast in self.address.base_get_toast(toast) except: self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("失败描述", f.read(), allure.attach_type.PNG) raise """"删除""" @pytest.mark.run(order=3) def test_delete_address(self): #删除操作 self.address.page_delete_address() #断言 try: assert self.address.page_is_delete_ok() except: self.login.base_getImage() with open("./Image/faild.png", "rb") as f: allure.attach("失败描述", f.read(), allure.attach_type.PNG) raise
class TestLogin(): # setup_class @allure.step("开始执行初始化函数") def setup_class(self): # 实例化 统一入口类 allure.attach("步骤描述:", "实例化统一入口类") self.page = PageIn() allure.attach("步骤描述:", "实例化login页面对象") """ PageIn():统一入口类,可以通过此类获取Page页面对象 self.page:为PageIn()实例化对象引用名称 page_get_login(): PageIn()类内的一个获取PageLogin页面对象方法 传参数: 1. 封装的时候所定义的参数,都为形参(虚拟占位符) 2. 在应用函数的时候,如果发现有参数站位符号,调用的时候必须传进来 """ self.login = self.page.page_get_login() """ 说明:使用统一入口类的时候,调用页面对象的方法是匿名调用的好,还是实名实例化好? 1. 如果此类只用一次,一定推荐匿名 2. 如果此类需要用多次,推荐实名 """ """说明:由于8条用例,任何一条都有一个共同点,就是必须先点击我,再点击、已有账号去登录""" # 点击我 allure.attach("步骤描述:", "点击 我") self.login.page_click_me() # 点击 已有账号去登录 allure.attach("步骤描述:", "点击已有账号去登录") self.login.page_click_name_ok_link() # teardown_class def teardown_class(self): # 关闭driver 驱动对象 allure.attach("步骤描述:", "关闭驱动对象") self.login.driver.quit() # test_login """ 注意事项 1). parametrize无法通过助写完成,需要手动编写或粘贴 2). 参数名:单个参数时为字符串;多个参数时,为一个字符串,在一个字符串中使用逗号分隔 如:"username,password,expect_result" 3). 参数值:必须为列表;多个参数时,值为:在表中嵌套元祖 如:[("参数1值","参数2值"),(第二轮)] 4). 应用:在引用函数内的参数名称,必须和参数化设置的名称一致! 5). 在项目中,参数值一定是通过一个自定义函数来获取,看起来不乱! 6). 注意:参数的个数,一定不能少; """ @allure.step("开始执行登录脚本...") @pytest.mark.parametrize("username,password,expect_result,expect_toast", get_data()) def test_login(self, username, password, expect_result, expect_toast): # 如果expect_result不为空,说明为正向用例,执行以下操作步骤 if expect_result: try: # 输入用户名 self.login.page_input_username(username) # 输入密码 self.login.page_input_pwd(password) # 点击登录 self.login.page_click_login_btn() # 获取昵称 nickname = self.login.page_get_nickname() allure.attach("开始判断是否登录成功", " ") assert expect_result in nickname allure.attach("登录成功!", " ") # 设置 self.login.page_click_setting() # 滑动 推送消息-》修改密码 self.login.page_drag_and_drop() # 点击退出 self.login.page_click_exit_btn() # 确认退出 注意:在此我们要关注,退出后的停留界面,因为循环遍历用例 self.login.page_click_exit_btn_ok() # 下调用例的处理方式 # 点击我 self.login.page_click_me() # 点击 已有账号去登录 self.login.page_click_name_ok_link() except: # 截图 self.login.base_get_screenshot() # 失败图片写入报告 with open("./Image/faild.png", "rb") as f: allure.attach("失败原因请查看附加图:", f.read(), allure.attach_type.PNG) # 抛异常 raise else: try: # 输入用户名 self.login.page_input_username(username) # 输入密码 self.login.page_input_pwd(password) # 点击登录 self.login.page_click_login_btn() # 断言 """ 说明: 1. 调用获取toast消息封装方法,传入全部匹配值:也就是预期结果,获取元素的文本 2. 让获取结果和预期结果做对比; 注意:这里的 expect_toast和传入的匹配参数是相同的,不要混淆 """ assert expect_toast in self.login.base_get_toast(expect_toast) except: # 截图 self.login.base_get_screenshot() # 失败图片写入报告 with open("./Image/faild.png", "rb") as f: allure.attach("失败原因请查看附加图:", f.read(), allure.attach_type.PNG) # 抛异常 raise
class TestLogin(): @allure.step("实例化 PageIn") def setup_class(self): # 实例化 PageIn self.page = PageIn() self.login = self.page.page_get_login() # 点击 我 self.login.page_click_login_me() # 点击 已有账号,登录 self.login.page_login_name_ok_link() @allure.step("关闭驱动") def teardown_class(self): # 关闭驱动 allure.attach("关闭驱动", "") self.login.driver.quit() # 添加步骤描述 @allure.step("开始执行测试登录脚本...") # 参数化 @pytest.mark.parametrize("username,password,expect_result,expect_toast", get_data()) def test_login(self, username, password, expect_result, expect_toast): # 登录 正向逆向逻辑问题 # 正向登录 if expect_result: try: # 输入用户名 self.login.page_login_username(username) # 输入密码 self.login.page_login_password(password) # 点击登录 self.login.page_login_btn() # 获取昵称 result_nick = self.login.page_login_nickname() # 断言 assert expect_result in result_nick # 点击设置 self.login.page_login_setting() # 滑动 从消息推送-->滑到-->修改密码 self.login.page_drag_drop() # 点击 退出 self.login.page_login_logout() # 确认 退出 self.login.page_login_logout_ok() # 点击 我 self.login.page_click_login_me() # 点击 已有账号,登录 self.login.page_login_name_ok_link() except Exception: # 截图 self.login.base_get_screenshot() # 失败图片写入报告 with open("./Imags/faild.png", "rb") as f: allure.attach("正向登录失败原因", f.read(), allure.attach_type_PNG) # 抛异常 raise # 逆向登录 else: try: # 输入用户名 self.login.page_login_username(username) # 输入密码 self.login.page_login_password(password) # 点击登录 self.login.page_login_btn() # 获取 toast result_toast = self.login.base_get_toast(expect_toast) # 断言 allure.attach("开始断言...", "") assert expect_toast in result_toast except Exception: # 截图 self.login.base_get_screenshot() # 失败图片写入报告 with open("./Imags/faild.png", "rb") as f: allure.attach("逆向登录失败原因", f.read(), allure.attach_type_PNG) # 抛异常 raise
class TestLogin(): def setup_class(self): # 获取driver self.driver = get_driver() # 实例化统一入口类 self.page = PageIn(self.driver) # 实例化登录页面对象 self.login = self.page.page_get_login() # 实例化设置页面对象 self.setting = self.page.page_get_setting() # 点击我的 self.login.page_click_me() # 点击已有账号去登录 self.login.page_click_login_link() def teardown_class(self): self.driver.quit() @pytest.mark.parametrize("username,password,expect_nickname,expect_toast", get_data()) @allure.step("开始登录操作") def test_login(self, username, password, expect_nickname, expect_toast): # 把登录页面对象赋值给login 便于调用 login = self.login setting = self.setting # 如果expect_nickname不为空,说明为正向数据,执行以下步骤 if expect_nickname: try: allure.attach("开始登录", "username:%s,password%s" % (username, password)) # 输入用户名和密码 进行登录 login.page_login(username, password) # 断言是否登录成功 assert expect_nickname in login.page_get_nickname() allure.attach("登录成功", "username:%s,password%s" % (username, password)) # 点击设置 login.page_click_setting() allure.attach("退出操作", "开始退出") # 点击退出 setting.page_drag_and_drop() # 断言 退出是否成功 assert setting.page_if_logout() allure.attach("退出状态", "退出成功!") # 点击我的 login.page_click_me() # 点击已有账号 login.page_click_login_link() except: # 截图 login.base_get_screenshot() # 逆向数据(登录失败数据)执行流程 else: allure.attach("开始校验类数据验证", "usrename:'%s',password:'******'" % (username, password)) # 调用登录方法 login.page_login(username, password) try: allure.attach("开始验证", "预期toast消息为:%s" % expect_toast) # 断言 assert expect_toast in login.base_get_toast(expect_toast) allure.attach("验证成功", "预期toast:%s和捕获toast提示信息相符合" % expect_toast) except: allure.attach("开始截图", "捕获toast消息与预期toast消息不符合") # 截图 login.base_get_screenshot() img_path = os.getcwd( ) + os.sep + "Image" + os.sep + "faild_img.png" with open(img_path, "rb") as f: allure.attach("截图完成,写入报告", f.read(), allure.attach_type.PNG) raise