How to automate Login page using Selenium WebDriver with Java? | Selenium WebDriver Tutorial

Faisal Khatri
31 May 202413:01

Summary

TLDRThis tutorial video guides viewers on automating multiple login page test cases using Selenium WebDriver with Java and TestNG. It covers three scenarios: entering a valid email with an invalid password, leaving the password field blank, and a successful login with valid credentials. The video demonstrates navigating to the login page, locating elements, and using data providers to pass credentials. It also shows how to verify warning messages and successful login redirection to the 'My Account' page, emphasizing the importance of testing for both security and functionality.

Takeaways

  • πŸ” The video focuses on automating test cases for a login page, which is crucial for user security and website performance.
  • πŸ› οΈ The demonstration uses Selenium WebDriver with Java, running tests on the Chrome browser, and TestNG for the test framework.
  • πŸ“ The video covers three distinct test scenarios: entering a valid email with an invalid password, leaving the password field blank, and a positive login scenario with valid credentials.
  • πŸ“ The video explains how to locate web elements such as the email address field, password field, and login button using Selenium.
  • πŸ”‘ The 'perform login' method is detailed, which includes clearing fields, entering values, and clicking the login button.
  • ⚠️ The video demonstrates how to handle and verify warning messages that appear when login attempts fail due to invalid credentials.
  • 🎯 The positive test scenario involves successfully logging in and verifying that the 'my account' page is displayed, confirming a successful login.
  • πŸ“š The project structure is outlined, including the use of a Page Object Model (POM) for organizing test code and separating page-specific logic.
  • πŸ“ˆ The video uses TestNG's data provider to supply different sets of login credentials for testing various scenarios efficiently.
  • πŸ”„ The video concludes with a live demonstration of the test execution, showcasing the automation of all scenarios in a sequential manner.

Q & A

  • What is the main focus of the video?

    -The main focus of the video is to demonstrate how to automate multiple test cases for a login page using Selenium WebDriver with Java and TestNG.

  • Why is the login page considered an important component of a website?

    -The login page is considered an important component of a website because it is pivotal in ensuring user security and overall website performance.

  • Which website is used for demonstrating the automation of login test scenarios?

    -The LRA Testmerce website is used for demonstrating the automation of multiple login test scenarios.

  • What tools and technologies are used in the video to automate the login page?

    -The tools and technologies used include Selenium WebDriver with Java, TestNG, Chrome browser, and data provider and rotation features of TestNG.

  • How many different test scenarios are automated in the video?

    -Three different test scenarios of the login page are automated in the video.

  • What are the three test scenarios automated in the video?

    -The three test scenarios are: 1) Entering a valid email address and an invalid password, 2) Entering a valid email address and leaving the password field blank, and 3) Entering a valid email address and a valid password.

  • What is the purpose of the warning message 'No match for email address and or password' in the test scenarios?

    -The purpose of the warning message 'No match for email address and or password' is to verify that the login feature is working correctly and that the appropriate message is displayed when the login fails.

  • How is the success of the login operation verified in the positive test scenario?

    -The success of the login operation in the positive test scenario is verified by checking if the user is taken to the 'my account' page and if the 'my account' text is displayed after a successful login.

  • What is the role of the page object model in the automation process described in the video?

    -The page object model serves as a design pattern that is used to create an object repository for all the web elements on the login page, encapsulating the logic for interacting with the page.

  • What is the significance of the 'dataProvider' annotation used in the test?

    -The 'dataProvider' annotation is used to supply data for the test methods, allowing for the automation of different test scenarios by passing various sets of login credentials.

  • How is the test execution time reported in the video?

    -The test execution time is reported as 26 seconds and 459 milliseconds, indicating the duration it took for all three test scenarios to execute successfully.

Outlines

00:00

πŸ” Automating Login Page Test Scenarios

This paragraph introduces a tutorial video on automating multiple test cases for a login page using Selenium WebDriver with Java. The video emphasizes the importance of the login page for user security and website performance. The demonstration uses the 'lra test e-Merce website' and Selenium WebDriver with Java, running tests on Chrome browser. The tutorial will cover three different scenarios: entering a valid email with an invalid password, leaving the password field blank, and a successful login with valid credentials. The video also mentions the use of TestNG with data providers for managing test data.

05:01

πŸ›  Setting Up the Test Environment and Project Structure

