0
votes

i am trying to make selenium test for spring security, and i followed many examples with no luck, i always end up with login fail:

public class LoginTest {

    private WebDriver driver;
    private String baseUrl;

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

    @Test
    public void testLoginClass() throws Exception {
        driver.get(baseUrl + "/MyAPP/login");

        final WebElement usernameField = driver.findElement(By
                .id("j_username"));
        usernameField.sendKeys("1");

        final WebElement passwordField = driver.findElement(By
                .id("j_password"));
        passwordField.sendKeys("123456");

        passwordField.submit();

        System.out.println("##################### URL: "
                + driver.getCurrentUrl());
        Assert.assertNotNull(driver.findElement(By.className("welcomeHome")));
    }

    @After
    public void tearDown() throws Exception {
        driver.close();
    }

}

ISSUE: i am sure that entered username and password matches, and i can print them in loginFailureHandler and i see that they are correct (before encoding, should they be encoded or not in loginFailure ? )

anyway, i am using SHA encoding:

<authentication-manager alias="authenticationManager">          
      <authentication-provider user-service-ref="userDetailsService">
         <password-encoder hash="sha"/>             
      </authentication-provider>   
    </authentication-manager> 

can anyone tell me please why i alwyas get login fail ?

this is my login form:

<form action="#{request.contextPath}/j_spring_security_check" method="post">

                        <h:inputText  id="j_username"  />
                        <h:inputSecret  id="j_password" />
                        <h:commandButton   id="loginBtn" />

 </form>

UPDATE: using button click, make login success but doesn't forward the user to home page, and he still in login page, driver.getCurrentUrl() will print login

WebElement loginBtn = driver.findElement(By
            .id("loginBtn"));
loginBtn.click();

UPDATE2: i tried to forward the user to home page after login with no luck too, user still in login page.

WebElement loginBtn = driver.findElement(By.id("loginBtn"));
        loginBtn.click();

        driver.get(baseUrl + "/MyAPP/home");

UPDATE3: when i replaced the button click with enter click, it works fine i don't know why

OLD:

WebElement loginBtn = driver.findElement(By.id("loginBtn"));
loginBtn.click();

NEW:

passwordField.sendKeys(Keys.ENTER);
2
The .sendKeys(Keys.ENTER) solved a problem I was having with running WebDriver on Android. For some reason .click() wasn't working for buttons. - Merkidemis

2 Answers

3
votes

Have you tried clicking on the button instead of passwordfield.submit()?

WebElement loginBtn = driver.findElement(By
            .id("loginBtn"));
loginBtn.click();
1
votes

Replace name by id

use the following snip:

final WebElement usernameField = driver.findElement(By.id("j_username"));         usernameField.sendKeys("1");
final WebElement passwordField = driver.findElement(By.id("j_password"));         passwordField.sendKeys("123456");