开头
用配置好的 selenium 进行一个简单的测试用例的编写, 可以参考 allure 的美化这一遍博文 https://www.cnblogs.com/c-keke/p/14837766.html
代码编写
新建一个测试用例 test_02.py, 开启一个远程 selenium 调试, 编写如下代码
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- '''
- @File :test_02.py
- @Describe :
- @Create :2021/06/23 00:16:26
- @Author :od
- '''
- import pytest
- import time
- from selenium import webdriver
- from selenium.webdriver.Chrome.options import Options
- class Testerhome:
- def setup(self):
- self.chrome_options = Options()
- self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # 指定配置好的 chrom
- self.chrome_driver = r"./chromedriver.exe" # 驱动路径
- self.driver = webdriver.Chrome(self.chrome_driver, chrome_options=self.chrome_options) # 加入驱动设置
- self.driver.get('https://testerhome.com/') # 发起请求
- self.driver.implicitly_wait(3) # 添加一个隐式等待默认等待 3 秒
- def teardown(self):
- print('关闭浏览器')
- time.sleep(1)
- # self.driver.quit()
- # 测试用例如果不加 sleep 的话, 元素如果没加载出来, 是会报错的, 所以我们要加个隐式等待
- def test_hogwards(self):
- self.driver.find_element_by_xpath("//a[contains(text(),'社团')]").click() # 点击涉毒案
- self.driver.find_element_by_xpath("//a[contains(text(),'求职面试圈')]").click() # 选择一个活跃的社团
- self.driver.find_element_by_xpath("//a[@title='面试瓶颈']").click() # 点击一个帖子
- print('go')
执行: pytest -vs test_02 即可得到结果
来源: http://www.bubuko.com/infodetail-3787491.html