The speaker provides a walkthrough of the project structure and libraries used for the test automation. The project utilizes Selenium WebDriver with Java and TestNG, with dependencies listed as Selenium Java and TestNG with specific version numbers. A new package 'login test demo' is created, containing a 'pages' package and a 'Login Page' class that follows the Page Object Model (POM). The class includes methods to locate web elements like the email and password fields and the login button. The 'perform login' method is explained, which takes email and password as parameters, clears fields, enters values, and clicks the login button. The method checks for warning messages in case of invalid login attempts and verifies successful login by checking for the 'my account' page title.

10:03

πŸ“ Implementing Test Cases and Data Providers

The paragraph details the implementation of test cases using TestNG and data providers. A data provider method 'get login data' is created to supply test data. The data includes scenarios with invalid password, blank password, and valid credentials. The 'LoginPageTest' class extends 'BaseTest' and uses the data provider to execute tests. The test method 'testLoginFeature' navigates to the login page, performs login with given credentials, and checks for warning messages or verifies successful login by checking the 'my account' page title. The video concludes with a demonstration of running the tests, which successfully execute all scenarios within a short timeframe. The speaker encourages viewers to expand the test scenarios and use the demonstrated methods for their own test automation needs.

Mindmap

Keywords

πŸ’‘Selenium WebDriver

Selenium WebDriver is an open-source tool used for automating web browsers. It is a key component in the video's theme of automating test cases for a login page. The script mentions using Selenium WebDriver with Java to run tests on the Chrome browser, highlighting its role in automating interactions with web elements such as email and password fields, and the login button.

πŸ’‘TestNG

TestNG is a testing framework that allows for the execution of test cases in parallel, making it an efficient tool for software testing. In the video, TestNG is used in conjunction with Selenium WebDriver to handle multiple test scenarios for the login page, demonstrating its utility in managing and running automated tests.

πŸ’‘LoginPage

The login page is a crucial component of any website, as it serves as the entry point for users and ensures security. The video focuses on automating test cases for this page, emphasizing its importance in website performance and user authentication. The script describes various scenarios to test the login page, such as entering valid and invalid credentials.

πŸ’‘Page Object Model (POM)

Page Object Model is a design pattern used in Selenium testing to represent the structure of web pages. It is central to the video's narrative, as the script mentions creating a separate class for the login page following the POM. This approach helps in organizing the test code and makes it more maintainable and scalable.

πŸ’‘Data Provider

A Data Provider in TestNG is a mechanism to supply data to test methods. The video script discusses using a Data Provider to pass login credentials for different test scenarios. This feature allows for the automation of various test cases with different sets of data, enhancing the robustness of the testing process.

πŸ’‘Test Scenarios

Test scenarios are specific conditions or situations that are tested to ensure the functionality of a system. The video script outlines three different test scenarios for the login page, including entering a valid email with an invalid password, leaving the password field blank, and a positive scenario with valid credentials. These scenarios are essential for verifying the login feature's correctness.

πŸ’‘Web Elements

Web elements refer to the individual components of a web page, such as input fields, buttons, and links. The video script describes locating web elements like the email address field, password field, and login button using Selenium WebDriver. These elements are crucial for interacting with the login page during the automation of test scenarios.

πŸ’‘Assertions

Assertions are used in testing to verify that a condition or expected outcome is met. In the context of the video, assertions are performed to check if the warning message is displayed correctly when invalid credentials are entered. This confirms that the login page is functioning as expected and can handle incorrect inputs.

πŸ’‘My Account Page

The 'My Account' page is typically accessed after a successful login and is used to verify that the login process works correctly. The video script mentions verifying the presence of the 'My Account' text after a successful login, indicating that the user has been authenticated and redirected to the correct page.

πŸ’‘Implicit Wait

Implicit Wait is a Selenium WebDriver setting that tells the driver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The video script refers to setting an implicit wait of 30 seconds to ensure that the web page is loaded correctly before performing the login test scenarios.

Highlights

Introduction to automating multiple test cases for a login page, a critical component for user security and website performance.

Demonstration of using the LRA Testmerce website for automating login test scenarios.

Utilization of Selenium WebDriver with Java for test automation on the Chrome browser.

Employment of TestNG framework with data provider and rotation for handling login credentials.

Navigation to the login page and identification of email, password fields, and login button.

Automation of three distinct login scenarios: valid email with invalid password, valid email with blank password, and valid email and password.

Verification of warning messages for invalid login attempts.

Positive scenario verification by successful login and redirection to the 'My Account' page.

