给我一个用断言结果执行下一步的例子

在使用 pytestSelenium 进行自动化测试时,通常我们会根据断言的结果来决定测试流程的走向。如果断言失败,测试通常会停止执行后续的步骤,因为失败意味着被测系统没有按照预期工作。然而,有时候我们可能需要在断言失败后执行特定的操作,比如记录错误信息或截图以便调试。

下面是一个示例,展示了如何在断言失败后继续执行下一步操作:

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

class TestWebPage:
    def setup_method(self):
        self.driver = webdriver.Chrome()

    def teardown_method(self):
        self.driver.quit()

    def test_button_click(self):
        self.driver.get('http://example.com')
        try:
            # 尝试查找并点击按钮
            button = self.driver.find_element(By.ID, 'myButton')
            button.click()
            
            # 检查页面上的某个元素是否出现
            success_message = self.driver.find_element(By.CLASS_NAME, 'success-message').text
            assert 'Success' in success_message, "Success message not found"
        except AssertionError as e:
            # 断言失败时的操作
            print("Assertion failed:", str(e))
            # 可以在这里添加更多操作,如截图、记录日志等
            self.driver.save_screenshot('failed_assertion.png')
        finally:
            # 无论断言是否失败都会执行的操作
            print("Test execution completed.")

if __name__ == "__main__":
    pytest.main(['-v', 'test_example.py'])

在这个例子中,我们使用了 try-except-finally 结构来处理断言失败的情况。在 try 块中,我们执行了点击按钮和断言检查的代码。如果断言失败(即 assert 语句引发了一个 AssertionError),except 块会被执行,我们在这里打印了错误信息,并保存了一个截图。finally 块中的代码无论断言是否失败都会被执行,这里我们简单地打印了一条消息表明测试执行完成。

请注意,虽然在断言失败后继续执行代码是有用的,但在实际的测试脚本中,我们应该确保这种“失败后的操作”不会影响测试的正确性和完整性。通常,这些操作应该是辅助性的,比如记录信息,而不是改变应用程序的状态或测试的结果。

…………………………

当然,让我们来看一个更具体的例子,展示如何在断言失败后执行下一步操作。在这个例子中,我们将使用 pytestSelenium 来编写一个测试脚本,该脚本会测试一个表单提交功能。如果表单提交成功,我们期望看到一个确认消息。如果提交失败,我们将在页面上查找错误消息,并根据情况执行不同的操作。

首先,确保你已经安装了必要的库:

pip install pytest selenium

然后,创建一个名为 test_form_submission.py 的文件,内容如下:

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class TestFormSubmission:
    def setup_method(self):
        self.driver = webdriver.Chrome()

    def teardown_method(self):
        self.driver.quit()

    def test_submit_form(self):
        self.driver.get('http://example.com/form')
        
        # 填写表单
        input_field = self.driver.find_element(By.NAME, 'inputField')
        input_field.send_keys('Test Input')
        
        # 提交表单
        submit_button = self.driver.find_element(By.NAME, 'submitButton')
        submit_button.click()
        
        # 等待确认或错误消息出现
        wait = WebDriverWait(self.driver, 10)
        try:
            confirmation_message = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.confirmation-message')))
            assert 'Success' in confirmation_message.text, "Form submission was not successful"
            print("Form submitted successfully.")
        except AssertionError:
            # 断言失败,查找错误消息
            error_message = self.driver.find_element(By.CSS_SELECTOR, '.error-message')
            print("Form submission failed with error:", error_message.text)
            # 执行下一步操作,例如截图或记录日志
            self.driver.save_screenshot('form_submission_error.png')
            # 发送邮件通知错误(伪代码)
            self.send_email_notification(error_message.text)
        except Exception as e:
            print("An unexpected error occurred:", str(e))

    def send_email_notification(self, error_message):
        # 这里可以实现发送邮件的功能
        pass

if __name__ == "__main__":
    pytest.main(['-v', 'test_form_submission.py'])

在这个例子中,我们首先填写了一个表单并提交。然后,我们使用 WebDriverWait 等待确认消息的出现。如果确认消息中包含预期的文本 'Success',我们就认为表单提交成功。如果断言失败,我们会查找错误消息,并执行一些后续操作,比如保存截图和发送邮件通知。

请注意,这个例子中的 http://example.com/form 和表单字段的名称都是假设的,你需要替换为你实际要测试的网页和元素的属性。此外,发送邮件的函数 send_email_notification 是一个占位符,你需要根据实际情况实现这个功能。

要运行这个测试,你可以直接运行 test_form_submission.py 文件,或者在命令行中输入 pytest -v test_form_submission.py

……..........

 

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PyAIGCMaster

1毛钱也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值