LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I test a login process using Selenium and Java?

Can anyone please give me a tutorial on how can I test a login process using Selenium and Java.

Up Vote Down Vote 0 Votes
Posted 4 months ago

Answers


Below are the few steps that would help you testing a login web page using Selenium and Java:

1. Create A Selenium WebDriver Instance

Webdriver driver=new ChromeDriver();

In order to launch the website in the desired browser, you need to set the system properties to the path of the driver for the required browser. In this Selenium Java tutorial, we will use chromedriver for demonstrating Selenium login example with Java. The syntax for the same will be:

System.setProperty(“webdriver.chrome.driver”, “File path for the Exe”);

2. Configure Your Browser If Required

Based on the needs, we can configure the browser. For example, in this Selenium Java tutorial regarding Selenium login with Java, a browser by default, will be minimized mode, we can setup the browser in the maximize mode. Below is the syntax used for the same.

driver.manage().window().maximize();

Other things that you can do for configuring your browser is set up different options like disabling info bars, browser notifications, adding extensions, etc. You can also use the capabilities class to run your script on various browsers thereby helping in cross browser testing.

3. Navigate To The Required URL

Pretty simple, open the browser with the desired URL. All you have to do is write the below syntax and you have your URL open in the desired instantiated browser.

driver.get(“[https://www.linkedin.com/login](https://www.linkedin.com/login%E2%80%9D)”);

4. Locate The HTML Element

This is the heart of writing a Selenium script. For this to function, you need to have a clear understanding of the different locators used to find the HTML element. You can refer my below articles that talks about the different locators available in selenium and how to locate the element with different examples:

ID locator in Selenium WebDriver

Name Locator in Selenium WebDriver

TagName Locator in Selenium WebDriver

CSS Selector in Selenium WebDriver

XPath in Selenium WebDriver

For example, lets try to locate the email and password field of the login form of LinkedIn

Below is the DOM structure for the email input box:

email input box

You can locate it via ID locator in Selenium WebDriver as below:

driver.findElement([Domain Premium: By.id](http://by.id/)(“username”));

Since this returns a webelement, you can store it in webelement variable as below

WebElement username=driver.findElement([Domain Premium: By.id](http://by.id/)(“username”));

The same can be achieved for password and login button field which is

driver.findElement([Domain Premium: By.id](http://by.id/)(“password”));

WebElement password=driver.findElement([Domain Premium: By.id](http://by.id/)(“password”));

driver.findElement(By.xpath(“//button[text()=’Sign in’]”));

WebElement login= driver.findElement(By.xpath(“//button[text()=’Sign in’]”));

driver.findElement([Domain Premium: By.id](http://by.id/)(“password”));

WebElement password=driver.findElement([Domain Premium: By.id](http://by.id/)(“password”));

driver.findElement(By.xpath(“//button[text()=’Sign in’]”));

WebElement login= driver.findElement(By.xpath(“//button[text()=’Sign in’]”));

5. Perform Action On The Located HTML Element

Once located, you need to perform the desired action which in our case is sending text to email and password field and clicking on the login button. To execute this action in Selenium login example with Java, we make use of methods as sendKeys and click provided by Selenium as below:

username.sendKeys(“xyz@gmail.com”);

password.sendKeys(“exampleAboutSelenium123”);

login.click();

username.sendKeys(“xyz@gmail.com”);

password.sendKeys(“exampleAboutSelenium123”);

login.click();

And guess what, you just finished writing the most important parts of the script. Now, in this Selenium Java Tutorial, you only need to ensure these actions have successfully logged in the user, which comes to our final step of script creation for using Selenium to login with Java.

6. Verify & Validate The Action

In order to validate the results, all you need to do is use assertion. Assertions are vital for comparing the expected results vs the actual results. Almost similar to your test cases, wherein each test case has an actual and expected behavior to it. If it matches, the test case pass, if not, then the test case fails. Assertions does exactly the same. Assertion class are provided by both JUnit and TestNG framework, you can opt to choose any. The below syntax will help to assert (validate) the outcome from actions by performing Selenium login with Java.

Assert.assertEquals(String actual, String expected);

So, in this case, we will save our actual url post login into a string value which is:

String actualUrl=” [LinkedIn](https://www.linkedin.com/feed/%E2%80%9D);

And expected URL can be found from the below method:

String expectedUrl= driver.getCurrentUrl();

So your final assertion would become as:

Assert.assertEquals(actualUrl, expectedUrl);

Note: In order to use assertion, you need to use the annotations of TestNG or JUnit ‘@Test’ for assertions to function. In case, right now you don’t want to get into the hassle of going into the framework keywords, you can simply match the string using an ‘if’ statement and print the results in console accordingly, something like below:

if(actualUrl.equalsIgnoreCase(expectedUrl))

{

System.out.println(“Test passed”)

}

else

{

System.out.println(“Test failed”)

}

if(actualUrl.equalsIgnoreCase(expectedUrl))

{

System.out.println(“Test passed”)

}

else

{

System.out.println(“Test failed”)

}

Bingo!! You have executed automation testing using Selenium login example with Java.

If you are curious about using annotations then follow our blog on Selenium Java Tutorial On JUnit Annotations In Selenium With Examples.

Below is the collective code of all the statements explained above using assertions.

import java.util.concurrent.TimeUnit;

import [http://org.openqa.selenium.By](http://org.openqa.selenium.by/);

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.Test;

public class LoginUsingSelenium {

@Test

public void login() {

// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", "path of driver");

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get("[LinkedIn Login, LinkedIn Sign in](https://www.linkedin.com/login)");

WebElement username=driver.findElement(Domain Premium: By.id("username"));

WebElement password=driver.findElement(Domain Premium: By.id("password"));

WebElement login=driver.findElement(By.xpath("//button[text()='Sign in']"));

username.sendKeys("example@gmail.com");

password.sendKeys("password");

login.click();

String actualUrl="[LinkedIn](https://www.linkedin.com/feed/)";

String expectedUrl= driver.getCurrentUrl();

Assert.assertEquals(expectedUrl,actualUrl);

}

}

import java.util.concurrent.TimeUnit;

import http://org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.Test;

public class LoginUsingSelenium {

@Test

public void login() {

// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", "path of driver");

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get("LinkedIn Login, LinkedIn Sign in");

WebElement username=driver.findElement(Domain Premium: By.id("username"));

WebElement password=driver.findElement(Domain Premium: By.id("password"));

WebElement login=driver.findElement(By.xpath("//button[text()='Sign in']"));

username.sendKeys("example@gmail.com");

password.sendKeys("password");

login.click();

String actualUrl="LinkedIn";

String expectedUrl= driver.getCurrentUrl();

Assert.assertEquals(expectedUrl,actualUrl);

}

}

Console Output:

Up Vote Down Vote 0 Votes
Posted 3 months ago
212 Views
1 Answer
4 months ago