Explanation of the project structure, including dependencies for Selenium WebDriver and TestNG.

Introduction to the LoginPage class following the Page Object Model (POM).

Identification of web elements using IDs and CSS selectors for email, password fields, and the login button.

Description of the performLogin method, which takes email and password as parameters and simulates login actions.

Handling of different login outcomes with conditional checks for warning messages or successful login.

Setup of the BaseTest class for initializing the WebDriver, setting up the browser, and defining test conditions.

Creation of a data provider method to supply test data for different login scenarios.

Execution of the test cases using the data provider and TestNG annotations.

Demonstration of the test execution and the successful automation of all three login scenarios.

Conclusion and a call to action for viewers to apply the demonstrated techniques in their test automation practices.

Transcripts

play00:03

[Music]

play00:08

hello folks welcome back to my channel

play00:11

in this video we will learn how to

play00:12

automate multiple test cases of the

play00:14

login page as you all know login page is

play00:16

one of the important component of the

play00:18

website it is pivotal in ensuring user

play00:21

security and overall website performance

play00:23

let's now learn about the website and

play00:24

the tools used in this video for

play00:27

demo we will be using the lra test e

play00:30

Merce website for automating the

play00:32

multiple login test

play00:34

scenarios and uh the tools used will be

play00:37

selum web driver with Java we will be

play00:39

running the test on the Chrome browser

play00:41

and using test NG explicitly we will be

play00:44

using data provider and rotation of test

play00:45

NG to pass on the login credentials in

play00:47

the test now let's talk about the test

play00:50

scenarios that we will be automating so

play00:52

first of all we will be navigating to

play00:53

this login page and then allocating the

play00:56

email address password field and the

play00:59

login button we will be automating three

play01:01

different scenarios of this login page

play01:03

first scenario will be to enter a valid

play01:06

email let me show that to you manually

play01:09

david. Thomson

play01:12

gmail.com and we will enter an invalid

play01:14

password let's let enter an ABCD kind of

play01:17

a value in the password field and click

play01:19

on login so the first case will be to

play01:22

enter a valid email address and an

play01:24

invalid password and then verify this

play01:26

message warning no match for email

play01:29

address and or

play01:30

password the next case would be to enter

play01:34

a valid email address NN and then leave

play01:37

the password field blank and click on

play01:39

login so again it will show the same

play01:41

message we will again verify the message

play01:43

is been displayed uh this is to ensure

play01:45

this negative scenarios actually ensures

play01:47

that the login feature is actually

play01:49

working fine and we have a respective

play01:51

message displayed in case if the login

play01:54

does not happen in the website finally

play01:56

on the third scenario it will be a

play01:57

positive scenario and we'll verify by

play01:59

entering a username that is uh by

play02:02

entering an email address that is valid

play02:04

and entering a valid password as well

play02:06

and then click on login button so once

play02:08

the login is successful it should be

play02:10

taken to the my account page and we will

play02:13

verify that this my account text is

play02:16

displayed after the login is successful

play02:18

so let's now move on to the

play02:20

code and uh before I just move on to the

play02:25

uh code walk through let me first give

play02:27

you a small demo around the project

play02:31

structure as well as the libraries used

play02:33

so like I said we will be using selenium

play02:35

web driver with Java and test NG so you

play02:38

see you can see the dependencies

play02:40

selenium Java and test NG updated here

play02:42

and the version for selenium that is the

play02:45

latest version

play02:46

4.2.0 and for test NG

play02:49

7.1.2 I have created a new package

play02:52

called login test demo inside that I

play02:55

have created a new package that is pages

play02:57

and I've have created a new class called

play02:59

Lo login page now this login page we

play03:01

will be following a page object model

play03:04

that is the reason why I've created a

play03:05

separate class for the page

play03:07

objects the login page class will

play03:10

ideally uh have all the web elements as

play03:13

well as the logic for performing the

play03:15

login and let me take you to the page

play03:18

again and let me just uh log out of this

play03:21

web page and again open the login page

play03:25

so like I said there are three Fields

play03:27

here so let's uh locate the email

play03:30

address field first and you can see

play03:33

email address field has this ID input

play03:35

hyphen email this can be used to locate

play03:38

the uh email address field like you like

play03:41

you can see here private we web element

play03:43

email address field driver. find element

play03:45

by. input hyphen email so this will

play03:49

eventually locate the email address

play03:51

field similarly for the password field

