[FIXED] Automating login page test cases using Selenium

Issue

As I am new to Selenium and Java I got stuck while testing a login page. Below is my code which I am trying to test to always it is returning Test case failed even if I am giving the correct username and password. I wondering where it is going wrong. URL is also correct while checking the equal condition. The URL I have taken post login into the site.

package Seleniumtesting;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Selenium {

    public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe"); 
    WebDriver d = new ChromeDriver();
    d.get("https://onelogin.adityabirlacapital.com/login");
    d.findElement(By.id("login-id")).sendKeys("my_username");
    d.findElement(By.id("password")).sendKeys("my_password");
    //d.findElement(By.xpath("//*[@id=\"password-login\"]")).click();
    d.findElement(By.id("password-login")).click();
    String u = d.getCurrentUrl();
    if(u.equalsIgnoreCase("https://onelogin.adityabirlacapital.com/my-dashboard"))
    {
        System.out.println("Test case passed");
    }
    else
    {
        System.out.println("Test case failed");
    }
        d.close();
    }

}

Solution

You can try adding delay before executing d.getCurrentUrl(). Something like TimeUnit.SECONDS.sleep(2); into your code to make sure that everything is loaded retrieving the value of the current URL after clicking login.

Other solution might be, after clicking login button via Selenium, find any element in the homepage that possibly indicates a user is successfully logged in, such as welcome messages or if there’s already a logout button.

Answered By – Rob

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published