-1

I want to verify a log in action by using assert command in Selenium Webdriver (java):

username: admin
password :admin

When I entered an invalid password, it displays a message like wrong username or password. I tried with the code below, but the assert command is failing.

enter image description here

Code:

package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;


public class LoginPage 
{
    private WebDriver driver;

       @FindBy(id="userName")
       WebElement username;
       @FindBy(id="password")
       WebElement password;
       @FindBy(className="btn-info")
       WebElement button;
       public LoginPage(WebDriver driver)
       {
        //initialize elements
          PageFactory.initElements(driver, this);

       }
       public void set_username(String usern)
       {
        username.clear();
        username.sendKeys(usern);
       }
       public void set_password(String userp)
       {
        password.clear();
        password.sendKeys(userp);
       }
       public void click_button()
       {
        button.submit();
       }
}





package Test;
import org.testng.annotations.Test;


import Pages.LoginPage;
import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;



public class LoginTest
{
WebDriver driver;
@BeforeTest
public void setup()
{
    System.setProperty("webdriver.firefox.marionette","pathToGeckodriver");
    driver=new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("https://twhyderabad.github.io");
}
@Test(priority=5)
public void verify1()
{
    LoginPage login=new LoginPage(driver);
    login.set_username("admin");
    login.set_password("admin");
    login.click_button();
    Assert.assertTrue(driver.getPageSource().contains("Blog"));
}
@Test(priority=1)
public void verify2()
{
    LoginPage login=new LoginPage(driver);
    login.set_username("adm");
    login.set_password("admin");
    login.click_button();
    Assert.assertEquals(driver.findElement(By.xpath("//*[@id='errorMessage']")),"Wrong username or password!");
}
@Test(priority=2)
public void verify3()
{
    LoginPage login=new LoginPage(driver);
    login.set_username("admin");
    login.set_password("adm");
    login.click_button();
    Assert.assertEquals(driver.findElement(By.xpath("//*[@id='errorMessage']")),"Wrong username or password!");
}
0

Locate the element of the "Wrong username or password" label and get the text of it. then use assert the text , So if the assert get pass you can say entered username or password is wrong.

2

Assert is failing because assertEquals() is trying to compare an WebElement with a string.

So, Store the respective element in a variable and then use getText() method on the element, which would return a string and then call assertEquals() which compares both strings.

WebElement errorMessage= driver.findElement(By.xpath("//*[@id='errorMessage']");
Assert.assertEquals(errorMessage.getText(),"Wrong username or password!");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy