如何在selenium测试用例中声明登录成功?

fre*_*dev 1 continuous-integration selenium integration-testing java-ee

我为登录页面做了一个成功的selenium测试用例,用户输入正确的用户名和密码然后点击登录转发到主页,问题是当我在测试类中更改密码时,测试总是成功我不要我知道为什么.

这是测试类:

public class LoginTest extends TestCase {

    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://localhost:8080";
    }

    @Test
    public void testLoginClass() throws Exception {
        driver.get(baseUrl + "/MyAPP/login");
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        driver.findElement(By.id("j_username")).clear();
        driver.findElement(By.id("j_username")).sendKeys("1");
        driver.findElement(By.id("j_password")).clear();
        driver.findElement(By.id("j_password")).sendKeys("wrong password");
        driver.findElement(By.id("loginBtn")).click();

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    @SuppressWarnings("unused")
    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

请告知如何处理测试用例的失败,让我们假设在一段时间后数据库被更改并且没有这样的用户1现在成功.

Ame*_*mey 8

您没有检查是否有错误的密码让用户登录.

如果出现某些特定文本,您需要在登录后设置测试用例进行检查.

例如,如果用户成功登录,欢迎屏幕将显示"Hi UserName".所以你需要assert在你的selenium脚本点击之后文本存在loginBtn

目前您所做的只是发送" 一些 "文本作为密码,selenium显然不知道密码是否不正确,除非您验证输入错误密码的结果.

编辑


RUBY中的代码片段 -

assert(@driver.find_element(:tag_name => "body").text.include?("Welcome UserName"),"The User successfully logs in")
Run Code Online (Sandbox Code Playgroud)