I am new in selenium, I try to login and check if dashboard URL is opened, but java.lang.AssertionError appears every-time. Please help me to understand what am I doing wrong. I try to compare if current URL that is supposed to be my dashboard URL equals to expURL. here is my code:
//test
@Test
public static void LoginPage(){
PageLogin loginPage = new PageLogin(driver);
driver.get("http://37.252.65.134:8885/#/core/login");
loginPage.enterCredentials("Apollo", "123");
loginPage.clickOnLogin();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
result = PageLogin.validateURL(driver,"http://37.252.65.134:8885/#/app/dashboard");
Assert.assertTrue(result);
}
//page object
package LoginPage;
public class PageLogin {
WebDriver driver;
//locators
@FindBy(how = How.XPATH, using = "//input[contains(@type,'text')]")
private WebElement username;
@FindBy(how = How.XPATH, using = "//input[contains(@type,'password')]")
private WebElement password;
@FindBy(how = How.XPATH, using = "//button[contains(.,'login')]")
private WebElement loginbutton;
//Constructor
public PageLogin(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void enterCredentials(String username, String password){
this.username.clear();
this.username.sendKeys(username);
this.password.clear();
this.password.sendKeys(password);
}
public void clickOnLogin(){
this.loginbutton.click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
public static boolean validateURL (WebDriver driver, String expURL) {
boolean result = false;
if (driver.getCurrentUrl().equalsIgnoreCase(expURL))
{
result = true;
System.out.println("Login Success");
}
return result;
}
}
The error you are seeing says it all :
java.lang.AssertionError
The error must have been diagnosed much before as your program is not printing Login Success as per the line :
System.out.println("Login Success");
The main reason is Synchronization Issue and as the condition of if()
doesn't satisfies and result = false
is returned.
The solution would be to wait for the getCurrentUrl()
to contain/tobe/match http://37.252.65.134:8885/#/app/dashboard
. To achieve that you can use either of the clauses from ExpectedConditions of WebDriverWait options as follows :
urlContains(java.lang.String fraction)
new WebDriverWait(driver, 20).until(ExpectedConditions.urlContains("37.252.65.134:8885"));
new WebDriverWait(driver, 20).until(ExpectedConditions.urlToBe("http://37.252.65.134:8885/#/app/dashboard"));
Once the new url
is established then you can validate the result in a for()
loop as :
new WebDriverWait(driver, 20).until(ExpectedConditions.urlContains("37.252.65.134:8885"));
if (driver.getCurrentUrl().equalsIgnoreCase(expURL))