@ddt class ExcelText(unittest.TestCase): def setUp(self) -> None: self.driver = webdriver.Chrome() #测试用网站地址 self.driver.get('http://oa.newhopegroup.com:8094/sso/login?service=http://oa.newhopegroup.com:8090/sys/portal/page.jsp') self.driver.maximize_window() def tearDown(self) -> None: driver = self.driver sleep(3) driver.quit() @data(read_excel()) #读取xlsx @unpack def test_excel01(self,username, password): print(username, password) driver = self.driver sleep(2) driver.find_element(By.XPATH,'//').send_keys(username) driver.find_element(By.XPATH,'//[@id="password"]').send_keys(password) driver.find_element(By.XPATH,'///table/tbody/tr[3]/td/a/div/div/div').click() sleep(2) if name == 'main': unittest.main()我想在这个代码中增加一个没有成功跳转页面就记录的代码
时间: 2024-04-04 17:29:25 浏览: 59
http接口测试框架全套,基于Unittest+ddt,实现注册-登录-充值-加标-核标-投资全套功能.zip
您可以在 `test_excel01` 方法中添加一个判断是否跳转成功的代码块,并在跳转失败时记录下来。同样可以使用 Python 内置的 `logging` 模块来实现记录操作。具体代码如下:
```python
import logging
@data(read_excel())
@unpack
def test_excel01(self, username, password):
logging.info(f"Start testing with username: {username} and password: {password}")
driver = self.driver
driver.find_element(By.XPATH, '//').send_keys(username)
driver.find_element(By.XPATH, '//[@id="password"]').send_keys(password)
driver.find_element(By.XPATH, '///table/tbody/tr[3]/td/a/div/div/div').click()
sleep(2)
if "登录失败" in driver.page_source:
logging.error(f"Failed to login with username: {username} and password: {password}")
elif "页面跳转失败" in driver.page_source:
logging.error(f"Failed to jump to home page with username: {username}")
else:
logging.info(f"Successfully logged in and jumped to home page with username: {username}")
```
在上述代码中,我们增加了一个针对页面跳转失败的判断。如果在登录成功后发现页面中有 "页面跳转失败" 的字样,就记录下跳转失败的信息。否则记录下登录成功并成功跳转到首页的信息。
阅读全文