How to Write & Run a Test Case in Selenium | Selenium Tutorial | Selenium Training | Edureka

How to Write & Run a Test Case in Selenium | Selenium Tutorial | Selenium Training | Edureka


Hello all attendees this is Siddhant from
Edureka and I welcome you all to today’s learning journey on an automated
testing tool called Selenium now if you are IT aspirant this video is for you if
you look into switched from manual to automation testing well this video is
definitely for you in fact learning selenium can very well be your entry
point to the software industry so by the end of today’s session I’m going to make
sure that you guys are equipped to run your first selenium test case but before
we begin let me quickly curse you all through today’s training session so
first I’ll be talking about why you should be using selenium then I will
tell you what selenium is then I’ll introduce you to the versions it has had
and the suite of tools that it offers then I will show you how to set selenium
up and finally we will be going hands-on in selenium and running our first test
case so without any further ado let’s get started so the first thing that I’d
like you people to understand is why go with selenium as your tool for testing
now starting with the most obvious reason selenium is an automated testing
tool now when I say automated testing tool I’m fundamentally referring to the
software tools that can enable automation or automate the process of
testing an application unlike the manual testing tools where every possibility of
input output and interaction with the application has to be recorded reported
and compared manually so just like it sounds manual testing can be a really
tedious task it’ll need you to expend a lot of energy and time especially in
today’s world where every application or website has innumerable lines of code
and mundane amounts of data behind them but with automated tools all that hassle
can just become automatic now how many of you remember the rudimentary handheld
fans that we use back in the day so it was basically this flat surface that had
to be waved back and forth continuously for us to feel cooler on hotter days
well we’ve come a long way since then haven’t we all takes now is the flick of
a switch or just pressing a button for our air conditioners to kick in and make
it cold for us anyways I was just trying to tell you how we have been moving from
manual to automatic ever since so somebody seems to have a question
Mahesh wants me to tell you if selenium is the only automated testing tool that
there is well know Mahesh you’re right it isn’t
so why should you use selenium then well to answer that question we’ll have to go
meet others that are like selenium so qtp are ft and testcomplete are also
automated testing tools and are the very best based on industry standards so Qt P
stands for quick test professional and RF T stands for rational functional
tester so I’ve gone ahead and I’ve put them all into a table so that it’s easy
for us to compare now the first feature we’re looking at is license qtp requires
you to be licensed by HP similarly our ft by IBM and testcomplete by smart bear
software but selenium is an open-source tool and does not require a license to
be used now these license products can be pretty
expensive for just a single person to buy but open-source products can be used
by just about anybody for free now talking of customer support the license
products will never let you down selenium however does not really have a
rigid support for customers but depends entirely on an open source community
with lots of insanely active developers on it now coming to the programming
skills UTP and rft does not require you to be much of a coder there’s complete
we’ll need some coding skills though and in case of selenium you will get to
explore and flaunt all your coding skills moving on QT p and r ft work only
on Windows and has complete limits itself to only Windows 7 or higher
versions of Windows but selenium on the other hand works good on almost any
operating system now the final feature we’re looking at is language support
we’re in QT p supports only vbscript rft supports Java and c-sharp has complete
supports vbscript jscript Delfy script c++ and c sharp where as selenium gives
you a dynamic range of support for almost all popular programming languages
like Java c-sharp Ruby Python Perl and PHP now all that being said I take it
that you folks now have a better understanding so as to why selenium is a
technology that has been trending among its peers
so apart from beating the challenges of manual testing here’s why selenium is
your best choice being open-source it is available for free it does not really
matter what operating system you’ve been using
also selenium will let you perform tests on almost any web browser and let you
pick whichever programming language you comfortable in writing your test cases
in so before we move on to define what selenium is you should know that there
are several tools to test every other type of applications but dynamic
occasions are best tested with selenium and that those are the only kinds of
application that selenium is designed to test now that brings us to our next
topic let us finally establish a concrete
definition into what selenium is so selenium is just an open-source tool
that can automate almost any web browser and that is all there is to it so let us
go ahead and exemplify this definition now here’s a manual tester who’s really
troubled with testing the overwhelming web applications that make up most of us
Ibis paste today so he’s really stressed over the alarming number of test cases
he has to work upon every day so all that stress at work makes him feel like
his head is always at the brink of an explosion but don’t worry I went ahead
and I told him what selenium could do so let us see what he has to say now Wow
seems like he’s pretty relaxed now huh well what can I say I guess selenium can
do that to you anyways let’s take note of a few
important points before we move on to our next topic so as I have already
mentioned selenium can only be used to test web applications it is fast and
easy to use even with large sets of data and as a guaranteed accuracy so with too
much data to handle humans can be prone to mistakes but lucky for us selenium is
not human and now that we know what selenium is let us also quickly go over
how web applications are tested being able to automate any web browser
selenium directly run scripts in that browser to simultaneously automate the
web application and test it so we’ll be running our first test case towards the
end of today’s session so that you people can relate to everything that we
are learning right now let me now introduce you to the suite of
tools that selenium comprises and the versions that have evolved over the
years now the latest release of selenium is version 3.1 2.0 and these are the
components that selenium has offered over the years the selenium IDE the
selenium grid selenium RC and the selenium webdriver so let us look into
each of these and understand why they’re a part of selenium so initially selenium
RC was used to write test cases in different programming languages and run
recorded scripts in any browser but RC had a few drawbacks that led to its
deprecation like say RC had a complex architecture which required everything
to be passed through a RC server making it really slow now moving further we
also have the IDE which was previously known as the selenium recorder it
provides an environment to execute test cases record them and export the
programming code in different languages and then we also have the selenium grid
that enables parallel and distributed testing by executing test cases across
remote machines irrespective of whatever operating system they might use now that
leaves us with the webdriver let me simultaneously talk about the
evolution of selenium as we understand what webdriver is so the first version
of selenium came with the selenium IDE the selenium grid and the selenium RC a
little later a separate project called webdriver was also getting popular as a
superior tool for browser automation and by the time selenium launched its second
version it had merged selenium RC with the
webdriver and decided to call it selenium webdriver instead now this was
because webdriver overcame all the shortcomings that
selenium RC had been posing for a while in fact webdriver enabled the scope for
providing better functionalities with selenium like with the advent of
webdriver selenium had no need for the RC server to be the middleman anymore it
could now directly interact with the browser to run tests making it faster
than ever it also removed all the redundancies and confusing commands of
selenium RC and made sure that disabled elements on any web page or web app stay
inaccessible so by the third version selenium webdriver witnessed
improvements of all sorts and became the flagship tool for selenium so it could
basically do everything that selenium RC did but in a better way and with more
features all right so now that we have seen the Y
and the watt of selenium we’ve also made ourselves familiar with its version and
components let us now move to the how of selenium as in how to set it off for use
so there are three important prerequisites through using selenium
so first we’ll be needing a way to write out s scripts in which will be provided
by our programming language which in our case will be Java then we’ll need an
environment where those programming codes make sense and can be executed
which will be provided by our IDE there is Eclipse and finally we’ll be needing
a framework which is essentially a set of tools which will allow us to access
the proper resources to automate and test the web applications and that is
provided by selenium so let’s get started on installing and setting these
up so the first step will be installing Java and how can you do that you can
visit the link that’s given under the slide and download the latest release of
the Java platform so I’ll anyways go ahead and give you a demo of it let’s
open a browser type in Java into it once you’ve done that let’s look for Java
standard edition here is our standard edition so let’s go
to s e downloads here we can see 10 to be the latest release of Java go ahead
and download the JDK for it accept the license agreement and download JDK for
your operating system once you’ve downloaded the JDK you’ll get a set of
file please go ahead and install it on your system but we still have one more
step to do before we can use Java on our system so once you’re done with the
installation the next process would be setting up
your environment variables to use JDK with Eclipse so how can we do that it’s
pretty simple you can go to my PC or this PC and go to the properties the
system window opens you go to the advanced system settings so once you’ve
clicked on advanced system settings there’ll be another window that pops up
with the name system properties find the Advanced tab under it and go to
environment variables so once you’ve done that the environment variables
window now pops up find the user variable so go ahead and click on the
new button under the user variables once you’ve done that there should be a
window that says edit user variable now under that window go to the variable
name and set it as path and as for the variable value you will have to find the
JDK folder that has been installed on your system go into it and there must be
a folder called bin copy the location of the folder and paste it as the variable
value for path so basically we’ll need the location of the bin folder in your
JDK folder so once you have done that go ahead and click on OK and create another
new variable in the same fashion and name it Java underscore home this time
and set the value of the variable as the location of the JDK folder on your
system so once you’ve done that click on OK and you’re done setting up your
environment variables and you can use JDK with eclipse so let’s go ahead and
download eclipse first so you can visit the link that’s been given here or i’ll
anyways be doing it let’s go google and click on the address bar type in eclipse
once we’ve done that we find the official website
we can see the latest release of eclipse to be oxygen go ahead and download it
from a page like this and once you’ve done that you’ll get a setup file please
go ahead and install that set of file on your system so that is all you need to
do for eclipse so once a clip has been installed the next step would be
downloading selenium Java files you can visit the link here and you’ll find a
window of this kind under which there will be many versions of selenium go
ahead and select the version that you want and download it I’ll anyways be
doing it so that you people know which files exactly to download let’s go to
selenium this time once you’ve done that go to the official website click on
downloads you can see the latest release to be heard so you can go ahead and
download it or if you want to download one of the previous releases click on
previous releases you’ll find so many versions here let’s say we want to
download 3.9 now there’s so many files in the 3.9 folder but you don’t have to
download all of them we’ll just be needing selenium server three point nine
point one dot zip and selenium server standalone three point nine point one
dot jar so download both these file you should get a zip folder and a jar file
let’s then get back to the web page and I’ll encourage you to download a few
more things that will be requiring the first is a driver for the internet
explorer downloaded according to your specifications then come to third-party
drivers bindings and plugins install the gecko driver from Mozilla Firefox and
the chrome driver for Google Chrome so once you have done that you should get
three more zip files let me show you how they should look after you’ve downloaded
them so these are the drivers that we downloaded go ahead and extract them
similarly this should be the selenium server zip file go ahead and extract it
once you’ve extracted the drivers they should be in folders like this
containing the respective drivers similarly the jar files the selenium
server standalone is a Java file that we downloaded and we also extracted a few
more Java file like these and then there are more Java files inside the lips
folder so we’ll be needing all of these let us go ahead to the slide so once
we’ve downloaded the jar files we’ll have to configure Eclipse for selenium
now how can we do that for that we’ll have to open eclipse first let’s go
ahead and do that there we go so first there’ll be an eclipse launcher
they’ll ask you to set a workspace it’s basically just a folder where all your
projects are going to be saved in so click on launch once you have done a
fiying your workspace here we are see what a slide says so it’s telling us
to create a new Java project name it then configure the build path for
Eclipse to recognize selenium right so these are the steps we’ll have to add
the jar files that we’ve downloaded and click on apply and close
so let’s go ahead and do that so the first thing we’ll be doing is creating a
new project that is a new Java project and name it selenium project let’s say
once we have done that let’s go ahead and click on finish so here is our
project under which we have a source folder and the Java files so let us go
ahead and add the selenium files to it we go to build path we configure Bell
path under the module path we can see all the Java files so let’s go to class
path and add the external jar files so find the location of your Java files
then select them all open so I have no jar files that I have to add a jar files
so there were two in this folder open and there must be another one that is
the standalone one so let’s go ahead and add that too and finally when you’re
done go ahead and click on apply and close so once you’ve done that there
would be another folder called reference libraries
so now you’ve configured eclipse to use selenium in it so let’s go to our next
slide now all right so that brings us to our last section which is hands-on in
selenium so now that the prerequisites have all been installed and selenium has
been configured for Eclipse IDE how about we automate and test Gmail using
Google Chrome well okay so this is the test script for it let me anyways go and
write it down so that we can run our first test script what will we do we’ll
go ahead and first create a package just like Java goes so co dot ed u rayker dot
let’s say selenium dot demo right so let’s click on finish go ahead and
create a class let’s sell me my class demo as well and I’ll make sure this is
our main method in it and click on finish
so here we have our class let’s go ahead and write the test case so while I write
the code I’ll also be walking you through it so
don’t you worry if you don’t understand something we’ll all be covering that so
now we have our Java files here and our jar files for selenium here but what
about the drivers that we added so these drivers are really important things they
are the ones that can instantiate your browser which means drivers are
necessary if you want to automate your web browsers so we’ll have to type in a
command so that our drivers are connected to our
program so the command for it is set property under which we’ll have to tell
the command about what driver we’re talking about first we’re talking about
the web driver that is of Google Chrome ok so once I’ve done that let me also go
ahead and tell the command where to find it so I’ll have to go and find my Google
Chrome driver which is chrome driver copy the location for it so this is the
location of the folder that it is in but we need the location of the file so
we’ll be adding that so now that we have done that let us add the name of the
file as well which is chrome driver and what is the type of file it is it is an
exe file so that’s it we’ve gone ahead and we have successfully connected the
driver to our programs so let us go ahead and create a reference variable
for the interface web driver so I’ll name my reference variable driver and
assign it an object of the class chrome driver now these interfaces and classes
have been provided to us by the selenium Java files that we added we just have to
import it to our program so I use a constructor to initialize the reference
variable with an object so now you can see an error here that’s because we’ve
not imported our interface and our classes so if you hover your mouse over
it you can see an option to import the interface first and then let’s go and
import the class as well we’ve imported the class as well now so now let’s use a
driver to call some methods that will now let’s
say open and website for me first so what website do we want to open we
want to open Gmail so I’ll have to type in that dot gmail.com right so that is a
web page now once it has opened the web page for me I’ll ask it to maximize the
window that viewing my browser in we can do that by the commands driver dot
manage dot window dot maximize okay so let’s use a driver and find a web
element now what is a web element well any component anything on a web page
isn’t web element okay so in this case let’s say this is the place holder where
I will want my email ID to be so this is my web element so let me inspect it so
once I’ve done that I can see this web element to have a few attributes here so
that is how we are going to find that web element and send some text to it
which is in my case my email id so find element once you’ve done that
we’ll also have to tell it how to find the element so we’ll be using the ID
name to find the element which is identifier ID so let’s say by ID we’ll
have to tell it the name of the ID as well
and we just saw the name of the ID to be identifier and I the right okay so once
you’ve done that we’ll also have to tell it what to do once it finds the web
element for us it’ll send some text to it which can be done by the command send
so what keys do we want to send it I want to send an email IB let’s say right
I’ve send an email id to it now once I will entered my email id I will like to
click on the next button let’s go and inspect it all right so I can see the
class name to VCW ak9 so let me go ahead and find the element
by the class name and the command would be by dot last
name and we’ll have to give it the class name as well which in our case was C W a
canine right let’s Tarrant now we’ve done that so what does it do once it
finds it it’ll click on the button great so let’s tell it to click we’ve
done that so all of this should happen automatically that’s the entire purpose
of writing the script so now that I’ve gone ahead and I have automated some of
my browser features how about I also build a small test case so let’s say I
declare a variable of the string type and I name it a tea for my reference to
actual title and I ask my driver to get me the title of the webpage that it
automatically opened so get title so what should our title be actually I mean
since we opening Gmail the riedel should be Gmail right so let me go ahead and
create another string variable name it et for expected title and we expecting
it to be Gmail right so let me also go ahead and close the browser that I’ve
opened with the driver door close command now after that let me go ahead
and use the string variables to establish a test condition so that can
be done best with the if statement so let’s say 80 dot equals I want to ignore
cases I just want the names to be same so I’ll tell it to ignore cases and
so if this happens if they match we’ll know that our test is successful we print the same test
fool and if that does not happen we’d know that something went wrong and our
test was unsuccessful so let’s print out the same print Ln and
what do we write we wanted to display test failure right so we’ve successfully
built a test case so let me go ahead and execute it browser open automatically they’re also
navigated to Gmail and turd my ID and close the window what it happened really
fast so you people must not have seen everything but it’s a Staz successful
which means our titles also match but I would still like to slow the automation
down so that you people can actually see everything that happened so that is also
possible we can do it by thread dot sleep let’s say I’ll ask it to sleep for
2,000 milliseconds similarly I’ll have one more sleep
command after if you’ve clicked on the next button so this is not necessary I
mean normally testing would require you to do it really fast so that in a matter
of second maybe a thousand cases can be done or more but I’m doing this so that
you be built-in see everything that happened okay thread requires us to
throw an exception so we read that let me now go ahead and run it okay so my
browser is open and I have Gmail my ID typed in with the screen got
maximized I’m in the window and that’s it we also closed the browser and it
says test was successful so congratulations we just ran our first
selenium test case let us go back to the slide so this was the code that we just
ran and our test who is a success so that brings us to the end of our session
let us finally go over the session in a minute first we discussed why you should
be using selenium because it is free open-source works on any operating
system any web browser and lets you code in multiple programming languages then
we saw what selenium is we defined it it is just an open source tool that can
automate almost any browser then we went over the versions it has and the suite
of tools and offers and we also saw how to set selenium up the three
prerequisites for it and we finally ran a test case in selenium so that’s it for
today thank you so much and happy learning I
hope you have enjoyed listening to this video please be kind enough to like it
and you can comment any of your doubts and queries and we will reply them at
the earliest do look out for more videos in our playlist and subscribe to any
Rekha channel to learn more happy learning

