Detailed Notes
Selenium is a popular way to automate browser activity, record sessions, and setup testing scripts. In this series of tutorials we look at using Java and Selenium for all your browser automation needs.
Transcript Text
[Music] the last series we talked about was kind of integrating testing into your development flow and i wrapped up that one by talking about my framework with the two pieces one about the testing side of the framework and the other side being the actual java framework piece using the different test frameworks like pojo or page optic models things like that this one we're moving away from that just slightly and this may become a new series if you guys like it let me know and i can kind of put something else together but a lot of problems people have even with frameworks or with automated testing in general is actually how do we actually go and interact with our web pages and since selenium is pretty much the forefront for almost all web page interactions and automated testing i decided to do a presentation today on essentially how do we interact with the web pages uh so how do we use the selenium web driver to actually test our code to actually work with the web page so this is going to be kind of a high level i'm not going to get down into the wiis that's just setting up the binaries configuring your project we're actually just going to be kind of jumping right into it i'll highlight some of the components to actually use the components of the webdriver to get started but we're going to focus primarily on looking at the web pages themselves how to identify the individual page elements that we want to use for testing and then how we actually use those with the web driver so with that that's pretty much it for the slide because this is going to be pretty much all hands on so if you want to learn more about selenium and actually kind of do the how to's and everything if you go to selenium.gov this is their uh professional or i guess their main page for everything selenium and there's three flavors just blending you get the selenium ide which is a record playback tool you can use for chrome and firefox uh which is good if you're just testing interactively and want to make sure that something works on a page not really good for long term automation or testing this is just kind of one and done recording tool for frameworks or more long-term automation you want to do things with the selenium web driver now if you have more of an enterprise environment and you're going to do more load testing or more browser os testing you will want to look at the selenium grid now i've done a presentation on the grid in the past using kubernetes and docker so if you want you may want to go back and revisit that if you want to learn more about the grid today we're going to primarily just focus on using the web drive so if you've never used webdriver you can get it from here however i'm going to show you i live in the java world these days but i'll show you how to get all this using maven so you don't even have to come out to the dev page to get your binaries at all another useful point though on their site though is their documentations page this is a great how-to to get started with the sling selenium and the selenium web driver so basically you come down here getting started with selenium web driver it gives you kind of a quick overview some how-to's and then primarily the web drivers the driving factor of everything that you're going to be doing so here we want the selenium api all right so in the selenium api this is pretty cool they've updated all their uh java dots essentially to be the new uh look and feel for uh documentation which is really nice and easy to read a little tricky to navigate through initially but once you get the hang of it it's not that bad the main classes we're going to be concerned about today are going to be the buy webdriver and web element so these are going to be the three things you have to get familiar with with selenium in order to one create your tests two interact with the web browser and three locate your web elements on the page and actually interact with the web element so the web driver is the primary class that you're going to need and as the class name pretty much clarifies is a web driver it drives the interaction between your test and the browser that you're going to use as you can see here it supports chrome edge firefox ie and safari they used to support uh phantomjs and ghost.js but that has since been discontinued since now um since edge is a build on chromium so chrome edge firefox are all pretty much uh built on the chromium or the gecko initial browser open source browsers so those browsers all have the capability of running headless so you no longer need the support for the headless browsers out there like html unit ghost driver or phantom js driver so these are the browsers drivers that are supported and i recommend pretty much looking at chrome and firefox uh those are pretty much the two most easy to use uh edges also now becoming very easy to use because it's actually a very lightweight form of chrome uh because microsoft is pushing fast streamless browsing uh without all the bells and whistles that chrome has that slows down the browser so that's something to look at all right so we have webdriver to actually find our web elements we're going to need to use the by class this is going to be how we look up those locators and by looking at the locators we have a couple different methods here that we use to actually find our locators and we can find them using the css class names we can find them using css selectors we can find them by their individual element id we can look at by javascript we can look it up by link text we can also look it up by the element and attribute name we can also look at by partial link text you can also pull by tag name but as you can see that's being deprecated because really it's not that useful uh it can get very bloated so there this is going to be going away in the near future or you can use the very friendly reg edit xpath to do a more complex look up of your elements and we're going to actually look at an example of using each of these today and then finally once you have your locator once you locate it it's going to return a web element class and this is essentially the class you use to interact with the web element that you pull in so this could be your form input fields your labels your links and so so we will jump out and get right into the code so first we're going to create a class here i created a very simple class called using locators this is a maven project so i basically just created a basic maven archetype project so i would have my main java um source name java and sourcetestjava just to kind of show you how webdriver works i'm not going to include any j unit or testing g components to this example today we're just going to use a straight java class just so you can see how the actual selenium classes work and then we need to configure our palm file to get a couple of things so we need to get the org dot selenium hq dot selenium maven dependency to get our selenium java classes all right so our maven dependencies it's going to pull in all these selenium classes or selenium jar files here now these selenium uh jars here the selenium gas java selenium dash api are the primary ones that you get if you were to download selenium from the selenium site so these are the main binaries there are some additional support binaries you get support remote driver you'll need those as well these other ones here the selenium chrome driver edge firefox and ie driver come from the io github bonnie garcia now the webdriver manager library here that this guy incorporates that pulls in this guy here uh every time you start your test where you start your program you if you use the web driver manager it's going to automatically pull down the latest chrome driver that is required for interacting with the browsers so you no longer have to go out and manage your binaries anymore for your browsers this is a huge time saver uh when dealing with selenium or any type of automation testing okay so let's go back over to our class so we have a simple main method here now just to make sure that things go smoothly today we're going to just start out with a try catch block here because uh sometimes webdriver will crash you accidentally close your browser browser closes unsuspectively so we kind of want to catch that so we'll just do a simple try catch around the whole project here just do a simple exception and we're just not going to do anything with it all right so we're going to do the bulk of our testing in here now the first thing we need to do is we need to get our web driver started out by actually instantiating our web driver so to do that we need these two lines of code here so the first one webdriver manager if you don't use webdriver manager and you just want to use webdriver correctly you're going to have to actually do things like actually set up system dot set properties and then you're going to have to actually set the properties for the particular web driver that you want to use so if you want to use chrome you have to set up the webdriver chrome and then you have to point it to the actual physical location of the uh binaries and this gets tedious because if you want to change your browsers you have to download the latest binaries change your code so i don't recommend doing it this way instead i recommend using the webdriver manager class so we have our selenium webdriver we have our selenium chrome chrome driver and we have the i o github bonnie garcia webdriver manager so to use the webdriver manager it's really straightforward very easy you can do webdrivermanager. and then you just go pick the browser that you want to use firefox edge chromium chrome driver i believe they yes they simplified it now so before they also had gecko drivers but now uh firefox driver encompasses both the gecko and the firefox driver so if you want to use firefox if you use the firebox track to get rid of all the bloatedness of the set path you can do a couple different things here you can set your browser to be 32-bit 64-bit this is actually still necessary if you're doing cross browser testing across older operating systems you'll want to make sure that you actually set the browser to the correct uh fight 32 or 64. and you can also do things like clear cache uh set the particular browser path if you actually want to point to your own binary clear preferences you can also set preferences force cache so you can do a lot of different things here with the particular configuration but just to get started if you do your browser driver.setup you're good to go it has now configured your runtime for that particular browser driver and then the next thing you want to do is you call webdriver and then you tell it which browser you want to use which chrome or which browser driver to instantiate so you can do chrome driver you can do firefox driver firefox driver you could do safari driver so basically all the particular browsers have browser named our browser named driver so once we have our driver this instantiates our driver now once we have the driver once we're done with it we need to make sure we actually uh clean it up we need to actually close it so you want to treat it like an i o or a screen so we have driver dot close but there is also a driver dot clip now the difference between the two of these is one actually closes the browser but if you're dealing with say firefox or chrome where they have the like a manager tool behind the scenes uh what will essentially happen is yes it will close your browser window but it may not close the actual browser application itself so to do that you would need to call driver.quit now for browsers that don't have the management application running behind the scenes if you call quit sometimes that will fail or vice versa if you call close close isn't recognized by the browser but quit is so these will also have to be wrapped in a try catch and this just is quick and dirty um but i just kind of want to show you guys what you need to do just to get started my framework actually encompasses all this so it's very clean all you have to do is just call run test and it will actually build this and it will do all the supporting pieces you need to do behind the scenes but if you just are getting started it's kind of good to see how things work all right so now we have our driver so what can we do with our driver so say we want to actually go out and test walmart so say we wanted to go and test an actual site so we need to get our url so we'll copy walmart.com and we'll do driver dot get and we put in our url and if i did spread sleeve which is not really what you should do for testing or for safeguarding but we'll just do this this way all right so we've got thread sleeve it's just going to pause for a few seconds and we are just going to run this real quick and make sure that walmart.com opens all right so we'll close the existing browser so you'll see this working and we'll just right click and run it there we go and as you can see it put in walmart.com and we navigated over to walmart [Music] you
Transcript Segments
[Music]
the last series we talked about was kind
of integrating testing into your
development flow
and i wrapped up that one by talking
about my framework with the two pieces
one about the testing side of the
framework and the
other side being the actual java
framework piece
using the different test frameworks like
pojo or page optic models
things like that
this one we're moving away from that
just slightly and this may become a new
series if you guys like it
let me know
and i can kind of put something else
together
but
a lot of problems people have even with
frameworks or with
automated testing in general is actually
how do we actually go and interact with
our web pages
and since selenium is pretty much the
forefront for almost all web page
interactions and
automated testing
i decided to
do a presentation today on essentially
how do we interact with the web pages uh
so how do we use the selenium web driver
to actually
test our code to actually work with the
web page so this is going to be kind of
a high level i'm not going to get down
into the wiis that's just setting up the
binaries
configuring your project we're actually
just going to be kind of jumping right
into it
i'll highlight some of the components to
actually
use
the components of the webdriver to get
started but we're going to focus
primarily on looking at the web pages
themselves how to identify the
individual
page elements that we want to use for
testing and then how we actually use
those with the web driver
so with that that's pretty much it for
the slide
because this is going to be pretty much
all hands on
so if you want to
learn more about selenium and actually
kind of do the
how to's and everything
if you go to selenium.gov this is their
uh
professional or i guess their main page
for everything selenium
and there's three flavors just blending
you get the selenium ide which is a
record playback tool you can use for
chrome and firefox
uh which is good if you're just testing
interactively and want to make sure that
something works on a page not really
good for long term automation or testing
this is just kind of one and done
recording tool
for frameworks or more long-term
automation you want to do things with
the selenium web driver
now if you have more of an enterprise
environment and you're going to do more
load testing or more
browser os
testing you will want to look at the
selenium grid now i've done a
presentation on the grid in the past
using kubernetes and docker so if you
want you may want to go back and revisit
that if you want to learn more about the
grid
today we're going to primarily just
focus on using the web drive so if
you've never used webdriver you can get
it from here
however i'm going to show you
i live in the java world these days but
i'll show you how to get all this using
maven so you don't even have to come out
to the dev page to get your binaries at
all
another useful point though on their
site though is their documentations page
this is a great how-to to get started
with the
sling
selenium and the selenium web driver
so basically you come down here getting
started with selenium web driver it
gives you kind of a quick
overview some how-to's
and then
primarily the web drivers the driving
factor of everything that you're going
to be doing
so here we want the selenium
api
all right so in the selenium api this is
pretty cool they've
updated all their uh java dots
essentially to be the
new
uh look and feel for uh documentation
which is really nice and easy to read a
little tricky to navigate through
initially but once you
get the hang of it it's not that bad
the main
classes we're going to be concerned
about today are going to be the buy
webdriver and web element so these are
going to be the three things you have to
get familiar with with selenium in order
to one create your tests
two interact with the web browser and
three
locate your web elements on the page and
actually interact with the web element
so the web driver is the primary class
that you're going to need and as the
class name pretty much
clarifies is a web driver it drives the
interaction between your test and the
browser that you're going to use as you
can see here it supports chrome edge
firefox ie and safari
they used to support uh
phantomjs and ghost.js but that has
since been discontinued since now um
since edge is a build on chromium so
chrome edge firefox
are all pretty much uh
built on the chromium or the gecko
initial browser open source browsers so
those browsers all have the capability
of running headless so you no longer
need the support for the headless
browsers out there like html unit
ghost driver or phantom js driver so
these are the browsers
drivers that are supported and i
recommend pretty much looking at chrome
and firefox uh those are pretty much the
two most easy to use
uh edges also now becoming very easy to
use because it's actually a very
lightweight form of chrome uh because
microsoft is pushing fast streamless
browsing uh without all the bells and
whistles that chrome has that slows down
the browser
so that's something to look at all right
so we have webdriver
to actually find our web elements we're
going to need to use the by class this
is going to be how we look up those
locators and by looking at the locators
we have a couple different
methods here that we use to actually
find our locators and we can find them
using the css class names we can find
them using css selectors we can find
them by their individual element id we
can look at by javascript we can look it
up by link text we can also look it up
by
the element and attribute name we can
also look at by partial link text you
can also pull by tag name but as you can
see that's being deprecated because
really it's not that useful
uh it can get very bloated so there this
is going to be going away in the near
future
or you can use the very
friendly reg edit xpath to
do a more complex look up of your
elements and we're going to actually
look at an example of using each of
these today
and then finally once you have your
locator once you locate it it's going to
return a web element class and this is
essentially the class you use to
interact with the web element that you
pull in so this could be your form input
fields your labels your links and so
so we will jump out and get right into
the code
so first we're going to create a class
here i created a very simple class
called using locators
this is a maven project
so i basically just created a basic
maven archetype
project so i would have my main java um
source name java and sourcetestjava
just to kind of
show you how webdriver works i'm not
going to
include any
j unit or testing g components to this
example today we're just going to use a
straight java class
just so you can see how the actual
selenium classes work
and then we need to configure our palm
file to get a couple of things so we
need to get the org dot selenium hq dot
selenium
maven dependency to get our selenium
java classes
all right so our maven dependencies it's
going to pull in all these selenium
classes or selenium jar files here now
these selenium uh
jars here the selenium
gas java selenium dash api are the
primary ones that you get if you were to
download selenium from
the selenium site so these are the main
binaries there are some additional
support binaries you get support remote
driver you'll need those as well these
other ones here the selenium chrome
driver edge firefox and ie driver come
from the io github
bonnie garcia now the webdriver manager
library here that this guy incorporates
that pulls in this guy here
uh every time you start your
test where you start your program
you if you use the web driver manager
it's going to automatically pull down
the latest
chrome driver that is required for
interacting with the browsers so you no
longer have to go out and manage your
binaries anymore for your browsers this
is a huge time saver uh when dealing
with selenium or any type of automation
testing
okay so let's go back over to our class
so we have a simple main method here now
just to make sure that things go
smoothly today we're going to just start
out with a try catch block here because
uh sometimes webdriver will crash you
accidentally close your browser browser
closes unsuspectively so we kind of want
to catch that so we'll just do a simple
try catch around the whole
project here
just do a simple exception
and we're just not going to do anything
with it
all right so we're going to do the bulk
of our testing in here
now the first thing we need to do is we
need to get our web driver
started out by actually instantiating
our web driver
so to do that
we need these two lines of code here
so the first one webdriver manager if
you don't use webdriver manager and you
just want to use webdriver correctly
you're going to have to actually
do things like actually set up system
dot
set properties and then you're going to
have to actually set the properties for
the particular web driver that you want
to use so if you want to use chrome you
have to set up the webdriver chrome and
then you have to point it to the actual
physical location of the uh
binaries
and this gets tedious because if you
want to change your browsers you have to
download the latest binaries change your
code so i don't recommend doing it this
way instead i recommend using the
webdriver manager class
so we have our selenium webdriver we
have our selenium chrome chrome driver
and we have the
i o github bonnie garcia webdriver
manager
so to use the webdriver manager it's
really straightforward very easy you can
do webdrivermanager. and then you just
go pick the
browser that you want to use firefox
edge chromium
chrome driver
i believe they
yes they simplified it now so before
they also had gecko drivers but now uh
firefox driver encompasses both the
gecko and the firefox driver so if you
want to use firefox if you use the
firebox track
to get rid of all the
bloatedness of the set path you can do a
couple different things here you can set
your browser to be 32-bit 64-bit this is
actually still
necessary if you're doing cross browser
testing across older operating systems
you'll want to make sure that you
actually set the
browser to the correct uh fight 32 or
64.
and you can also
do things like clear cache
uh set the particular browser path if
you actually want to point to your own
binary
clear preferences you can also set
preferences force cache
so you can do a lot of different things
here with
the
particular
configuration
but just to get started if you do
your browser driver.setup you're good to
go it has now configured your runtime
for that particular browser driver
and then the next thing you want to do
is you call webdriver and then you tell
it which browser you want to use which
chrome or which
browser driver to instantiate so you can
do chrome driver you can do firefox
driver
firefox
driver
you could do
safari
driver so basically all the particular
browsers have browser named our browser
named driver
so once we have our driver
this
instantiates our driver
now once we have the driver once we're
done with it we need to make sure we
actually uh clean it up we need to
actually close it so you want to treat
it like an i o or a screen so we have
driver
dot
close
but there is also a driver dot clip
now the difference between the two of
these is one actually closes the browser
but if you're dealing with say firefox
or chrome
where they have the like a manager tool
behind the scenes
uh what will essentially happen is yes
it will close your browser window but it
may not close the actual browser
application itself so to do that you
would need to call driver.quit
now for browsers that don't have the
management
application running behind the scenes if
you call quit sometimes that will fail
or vice versa if you call close close
isn't recognized by the browser but quit
is so
these will also have to be wrapped in a
try catch
and this just is quick and dirty um but
i just kind of want to show you guys
what you need to do just to get started
my framework actually encompasses all
this so it's very clean all you have to
do is just call
run test and it will actually build this
and
it will do all the supporting pieces you
need to do behind the scenes but if you
just are getting started it's kind of
good to see how things work
all right so now we have our driver so
what can we do with our driver so say we
want to actually go out
and test walmart
so say we wanted to go and test an
actual site so we need to get our url so
we'll copy
walmart.com and we'll do driver dot
get
and we put in
our url
and if i did
spread
sleeve which is not really what you
should do for testing or for
safeguarding but we'll just do this
this way all right so we've got thread
sleeve it's just going to pause for a
few
seconds
and
we are just going to run this real quick
and make sure that walmart.com opens
all right so we'll close the existing
browser so you'll see this working
and we'll just right click and run it
there we go
and as you can see it put in walmart.com
and we navigated over to walmart
[Music]
you