[FIXED] Automating login page test cases using Selenium


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) {
    WebDriver d = new ChromeDriver();
    String u = d.getCurrentUrl();
        System.out.println("Test case passed");
        System.out.println("Test case failed");



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