Automating an application can be done in various ways with various tools. Selenium WebDriver
is mostly used because of its open source and ease of use. Selenium WebDriver gives users a
lot of flexibility to write their test in different programming languages and also allows the user to
execute their test in different browsers like Firefox, Chrome, Edge, Safari, etc. This is integrated
with other 3rd party tools like Testing, Junit, etc. And
The login feature is a critical component of many websites. The login page is the most crucial
component when developing a website.And the login page also plays a vital role from a security
point of view. Here, the blog will show how easy it is to automate Login to an application using
Selenium WebDriver.
Requirements for Automated Login using Selenium
WebDriver
To get started with Selenium automations login, the following things are necessary.
- Download and Install JDK.
- Download and Install IDE (Integrated Development Environment) like Eclipse, and IntelliJ.
- Selenium Client Configuration.
- Integrate TestNG with IDE.
- Browser drivers for Firefox, Chrome, IE, etc
Selenium and Selenium WebDriver
Selenium is a zero cost, open-source automated testing framework that tests web applications
on various browsers or platforms. Testing can be implemented using Selenium, mainly referred
to as Selenium testing. As, It is open source in various ways, the Selenium framework can also
be implemented with other 3rd party tools.
Since Selenium is an open-source tool, there is no licensing cost involved. This is one of the
greatest advantages in testing tools.Selenium WebDriver API provides a communication course
between languages and browsers. Selenium WebDriver is a framework that permits you to
execute in different browser tests.
Selenium WebDriver is an upgrade of Selenium RC. Selenium WebDriver is a crossover
between the Selenium test and your browser.
The Steps for Automate Login functionality
- Instantiate the Selenium WebDriver.
- Maximize the browser.
- Navigate to the URL.
- Synchronization using implicit wait.
- Identify the web element by using the locating technique of Selenium.
- Verification and Validation.
Now let’s go through each step in details:- - Instantiate the Selenium WebDriver.
Launching any web browser requires setting the driver’s path and implementing the required
browsers. And with the commencement of the object of the browser driver, you can simply
create the objects with the help of the below command.
For Chrome:-System.setProperty(“webdriver.chrome.driver”, “path of driver”);
For Firefox:
WebDriver driver = new ChromeDriver();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();
Maximize the Browsers
Selenium has not launched any browser in maximize mode, And we need to call maximize()
Selenium method to maximize in the browser so that test scripts can execute without any errors.
So below code snippet would help to open the browsers in Maximize mode.
driver.manage().window().maximize();
- Navigate to URL
If the browser opens in maximize mode, we must negotiate to the required website. It can be
achieved by using the get() Selenium method. It accepts a string as a parameter, usually a URL
of the applications under test, and returns Void.
driver.get(“URL of the application/Website”); - Synchronism using Implicit wait.
Synchronism plays a very vital role in test automation. It has seen instances where applications
take longer to load, As well as our test script failing. To handle this dynamic, Selenium
introduces implicit waiting.
With the Implied wait, we can 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.
- Associate the web element by using the locating technique of Selenium.
These are an important 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
- Verification and Validation.
Assertions are used to compare your actual and expected result. If your actual values 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 Test Sigma application.
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.
}
}
Conclusion
The login features have always been an essential feature of any website.And the Testing login
functionality is simple and easy in Selenium WebDriver. You may test all the boundaries, value
conditions, negative situations, And the optimistic framework, with automation. And these are the
above articles demonstrating how we can automate the Test Sigma login page with the help of
Selenium. We can also give you a sample code to log in to the Test Sigma application.
These above articles help you to implement login functionality using Selenium WebDriver.