How to automate Login page using Selenium WebDriver with Java? | Selenium WebDriver Tutorial
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
π 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.
π 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.
π 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
π‘TestNG
π‘LoginPage
π‘Page Object Model (POM)
π‘Data Provider
π‘Test Scenarios
π‘Web Elements
π‘Assertions
π‘My Account Page
π‘Implicit Wait
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
[Music]
hello folks welcome back to my channel
in this video we will learn how to
automate multiple test cases of the
login page as you all know login page is
one of the important component of the
website it is pivotal in ensuring user
security and overall website performance
let's now learn about the website and
the tools used in this video for
demo we will be using the lra test e
Merce website for automating the
multiple login test
scenarios and uh the tools used will be
selum web driver with Java we will be
running the test on the Chrome browser
and using test NG explicitly we will be
using data provider and rotation of test
NG to pass on the login credentials in
the test now let's talk about the test
scenarios that we will be automating so
first of all we will be navigating to
this login page and then allocating the
email address password field and the
login button we will be automating three
different scenarios of this login page
first scenario will be to enter a valid
email let me show that to you manually
david. Thomson
gmail.com and we will enter an invalid
password let's let enter an ABCD kind of
a value in the password field and click
on login so the first case will be to
enter a valid email address and an
invalid password and then verify this
message warning no match for email
address and or
password the next case would be to enter
a valid email address NN and then leave
the password field blank and click on
login so again it will show the same
message we will again verify the message
is been displayed uh this is to ensure
this negative scenarios actually ensures
that the login feature is actually
working fine and we have a respective
message displayed in case if the login
does not happen in the website finally
on the third scenario it will be a
positive scenario and we'll verify by
entering a username that is uh by
entering an email address that is valid
and entering a valid password as well
and then click on login button so once
the login is successful it should be
taken to the my account page and we will
verify that this my account text is
displayed after the login is successful
so let's now move on to the
code and uh before I just move on to the
uh code walk through let me first give
you a small demo around the project
structure as well as the libraries used
so like I said we will be using selenium
web driver with Java and test NG so you
see you can see the dependencies
selenium Java and test NG updated here
and the version for selenium that is the
latest version
4.2.0 and for test NG
7.1.2 I have created a new package
called login test demo inside that I
have created a new package that is pages
and I've have created a new class called
Lo login page now this login page we
will be following a page object model
that is the reason why I've created a
separate class for the page
objects the login page class will
ideally uh have all the web elements as
well as the logic for performing the
login and let me take you to the page
again and let me just uh log out of this
web page and again open the login page
so like I said there are three Fields
here so let's uh locate the email
address field first and you can see
email address field has this ID input
hyphen email this can be used to locate
the uh email address field like you like
you can see here private we web element
email address field driver. find element
by. input hyphen email so this will
eventually locate the email address
field similarly for the password field
we have an ID input hyphen
password the same can be use in the
password field method as well and it
will return the web element for the
password field and finally the login
button so you can see there is an input
tag here and the class name is BTN so uh
we can make use of this input tag and
this button class uh the name of the
class that is BTN so by using CSS
selector we can locate the login button
input hyphen BTN is the CSS selector
that will be used to locate the login
button once all of these three elements
are located on the page we can can use
them in the perform login method this
method takes two parameter email and
password these are this will be
eventually uh these parameters will
eventually be replaced by the actual
value for the username and the password
and uh that will help in performing the
login so first of all this perform login
method will clear the email address
field this is done to ensure that uh the
existing value that is been typed in the
email address or the password field
actually gets cleared so we have a blank
field and we can take uh try out with
all the scenarios that we want to test
so email address field will be cleared
first then uh using the send Keys method
we'll Supply the email value and
similarly for the password ke field will
be cleared and then we'll Supply the
password value and finally the login
button will be
clicked now as you click on this login
button there are two uh conditions that
can happen one if the password uh or the
email address is not valid then in that
case
uh a message will be displayed a warning
message let me get that message again
let me type in any value out there
because anyways it's an invalid
case and you can see there is a message
coming here warning no match for email
address and our password and we can just
uh check out for the selector you can
see there is a ID account hyphen login
inside that there is a class alert this
can be used to locate this uh particular
web element and finally get the text out
of it so
hash account hyphen login and then d.
alert hash refers to this ID hash
account hyen login and then div. alert
div. alert dot is ideally a Shand for
the class name and that is why it has
been used in this CSS selector and
finally this will return a string by
checking out the text so this selector
will be located first and then it will
return return the text out of it in the
string format so this get message text
ideally should return return this text
warning no match for email address and
or password and the second condition is
if this uh login is
successful let me login
successfully Thon at
gmail.com and then
login so once this login is successful
we'll have this my account page getting
loaded and we will verify this my
account text is displayed so uh for this
I have created another page my account
page and in this page we have this title
get page title this method that actually
Returns the text in the string format so
you can see there is an ID called
content inside that we have a h2 tag so
ideally a CSS selector hash content
space H2 can be used to get the text of
this uh field this particular web
element my account you can see there are
two extra ra there is one extra
Constructor that is updated this is done
for the page object model the web driver
interface is been initialized in the uh
login page Constructor similarly the
same Constructor is applied in uh my
account page as well and final value for
this web driver uh object will be
supplied through the base
test so in the base test you can see
there is a before test annotation that
has a SE setup method uh this method is
eventually uh start the Chrome dri
Chrome browser and then maximize the
window and apply the implicit weight of
30 seconds this is done to ensure that
we have the page loaded correctly before
we perform the uh login test scenarios
and there is an after test annotation as
well which will eventually quit the
driver gracefully let's now move on to
the test if you uh check out the test
there is a new class created that is
called as login page test and that
extends the base test so so once the
base test is extended you can use reuse
all the variables that are available in
the base test method uh that can be used
here first of all we will create a bit
data provider this data provider
annotation is applied on the get login
data this method will eventually return
the data in the iterator format as an
object
array so you can see there is a get uh
login data array list created which is
ideally a list of object arrays and
inside that there are different data
sets that are being supplied so the
First Data is uh having a valid email
but an invalid password you can see the
third parameter being supplied here
false true this is ideally to check if
it is a valid test case or an invalid
test case so this Boolean false. true
will determine if the valid if the test
case is valid or invalid false refers to
invalid test case and true refers to
valid test case the second test set of
data that we have is uh having the
password field black that it is again a
negative scenario hence the false
parameter is been updated and finally
for the valid scenario which has a valid
username and valid password the password
is Secret at theate 1 123 the value for
the Boolean is is valid user is supplied
as true which means this is a valid uh
uh valid test uh test data and ideally
the my account scenario condition will
be checked there so let's move on to the
test now before we move on to the test
one thing to note here is for supplying
the data provider in the test we need to
have this data provider parameters and
the name of that method being supplied
in the at theate test annotation and
then we have the test method finally
created test login feature which will
eventually uh consume three parameters
first one is email the second one is
password and third one is the is valid
user so the test will first navigate to
the login page and then the login page
class will be instantiated where the
perform login email uh and password with
the parameters this method will be
called once the login is done then the
condition will be checked if it is not a
valid user then the assertions will be
performed to check that the warning
message is displayed correctly in case
if it is a valid case if it is not a
valid case then this method Sr equals
will be applied if it is a valid case
then my account page class will be
instantiated and ideally the get page
title method will be called which will
check if there is the
uh text my account displayed on the page
so let me quickly run this
test and see how this performs in the
automation so you can see the setup
method being executed first as it is uh
at it as it has the before test
annotation mentioned over
it so the test is navigating to the
login page now and you can see the
invalid test been executed and finally
the valid test
and we should wait till the my account
page
loads and finally the third test
executed successfully and we can see
that uh all the three scenarios executed
successfully in a single go with the 20
within 26 seconds 26 seconds 459
milliseconds so this is how you can
automate different test scenarios using
this data provider we have an example of
login test scenarios uh been used if
there are more scenarios that you can
test you can just add up the scenarios
by copy pasting this line and then
whatever if you want to test with the
blank username or blank password then
you can put it the same way
here but ideally make sure that whatever
scenarios that you put if it is a valid
invalid scenario you need to put it
above the true the the valid scenario
should be tested in the last
ideally uh this will help up in testing
the other scenarios that are related to
uh login which requires login so if the
login scenarios gets executed
successfully then the next scenario
after login can be performed so this is
how you can test uh the different login
scenarios easily using selenia web
driver and test NG uh explicitly using
the data provider
annotation thank you so much for
watching this video I hope you learn
something new from this video do like
share and comment and do share it with
your network as well thank you
Browse More Related Video
Angular Login and Signup Page | Local-Storage | angular tutorial | angular tutorial for beginners
Netflix Automation Using Python And Selenium
AppInventor-Tutorial #3-Login Screen- Part#2
All about Firebase Authentication π₯ | Login & Signup | Jetpack Compose
My progressive report week-6
How To Add Different Types Of Background Images In Oracle APEX Applications - Part 41
5.0 / 5 (0 votes)