play03:55

we have an ID input hyphen

play03:58

password the same can be use in the

play04:00

password field method as well and it

play04:02

will return the web element for the

play04:03

password field and finally the login

play04:06

button so you can see there is an input

play04:08

tag here and the class name is BTN so uh

play04:12

we can make use of this input tag and

play04:14

this button class uh the name of the

play04:16

class that is BTN so by using CSS

play04:19

selector we can locate the login button

play04:21

input hyphen BTN is the CSS selector

play04:23

that will be used to locate the login

play04:25

button once all of these three elements

play04:28

are located on the page we can can use

play04:30

them in the perform login method this

play04:32

method takes two parameter email and

play04:35

password these are this will be

play04:36

eventually uh these parameters will

play04:39

eventually be replaced by the actual

play04:40

value for the username and the password

play04:42

and uh that will help in performing the

play04:44

login so first of all this perform login

play04:47

method will clear the email address

play04:49

field this is done to ensure that uh the

play04:52

existing value that is been typed in the

play04:54

email address or the password field

play04:56

actually gets cleared so we have a blank

play04:58

field and we can take uh try out with

play05:00

all the scenarios that we want to test

play05:02

so email address field will be cleared

play05:04

first then uh using the send Keys method

play05:07

we'll Supply the email value and

play05:10

similarly for the password ke field will

play05:12

be cleared and then we'll Supply the

play05:14

password value and finally the login

play05:16

button will be

play05:17

clicked now as you click on this login

play05:20

button there are two uh conditions that

play05:23

can happen one if the password uh or the

play05:26

email address is not valid then in that

play05:29

case

play05:30

uh a message will be displayed a warning

play05:32

message let me get that message again

play05:36

let me type in any value out there

play05:38

because anyways it's an invalid

play05:41

case and you can see there is a message

play05:43

coming here warning no match for email

play05:45

address and our password and we can just

play05:48

uh check out for the selector you can

play05:51

see there is a ID account hyphen login

play05:54

inside that there is a class alert this

play05:57

can be used to locate this uh particular

play05:59

web element and finally get the text out

play06:01

of it so

play06:03

hash account hyphen login and then d.

play06:06

alert hash refers to this ID hash

play06:10

account hyen login and then div. alert

play06:13

div. alert dot is ideally a Shand for

play06:15

the class name and that is why it has

play06:17

been used in this CSS selector and

play06:18

finally this will return a string by

play06:22

checking out the text so this selector

play06:24

will be located first and then it will

play06:26

return return the text out of it in the

play06:27

string format so this get message text

play06:30

ideally should return return this text

play06:32

warning no match for email address and

play06:34

or password and the second condition is

play06:37

if this uh login is

play06:40

successful let me login

play06:43

successfully Thon at

play06:48

gmail.com and then

play06:51

login so once this login is successful

play06:54

we'll have this my account page getting

play06:56

loaded and we will verify this my

play06:59

account text is displayed so uh for this

play07:02

I have created another page my account

play07:04

page and in this page we have this title

play07:08

get page title this method that actually

play07:10

Returns the text in the string format so

play07:12

you can see there is an ID called

play07:14

content inside that we have a h2 tag so

play07:17

ideally a CSS selector hash content

play07:20

space H2 can be used to get the text of

play07:22

this uh field this particular web

play07:25

element my account you can see there are

play07:28

two extra ra there is one extra

play07:30

Constructor that is updated this is done

play07:32

for the page object model the web driver

play07:35

interface is been initialized in the uh

play07:38

login page Constructor similarly the

play07:41

same Constructor is applied in uh my

play07:43

account page as well and final value for

play07:46

this web driver uh object will be

play07:49

supplied through the base

play07:51

test so in the base test you can see

play07:54

there is a before test annotation that

play07:56

has a SE setup method uh this method is

play08:00

eventually uh start the Chrome dri

play08:02

Chrome browser and then maximize the

play08:04

window and apply the implicit weight of

play08:06

30 seconds this is done to ensure that

play08:08

we have the page loaded correctly before

play08:10

we perform the uh login test scenarios

play08:13

and there is an after test annotation as

play08:15

well which will eventually quit the

play08:16

driver gracefully let's now move on to

play08:19

the test if you uh check out the test

play08:22

there is a new class created that is

play08:24

called as login page test and that

play08:27

extends the base test so so once the

play08:31

base test is extended you can use reuse

play08:33

all the variables that are available in

