gpt4 book ai didi

java - 我的 Selenium 测试应该在哪里检查预期/实际结果?

转载 作者:行者123 更新时间:2023-11-30 10:55:26 26 4
gpt4 key购买 nike

我目前正在为网络应用程序设计测试框架。该框架是一个混合(数据和关键字驱动)框架。我已将其设置好,因此我拥有充满 UI 功能的程序包,我可以将这些方法调用到测试类中以导航和单击应用程序。

我想知道最佳实践。

我应该检查每一步的结果吗?还是应该在测试结束时检查结果?

有关更多详细信息,这是我使用最小化代码进行测试的当前结构:

// This test contains one step. The following method navigates to the page and inputs invalid credentials.
// We than check if we're still on the login page. If we are, the test passes. If we can't find the
// "login text", than the test has failed.
LoginPage.invalidCredentialsOne.run(driver);
Thread.sleep(2000);

WebElement logintext = driver.findElement(By.xpath(prop.getProperty("logintext")));
if (logintext.getText().equals("LOG IN")) {
//add pass entry to the excel sheet
testresultdata.put("3", new Object[] {
2d, "User should not be able to login with an invalid password", "Login failed", "Pass"
});
driver.close();
Alert alert = driver.switchTo().alert();
alert.accept();
} else {
//add fail entry to the excel sheet
testresultdata.put("3", new Object[] {
2d, "User should not be able to login with an invalid password", "Login failed", "Fail"
});
fail();
driver.close();
Alert alert = driver.switchTo().alert();
alert.accept();
}

在上面的代码中,我检查了预期/实际结果 AFTER 我已经运行了该方法。检查方法本身内部的预期/实际结果是否可取?这样做的优点/缺点是什么?

我觉得好像在该方法中运行预期/实际结果会提高可维护性和测试准确性(能够准确地看到测试失败的地方)。

或者为什么不同时测试两者呢?

方法 - 检查每个导航。如果导航失败,在控制台输出一行告诉你在哪里。这会检查简单的 UI 错误。

测试类 - 检查更具体的用户场景。

对执行此操作的正确最佳实践非常感兴趣。提前致谢。

最佳答案

我会将测试方法(尤其是用 selenium 编写的系统测试)分为操作和验证。这意味着您必须划分导致某些系统更改的方法(例如您的 invalidCredentialsOne)和应在更改发生后验证/测试系统状态的方法。

为什么这样好?通过这种方式,您可以借助一些操作集来构建您的用户场景,并在需要的地方验证这组操作。这种方法为构建各种场景和 verios 测试提供了灵 active 。

如果我们回到你的例子,让我们假设你不仅要测试你回到登录页面以防错误的凭据,而且,例如,在 5 次尝试后系统(只是假设!)将阻止用户。在这种情况下,您可以使用您的第一个操作 5 次,然后验证系统是否阻止了该用户。

附言请尽量避免在您的 selenium 代码中使用 Thread.sleep。等待对象的外观好多了。

关于java - 我的 Selenium 测试应该在哪里检查预期/实际结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33349343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com