testsigma
left-mobile-bg

How To Automate Login Functionality Using Selenium WebDriver

October 30, 2024
Shreshtha Gokhru
right-mobile-bg
How To Automate Login Functionality Using Selenium WebDriver
image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free

Automating an application can be performed in various ways with various tools. Selenium WebDriver is most used among those because of being an open source and ease of use. Selenium WebDriver gives users a lot of flexibility to write their test in different programming language and allow the user to execute their test on different browser like Firefox, Chrome, Edge, Safari, etc. It can also be integrated with other 3rd party tools like TestNG, Junit, etc. 

The login feature is a critical component of many websites. The login page is the most crucial component when developing a website. The login page also plays a vital role from a security point of view. This blog will show how easy it is to automate Login to an application using Selenium WebDriver.

Prerequisites for Automated Login using Selenium WebDriver

To get started with Selenium automation login, the following components are necessary.

  1. Download and Install JDK
  2. Download and Install IDE (Integrated Development Environment) like Eclipse, and IntelliJ
  3. Selenium Client Configuration
  4. Integrate TestNG with IDE
  5. Browser drivers for Firefox, Chrome, IE, etc. 

Selenium and Selenium WebDriver

Selenium is a free, open-source automated testing framework that tests web applications across different browsers and platforms. Testing was done using Selenium, usually referred to as Selenium testing. Being an open source in nature, the Selenium framework can be integrated with other 3rd party tools. 

Since Selenium is an open-source tool, there is no licensing cost involved, which is one of the greatest advantages over any other testing tool.

Selenium WebDriver API provides a communication channel between languages and browsers. Selenium WebDriver is a framework that permits you to execute a cross-browser test. 

Selenium WebDriver is an upgrade of Selenium RC. Selenium WebDriver is a bridge between the Selenium test and your browser.

Steps for Automate Login functionality

  1. Instantiate the Selenium WebDriver
  2. Maximize the browser
  3. Navigate to URL
  4. Synchronization using implicit wait
  5. Identify the web element by using locating technique of Selenium
  6. Verification and Validation

Now let’s go through each step in detail:

1. Instantiate the Selenium WebDriver

Launching any web browser requires setting the driver’s path and instantiating the required browser. To instantiate the object of the browser driver, you can simply create the object with the help of the below command

For Chrome:

System.setProperty(“webdriver.chrome.driver”, “path of driver”);
WebDriver driver = new ChromeDriver(); 

For Firefox:

System.setProperty(“webdriver.gecko.driver”, “path of driver”);
WebDriver driver = new FirefoxDriver();

For Edge:

System.setProperty(“webdriver.edge.driver”, “path of driver”);
WebDriver driver = new EdgeDriver();

Read here – Setproperty in Selenium

Maximize the Browser

Selenium does not launch the browser in maximize mode, so we need to call maximize() Selenium method to maximize the browser so that test scripts can execute without any errors.

So below code snippet would help to open the browser in Maximize mode.

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

3. Navigate to URL

Once the browser opens in maximize mode, we must navigate to the required website. This can achieve by using the get() Selenium method. It accepts a string as a parameter, usually a URL of the application under test, and returns void. 

driver.get(“URL of the application/Website”);

4. Synchronization using Implicit wait

Synchronization plays a very vital role in test automation. We’ve seen instances where applications take longer to load, and our test script fails. To handle this dynamic, Selenium introduces implicit waiting.

With Implicit wait, we specify a maximum time we would want to wait for an element to be present. In the below snippets, it will wait a maximum of 30 seconds for each element.

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

5. Identify the web element by using locating technique of Selenium.

There are different ways by which users can identify the elements of an application.

  • ID
  • className
  • Name
  • XPath
  • tagName
  • CssSelector
  • linkText
  • partialLinkText

To learn more about Xpath in detail, please refer to the link: Xpath selector

6. Verification and Validation

Assertions are used to compare your actual and expected result. If your actual value and expected match, the test case passes. If not, then the test case fails. 

Assert.assertEquals(expectedValue, actualValue);

Code Implementation:

We will automate login functionality for the Testsigma application.

Code Implementation

Let’s see the code snippet for login automation.

Code:

import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Sample { @Test public void TestSigmaLogin() { System.setProperty(“webdriver.chrome.driver”, “path of driver”); WebDriver driver = new ChromeDriver(); // instantiate the browser driver.manage().window().maximize(); // maximize the browser driver.get(“https://app.testsigma.com/ui”); // opening the application driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.findElement(By.id(“loginUsername”)).sendKeys(“xxxxxx@abc.com”); driver.findElement(By.name(“password”)).sendKeys(“xxxxxxx”); driver.findElement(By.xpath(“//button[text()=’SIGN IN’]”)).click(); String actualURL = “https://app.testsigma.com/home”; String expectedURL = driver.getCurrentUrl(); // It will fetch the current URL Assert.assertEquals(expectedURL, actualURL); // It will verify both the URL are equal driver.close(); // It will close the current browser. } }

Automate login and other scenarios, 10x faster, with Testsigma

Try for free

Conclusion

The login feature has always been an essential feature of any website. Testing login functionality is simple and easy with Selenium WebDriver. You may test all boundary value conditions, negative situations, and optimistic scenarios with automation. The above articles demonstrate how you can automate the Testsigma login page with the help of Selenium. We have also given you a sample code to log in to the Testsigma application. 

The above articles help you to implement login functionality using Selenium WebDriver. Feel free to provide your feedback in the comment section. Happy testing 😊

FAQs

How do you automate login in Python?

  • Download and install python on your system
  • Install python related libraries into your project
  • Download and install PyCharm
  • Write your test script in python related syntax
  • Run and validate your test results. 

Note – Syntax in Selenium with Python is a little different from Selenium with Java.

How to automate login with OTP in Selenium

  • Download and install Appium
  • Integrate it with your Selenium framework
  • Connect your device with your system for OTP
  • Write a method to read OTP

Check here – OTP Test Cases

Testsigma Author - Shreshtha Gokhru

Shreshtha Gokhru

An astute professional with over 8+ years of experience in Software testing, Agile methodologies, and product sales. I am also keen to invest in the Stock market and Mutual funds. Apart from that, I like travelling and playing cricket.

image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free
imageimage
Subscribe to get all our latest blogs, updates delivered directly to your inbox.

By submitting the form, you would be accepting the Privacy Policy.

RELATED BLOGS


How to Write Test Cases for Trading Application Testing?
RITIKA KUMARI
TEST AUTOMATIONTESTING DISCUSSIONS
Scriptless Test Automation | What , Why it Matters & Examples
KIRUTHIKA DEVARAJ
TEST AUTOMATION
Top 6 Game Testing Tools You Need to Know
RAUNAK JAIN
TEST AUTOMATION