47 thoughts on “How to Write & Run a Test Case in Selenium | Selenium Tutorial | Selenium Training | Edureka”

  1. Got a question on the topic? Please share it in the comment section below and our experts will answer it for you. For Selenium Training and Certification Curriculum, Visit our Website: http://bit.ly/2zoLfPY

  2. Got error as "org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed" while executing the above script can you help me on this?

  3. Hey Siddhant, I have got a doubt. While inspecting the "Sign in" element, you have used 'class' as a locator and in the code you wrote..By.classname. So here's my doubt why did you use classname and why not class. Could you please clarify the difference between By.classname and By.class. I'm not getting that part.
    Thank You!

  4. hello sir. when i execute the code after that i never get output. and show me many red lines of error at console. plzz.. help me.

  5. "import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.chrome.ChromeDriver;
    "
    the error with these lines of code is "The import org cannot be resolved". Help me resolve the import statement.

  6. I am getting error for ChromeDriver and get functions after importing the pacakges for these. How to remove errors for these?

  7. Really helpful for the beginners. Thanks. One question: r we need to import selenium jars in Java package Everytime ?

  8. Hello, when I click run, i get this error message :

    Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: C:UserserichuiDesktopSelenium setup.exe fileschromedriver

    I have made sure that the chrome driver is there in that folder. Am i missing something?

Leave a Reply

Your email address will not be published. Required fields are marked *