I have created a negative test case for a webpage login URL.
In LoginPage(java) I have defined a Webelement(alertMessage), before hit login Firepath is :
<div id="submit-box">
<div id="loginAlert" class="loginAlert"/>
</form>
After Hit Firepath-ul is modifing because the user is wrong:
<div id="submit-box">
<div id="loginAlert" class="loginAlert">User doesn't exist.</div>
</form>
The URL remains the same after hit login.
Running the test in Maven, I have Assert error:
java.lang.AssertionError:
Expected: a string containing "Wrong password."
but: was ""
Expected :a string containing "Wrong password."
Actual :""
<Click to see difference>
My java package for LoginPage:
package CsphEbox.Pages;
import Resources.PageBase;
import com.gargoylesoftware.htmlunit.javascript.host.Console;
import com.sun.org.apache.xerces.internal.impl.xs.identity.Field;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import static javax.swing.text.html.CSS.getAttribute;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;
public class LoginPage extends PageBase
{
@FindBy (id = "UserName")
private WebElement loginUserNameInput;
@FindBy (id = "UserPassword")
private WebElement loginUserPasswordInput;
@FindBy (id = "Submit")
private WebElement loginButton;
@FindBy(xpath = ".//*[@id='loginAlert']")
private WebElement alertMessage;
// private WebElement alertMessage;
//String source = driver.findElement(By.xpath(".//form[@id='login']//div[@class='loginAlert' and @id='loginAlert']")).getAttribute("innerHTML");
public LoginPage(WebDriver driver)
{ super(driver);
waitForURLToContains("/login/login");
}
public String getAlertMessage()
{
waitForElementToBeVisible(alertMessage);
return alertMessage.getAttribute("innerHTML");
}
public LoginPage loginWrongAs(String username, String password)
{
waitForElementToBeVisible(loginUserNameInput);
loginUserNameInput.sendKeys(username);
loginUserPasswordInput.sendKeys(password);
loginButton.click();
return new LoginPage(driver);
}
My java package for LoginTest:
package CsphEbox.Test;
import CsphEbox.Pages.ActionEboxPage;
import CsphEbox.Pages.LoginPage;
import Resources.TestBase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static CsphEbox.Utils.LoginUtils.*;
import static java.lang.Thread.sleep;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class LoginTest extends TestBase
{ private LoginPage loginPage;
@Before
public void setUp()
{
driver.get(LOGIN_PAGE_URL);
loginPage = new LoginPage(driver);
}
@Test
public void loginWithWrongPassword() throws Exception
{
LoginPage eboxLog = loginPage.loginWrongAs(USERNAME,WRONGPASSWORD);
assertThat(eboxLog.getAlertMessage(),containsString("Wrong password."));
sleep(2000);
}
I am using Java with Selenium Webdriver.
Does anyone knows with the xpath for the WebElement alertMessage is not updating with the LoginAlert message: "Wrong password." ??
Thank you!