在Selenium中,如果测试用例的步骤失败,是否可以仅报告失败并继续执行剩余步骤?目前,如果存在异常,则执行暂停。这就是我的测试案例 -
public class TC002_abc extends OpentapWrappers
{
@Test (description="Test")
public void main()
{
try
{
WebDriverWait wait=new WebDriverWait(driver, 60);
VerifyTitle(Constant.HomePage_Title);
Click(HomePage.link_Login(driver), "Login Link");
wait.until(ExpectedConditions.urlContains(Constant.LoginURL));
VerifyTextPopulated(CommunicationPref.lbl_EmailAddress_Input(driver), Constant.EmailAddress);
/* Validate Email Communications */
Click(CommunicationPref.link_EditEmailCommunications(driver),"Edit Email Communications");
VerifyText(CommunicationPref.lbl_UnCheckedEmailCommunications(driver), Constant.UnCheckedEmailCommunications_Text);
Click(CommunicationPref.btn_EmailCommunicationsSave(driver), "Save");
VerifyText(CommunicationPref.lbl_CheckedEmailCommunications(driver), Constant.CheckedEmailCommunications_Text);
}
catch (NoSuchElementException e)
{
e.printStackTrace();
Reporter.reportStep("NoSuchElementException" , "FAIL");
}
}
@BeforeClass
public void beforeClass()
{
browserName="firefox";
testCaseName = "TC002_abc";
testDescription = "Test";
}
}
样本方法 -
public static void VerifyTitle(String title){
try
{
if (driver.getTitle().equalsIgnoreCase(title))
{
Reporter.reportStep("Page is successfully loaded :"+title, "PASS");
}
else
Reporter.reportStep("Page Title :"+driver.getTitle()+" did not match with :"+title, "FAIL");
}
catch (Exception e)
{
e.printStackTrace();
Reporter.reportStep("The title did not match", "FAIL");
}
}
答案 0 :(得分:0)
由于您正在使用TestNG,请实施Soft Assertion
public void VerifyTitle(String title)
{
SoftAssert assertion = new SoftAssert();
String returnedTitle = driver.getTitle();
if (assertion.assertTrue(returnedTitle.contains(title)))
{
Reporter.reportStep("Page is successfully loaded :" + title, "PASS");
} else
{
Reporter.reportStep("Page Title :" + driver.getTitle() + " did not match with :" + title, "FAIL");
}
}
如果有帮助,请告诉我。
答案 1 :(得分:0)
如果测试用例的步骤失败,是否可以仅报告 失败并继续执行剩余步骤?
简短回答:是 长答案:是
Selenium是在诸如JUnit或TestNG之类的测试引擎之上构建的框架。在这些引擎上,如果您什么也不做,该工具将被解释为通过。换句话说,在没有断言的情况下,引擎将假定测试通过。由于Selenium是基于此构建的,因此Selenium也是如此。下面的代码段代表了黄瓜步骤的样子。
@When("my test step here")
public void myTestStep(...) {
boolean result = false;
try {
result = myTest(...);
}
} catch (Exception e) {
// log your exception (don't rethrow)
}
if (result) {
// log your passing test
} else {
// log your failing test
Assert.fail(); // This is what prevents subsequent steps to be executed. Remove it, and you should be able to continue to test.
}
对于JUnit或TestNG样式,方法基本上是相同的。您可能有一个@AfterClass
或@AfterTest
钩子,可以告诉测试框架通过失败测试。 通常,这意味着传递断言(不执行任何操作,即执行空方法)。但是,失败的断言是明确的,必须包含在某个地方。只需查找那些Assert.fail()
方法并将其删除。更好的选择是在测试套件中添加可配置的属性,以将其打开或关闭。
} else {
// log your failing test
if (skip_off) {
Assert.fail(); // This is what prevents subsequent steps to be executed. Remove it, and you should be able to continue to test.
}
}
在这种情况下,skip_off
是您可能存储在配置文件中的布尔属性值,当该属性设置为true时,它将跳过强制执行失败断言。