play08:35

the base test method uh that can be used

play08:38

here first of all we will create a bit

play08:40

data provider this data provider

play08:42

annotation is applied on the get login

play08:44

data this method will eventually return

play08:47

the data in the iterator format as an

play08:50

object

play08:51

array so you can see there is a get uh

play08:54

login data array list created which is

play08:57

ideally a list of object arrays and

play08:59

inside that there are different data

play09:01

sets that are being supplied so the

play09:03

First Data is uh having a valid email

play09:07

but an invalid password you can see the

play09:08

third parameter being supplied here

play09:10

false true this is ideally to check if

play09:13

it is a valid test case or an invalid

play09:15

test case so this Boolean false. true

play09:18

will determine if the valid if the test

play09:20

case is valid or invalid false refers to

play09:22

invalid test case and true refers to

play09:24

valid test case the second test set of

play09:26

data that we have is uh having the

play09:28

password field black that it is again a

play09:30

negative scenario hence the false

play09:33

parameter is been updated and finally

play09:35

for the valid scenario which has a valid

play09:38

username and valid password the password

play09:40

is Secret at theate 1 123 the value for

play09:42

the Boolean is is valid user is supplied

play09:45

as true which means this is a valid uh

play09:48

uh valid test uh test data and ideally

play09:52

the my account scenario condition will

play09:54

be checked there so let's move on to the

play09:56

test now before we move on to the test

play09:58

one thing to note here is for supplying

play10:00

the data provider in the test we need to

play10:02

have this data provider parameters and

play10:04

the name of that method being supplied

play10:06

in the at theate test annotation and

play10:09

then we have the test method finally

play10:11

created test login feature which will

play10:13

eventually uh consume three parameters

play10:16

first one is email the second one is

play10:18

password and third one is the is valid

play10:21

user so the test will first navigate to

play10:24

the login page and then the login page

play10:26

class will be instantiated where the

play10:28

perform login email uh and password with

play10:32

the parameters this method will be

play10:35

called once the login is done then the

play10:38

condition will be checked if it is not a

play10:40

valid user then the assertions will be

play10:42

performed to check that the warning

play10:43

message is displayed correctly in case

play10:46

if it is a valid case if it is not a

play10:48

valid case then this method Sr equals

play10:50

will be applied if it is a valid case

play10:52

then my account page class will be

play10:54

instantiated and ideally the get page

play10:56

title method will be called which will

play10:58

check if there is the

play10:59

uh text my account displayed on the page

play11:02

so let me quickly run this

play11:04

test and see how this performs in the

play11:08

automation so you can see the setup

play11:11

method being executed first as it is uh

play11:14

at it as it has the before test

play11:16

annotation mentioned over

play11:19

it so the test is navigating to the

play11:22

login page now and you can see the

play11:25

invalid test been executed and finally

play11:27

the valid test

play11:30

and we should wait till the my account

play11:32

page

play11:33

loads and finally the third test

play11:35

executed successfully and we can see

play11:37

that uh all the three scenarios executed

play11:41

successfully in a single go with the 20

play11:44

within 26 seconds 26 seconds 459

play11:47

milliseconds so this is how you can

play11:49

automate different test scenarios using

play11:52

this data provider we have an example of

play11:54

login test scenarios uh been used if

play11:56

there are more scenarios that you can

play11:58

test you can just add up the scenarios

play12:00

by copy pasting this line and then

play12:02

whatever if you want to test with the

play12:04

blank username or blank password then

play12:06

you can put it the same way

play12:09

here but ideally make sure that whatever

play12:12

scenarios that you put if it is a valid

play12:14

invalid scenario you need to put it

play12:17

above the true the the valid scenario

play12:20

should be tested in the last

play12:23

ideally uh this will help up in testing

play12:25

the other scenarios that are related to

play12:27

uh login which requires login so if the

play12:29

login scenarios gets executed

play12:32

successfully then the next scenario

play12:33

after login can be performed so this is

play12:36

how you can test uh the different login

play12:38

scenarios easily using selenia web

play12:40

driver and test NG uh explicitly using

play12:43

the data provider

play12:45

annotation thank you so much for

play12:47

watching this video I hope you learn

play12:48

something new from this video do like

play12:50

share and comment and do share it with

play12:52

your network as well thank you

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
SeleniumTestNGAutomationLogin PageWeb TestingJavaChrome BrowserData ProviderTest ScenariosWeb Development