def before(self): self.driver = init_driver() self.page_factory = PageFactory(self.driver) yield time.sleep(2) self.driver.quit()
class TestLogin(object): """搜索测试用例""" @pytest.fixture(autouse=True) def before_func(self): self.driver = get_driver() self.page_factory = PageFactory(self.driver) # 实例化页面对象统一入口类 yield time.sleep(3) self.driver.quit() # 退出驱动对象 @pytest.mark.parametrize('phone, pwd', build_data_func()) @allure.MASTER_HELPER.step(title='登陆测试步骤001') # 添加测试步骤 第二代语法 @allure.MASTER_HELPER.severity( allure.MASTER_HELPER.severity_level.CRITICAL ) # 作⽤: 测试⽤例设置不同的严重级别, 可以帮助测试和开发⼈员更直观的关注重要Case def test_login_func(self, phone, pwd): """搜索测试方法""" # 点击我的按钮 跳转我的页面 allure.MASTER_HELPER.attach("点击我的按钮", "跳转我的页面") self.page_factory.index_page().click_me_btn() time.sleep(2) # 点击登录注册按钮 跳转登录页面 allure.MASTER_HELPER.attach("点击登录注册按钮", "跳转登录页面") self.page_factory.me_page().click_login_sign_btn() # 输入账号密码登录 allure.MASTER_HELPER.attach("登录", "输入账号密码登录") self.page_factory.login_page().input_phone_box(phone) self.page_factory.login_page().input_pwd_box(pwd) # 点击登录按钮 self.page_factory.login_page().click_login_btn()
def befort_fun(self): """前置操作""" self.driver = ini_driver() # 获取驱动对象 self.page_factory = PageFactory(self.driver) yield time.sleep(2) self.driver.quit()
def before_func(self): DriverUtil.get_driver() # 获取驱动对象 self.factory = PageFactory() yield # 结束操作 time.sleep(3) DriverUtil.quit_driver() # 退出驱动对象
def before_function(self): # 获取驱动对象 self.driver = get_driver() self.page_factory = PageFactory(self.driver) # 实例化统一入口类 yield # 结束 # time.sleep(3) self.driver.quit() # 关闭驱动对象
class TestLogin(object): def setup(self): self.driver = init_driver() # 驱动对象 self.factory = PageFactory(self.driver) # 工厂类对象 def teardown(self): self.driver.quit() # 退出 @pytest.mark.parametrize('name,pwd', build_login_data()) def test_login(self, name, pwd): self.factory.homepage().click_minepage() # 点击我的 self.factory.minepage().click_login_btn() # 点击我的页面登录 self.factory.loginpage().input_name(name) # 账号 self.factory.loginpage().input_pwd(pwd) # 密码 self.factory.loginpage().click_login_bar() # 登录
class TestLogin(object): """登陆测试类""" @pytest.fixture(autouse=True) def befort_fun(self): """前置操作""" self.driver = ini_driver() # 获取驱动对象 self.page_factory = PageFactory(self.driver) yield time.sleep(2) self.driver.quit() @pytest.mark.parametrize("num,pwd,expect", build_login_data()) def test_login(self, num, pwd, expect): """测试登陆方法""" # # self.page_factory.index_page().click_mine() # 点击我的 # self.page_factory.mine_page().click_login_ret() # 点击登录/注册 # self.page_factory.login_page().input_phone_num("13580084109") # 输入账号 # self.page_factory.login_page().input_pwd("qiya77a369") # 输入密码 # self.page_factory.login_page().click_login_but() # 点击登录 # self.page_factory.login_page().click_alert_but() # 点击弹出确认按钮 self.page_factory.index_page().click_mine() # 点击我的 self.page_factory.mine_page().click_login_ret() # 点击登录/注册 self.page_factory.login_page().logint_func(num, pwd) # 登陆操作 username_text = self.page_factory.mine_page().get_usernmae_text( ) # 用户名标题 assert username_text in expect # 判断断言结果
class TestLogin(object): """登录测试类""" @pytest.fixture(autouse=True) def before_func(self): """前置操作""" self.driver = init_driver() # 获取驱动对象 self.page_factory = PageFactory(self.driver) yield time.sleep(2) self.driver.quit() def test_login(self): """登录测试方法""" # self.page_factory.index_page().click_mine() # 点击我的 # self.page_factory.mine_page().click_login_reg() # 点击登录注册 # self.page_factory.login_page().input_phone_num('15570925914') # 输入手机号码 # self.page_factory.login_page().input_pwd('qwer123456789') # 输入密码 # self.page_factory.login_page().click_login_btn() # 点击登录按钮 # self.page_factory.login_page().click_alert_btn() # 点击弹窗按钮 self.page_factory.index_page().click_mine() # 点击我的 self.page_factory.mine_page().click_login_reg() # 点击登录注册 self.page_factory.login_page().login_func('15570925914', 'qwer123456789') # 登录操作
class TestLogin(object): @pytest.fixture(autouse=True) def before_func(self): self.driver = init_driver() self.page_factory = PageFactory(self.driver) yield time.sleep(1) self.driver.quit() def test_login(self): self.page_factory.index_page().clicl_mine() self.page_factory.mine_page().click_login_reg() self.page_factory.login_page().input_phone_num(12345678901) self.page_factory.login_page().input_pwd(123456) self.page_factory.login_page().click_login_btn()
class TestLogin(object): """搜索测试类""" @pytest.fixture(autouse=True) def before_func(self): DriverUtil.get_driver() # 获取驱动对象 self.factory = PageFactory() yield # 结束操作 time.sleep(3) DriverUtil.quit_driver() # 退出驱动对象 def test_login(self): self.factory.index_page().click_mine() self.factory.mine_page().click_login() self.factory.login_page().login('13157523435', 'lm123456') self.factory.login_page().click_confirm() # 断言 username_text = self.factory.mine_page().get_username_text() assert '3435' in username_text
class TestSearch(object): """搜索测试类""" @pytest.fixture(autouse=True) def before_func(self): self.driver = get_driver() # 获取驱动对象 self.page_factory = PageFactory(self.driver) # 实例化统一入口类 yield # 结束操作 time.sleep(3) self.driver.quit() # 退出驱动对象 # def test_func(self): # """搜索测试方法""" # # self.page_factory.index_page().click_search_btn() # 点击搜索按钮跳转搜索页面 # self.page_factory.search_page().input_search_bar('黑马程序员') # 输入内容搜索结果 @pytest.mark.parametrize('text', build_data_func()) # 1.调用参数化方法, 传入测试数据 def test_func(self, text): # 2.声明参数 """搜索测试方法""" self.page_factory.index_page().click_search_btn() # 点击搜索按钮跳转搜索页面 self.page_factory.search_page().input_search_bar( text) # 输入内容搜索结果 # 3. 调用参数
class TestSearch(object): # 前置条件 @pytest.fixture(autouse=True) def before_func(self): self.driver = get_driver() self.page_factory = PageFactory(self.driver) yield self.driver.quit() @pytest.mark.parametrize('text', build_data_func()) def test_func(self, text): """搜索测试方法""" # # 点击搜索按钮跳转搜索页面 # self.index_page = IndexPage(self.driver) # 实例化设置首页页面对象 # self.index_page.click_search_btn() # 实例化设置首页页面对象 # self.search_page = SearchPage(self.driver) # 点击搜索按钮跳转搜索页面 # self.search_page.input_search_bar("你好") self.page_factory.index_page().click_search_btn() # 点击搜索按钮跳转搜索页面 self.page_factory.search_page().input_search_bar(text) # 输入内容搜索结果
class TestLogin(object): """登录测试类""" @pytest.fixture(autouse=True) def before_function(self): # 获取驱动对象 self.driver = get_driver() self.page_factory = PageFactory(self.driver) # 实例化统一入口类 yield # 结束 # time.sleep(3) self.driver.quit() # 关闭驱动对象 def test_login(self): """登录测试方法""" # 登录成功用例 # self.page_factory.index_page().click_my() # 点击我的跳转页面 # self.page_factory.my_page().clik_login_link()# 点击登录连接跳转登录页面 # self.page_factory.login_page().login_function("18739223899","123456") # 登录 # self.page_factory.login_page().click_alert() # 点击确定弹框 # # 断言昵称 # element = self.page_factory.my_page().nickname_text() # assert "用户_4514" in element # 未注册 self.page_factory.index_page().click_my() # 点击我 self.page_factory.my_page().clik_login_link() # 点击登录连接 self.page_factory.login_page().login_function("13800000000", "123456") # 登录 # 捕捉异常 try: # 断言 toast_msg = self.page_factory.login_page().login_get_toast( "账号还未注册") assert "账号还未注册" in toast_msg except AssertionError as e: # 获取时间戳 new_time = time.strftime("%Y%m%d_%H%M%S") # 给allure报告添加截图断言失败时进行截图 的命令:allure serve report # f"./screenshot/info_{new_time}.png" 在Terminal里使用pytest执行,不然会报错 # 如果在当前右键点击执行 路径要这样写f"../screenshot/info_{new_time}.png" self.driver.get_screenshot_as_file( f"./screenshot/info_{new_time}.png") # 断言失败时进行截图 # rb以二进制方式读取 # allure.MASTER_HELPER.attach(文件名称, 读取文件内容,文件类型) with open(f"./screenshot/info_{new_time}.png", "rb") as f: allure.MASTER_HELPER.attach( "my_info", f.read(), allure.MASTER_HELPER.attach_type.PNG) raise e # 当截图操作完成后,应该还远测试用例的真是结果,因此需要再主动抛出异常
class TestLogin(object): """登录测试类""" @pytest.fixture(autouse=True) def before_func(self): self.driver = get_driver() # 获取驱动对象 self.page_factory = PageFactory(self.driver) # 实例化统一入口类 yield # 结束工作 self.driver.quit() def test_login(self): """登录测试方法""" # 成功登录 # self.page_factory.index_page().click_mine() # 点击我的 # self.page_factory.mine_page().click_login() # 点击登录/注册 # self.page_factory.login_page().login_func('13157523435', 'lm123456') # 登录 # self.page_factory.login_page().click_confirm_btn() # 点击登录提示确定按钮 # # 断言 # username_text = self.page_factory.mine_page().get_username_text() # assert '3435' in username_text # 未注册 self.page_factory.index_page().click_mine() # 点击我的 self.page_factory.mine_page().click_login() # 点击登录/注册 self.page_factory.login_page().login_func('13800001111', 'lm123456') # 登录 try: # 断言 toast_msg = self.page_factory.login_page().get_login_toast( '账号还未注册') assert '账号还未注册' in toast_msg except AssertionError as e: # AssertionError: 为断言失败异常类型 # 给 allure 报告添加截图(断言失败时进行截图操作) self.driver.get_screenshot_as_file('./screenshot/info.png') # rb: 以二进制方式读取 with open('./screenshot/info.png', 'rb') as f: # allure.MASTER_HELPER.attach('文件名称', 文件内容, 文件类型) allure.MASTER_HELPER.attach( 'my_info', f.read(), allure.MASTER_HELPER.attach_type.PNG) raise e # 当截图操作完成后, 应该还原测试用例的真实结果, 因此需要再主动抛出异常
class TestLogin(object): """登录测试类""" def setup_class(self): self.driver = init_driver() # 驱动对象获取 self.page_factory = PageFactory(self.driver) # 工厂类实例化 def teardown_class(self): self.driver.quit() # 退出驱动对象 def test_login(self): """测试登录方法""" self.page_factory.home_page().click_mine() # 点击我的 self.page_factory.mine_page().click_login() # 点击登录注册 self.page_factory.login_page().input_username("13058070271") # 输入账号 self.page_factory.login_page().input_password("zm4567890") # 输入密码 self.page_factory.login_page().click_login_btn() # 点击登录 self.page_factory.login_page().click_con_btn() # 点击确认 nick_name = self.page_factory.login_page().get_nick_name() # 获取昵称 print("昵称是:", nick_name) # 断言判断结果 assert 5372 in nick_name
def setup(self): self.driver = init_driver() # 驱动对象 self.factory = PageFactory(self.driver) # 工厂类对象
def setup_class(self): self.driver = init_driver() # 驱动对象获取 self.page_factory = PageFactory(self.driver) # 工厂类实例化
class TestLogin(object): @pytest.fixture(autouse=True) def before(self): self.driver = init_driver() self.page_factory = PageFactory(self.driver) yield time.sleep(2) self.driver.quit() @pytest.mark.parametrize('type,username,pwd,expect', read_data()) def test_login(self, type, username, pwd, expect): if type: # 点击我的 self.page_factory.first_page().click_mine_btn() # 点击登录按钮 self.page_factory.mine_page().click_log_reg_btn() # 输入用户名 self.page_factory.login_page().input_username(username) #输入密码 self.page_factory.login_page().input_password(pwd) #点击登录 self.page_factory.login_page().click_login_btn() #点击弹出框 self.page_factory.login_page().click_alter_btn() # #正向 # self.page_factory.login_page().login_func(username,pwd) # #获取用户名 # username_text = self.page_factory.mine_page().get_username_text() # # print(username_text) # #断言 # assert username_text in expect else: #反向 # 点击我的 self.page_factory.first_page().click_mine_btn() # 点击登录按钮 self.page_factory.mine_page().click_log_reg_btn() # 输入用户名 self.page_factory.login_page().input_username(username) # #输入密码 self.page_factory.login_page().input_password(pwd) # #点击登录 self.page_factory.login_page().click_login_btn() # 截图 image_name = BASE_DIR + '/image/info_{}.png'.format( time.strftime('%Y%m%d_%H%M%S')) self.driver.get_screenshot_as_file(image_name) # 注意: 需要打开生成截图文件, 再写入 allure 报告 with open(image_name, 'rb') as f: # 注意: 写入语法必须保证在 with open() 方法的作用域范围内 allure.MASTER_HELPER.attach( 'info', f.read(), allure.MASTER_HELPER.attach_type.PNG) # #获取弹出信息 toast_msg = self.page_factory.login_page().get_toast_message( expect) print(toast_msg) #断言 assert expect in toast_msg
def before_func(self): self.driver = get_driver() # 获取驱动对象 self.page_factory = PageFactory(self.driver) # 实例化统一入口类 yield # 结束工作 self.driver.quit()
def before_func(self): # 相当于setup() self.driver = get_driver() self.pagefactory = PageFactory(self.driver) # 实例化统一入口类 yield self.driver.quit()
def setup(self): self.driver = init_driver() # 获取驱动对象 self.page_factory = PageFactory(self.driver) # 工厂类实例对象
def setup(self): self.driver = init_driver() self.page_factory = PageFactory(self.driver)
def before_func(self): self.driver = get_driver() self.page_factory = PageFactory(self.driver) # 实例化页面对象统一入口类 yield time.sleep(3) self.driver.quit() # 退出驱动对象
def before_func(self): self.driver = get_driver() self.page_factory = PageFactory(self.driver) yield self.driver.quit()