如何断言在selenium测试用例的登录成功? [英] How to assert in selenium test case for login success?

查看:5025
本文介绍了如何断言在selenium测试用例的登录成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为登录页制作了成功的硒测试用例,用户输入正确的用户名和密码,然后点击登录以转发到主页问题是,当我更改测试类中的密码,测试总是成功我不知道为什么。

i made a success selenium test case for login page, which a user enters a correct username and password and then hit login to forwarded to home page,issue is that when i change the password in the test class, the test always success i don't know why.

这里是测试类: p>

here's the test Class:

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;
        }
    }

}

如何处理测试用例的失败,让我们假设有一段时间后数据库被改变,没有这样的用户1现在是成功的。

please advise how to handle the fail of test case, let's suppose that after sometime the database is changed and there's no such user 1 which is success right now.

推荐答案

您不是检查错误的密码是否让用户登录。

Your not checking to see if the wrong password got the user logged in.

您需要在登录后放入一个测试用例,

You need to put a test case to check after logging in, if some particular text appears.

例如,如果用户成功登录,欢迎屏幕显示Hi UserName。因此,如果在您的selenium脚本命中 loginBtn

Like for example, if the user successfully logs in, the welcome screen shows "Hi UserName". So you need to assert if the text is present after your selenium script hits on the loginBtn

目前你正在做的是发送一些文本作为密码,selenium显然不知道密码是否不正确,除非你验证输入结果不正确的密码。

Currently all you are doing is sending "some" text as the password, selenium will obviously not know if the password is incorrect, unless you validate the outcome of entering an incorrect password.

编辑

RUBY的程式码片段 -

A code snippet in RUBY-

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

这篇关于如何断言在selenium测试用例的登录成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