How to assert in selenium test case for login succ

2019-07-29 04:49发布

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.

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

}

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.

2条回答
混吃等死
2楼-- · 2019-07-29 05:29

In order to verify your scenario, your application should display a message like, "Invalid Password Entered."

Then try this after login button click.

try {
    assertEquals(driver.findElement(By.id("Your Id for the message")).getText(), "Invalid UserID or Password Entered");

    //If the message is displayed

    System.out.println("PASS");

} catch (Exception e) {

    //If the message is not displayed

    System.out.println("FAIL");

    verificationErrors.append(e.toString());

}
查看更多
对你真心纯属浪费
3楼-- · 2019-07-29 05:38

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.

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

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.

Edit


A code snippet in RUBY-

assert(@driver.find_element(:tag_name => "body").text.include?("Welcome UserName"),"The User successfully logs in")
查看更多
登录 后发表回答