Selenium Basics
-
Upload
satish-tirumani -
Category
Documents
-
view
118 -
download
7
Transcript of Selenium Basics
Selenium Basics
What is covered in this Session?
IntroductionWhere to get Selenium?Selenium Vs QTP,SilktestLimitationsFeatures of SeleniumComponentsSelenium COREConfiguring Selenium CORESelenium IDESelenium IDE-Sample
ScriptRecording and Executing a
Script
Selenium RC(Remote Control)
Starting Selenium ServerDefault Selenium ClassLocatorsSample ScriptSelenium Command
ReferenceSelenium RC Configuration
with EclipseObject IdentificationSelenium Grid
Selenium is set of different software tools each with a
different approach to supporting test automation.
Selenium is an open source tool.
Selenium supports as many as 8 programming
languages such as HTML , Ruby , PHP, Python, C#,
Java, Perl etc.
Latest side of Selenium is Grid, which allows the
running of multiple Selenium tests on different
browsers and different platforms.
Introduction
We can everything from the Selenium site
www.seleniumhq.org
Software's:
All the selenium software's can be downloaded by using the
below link
http://seleniumhq.org/download/
Documentation:
Documentation can be downloaded by using the below link
http://seleniumhq.org/docs/
Where to get Selenium??
Object Identification: Faster: Selenium can identify objects using xpath, So it can
directly go to a specific object Slow: Silk test and QTP follows a hierarchy for locating objects.
Scripting Language: QTP allow users to write script in VB script only. Silk test has proprietary scripting language 4TL. ( 4 Test
Language ) But Selenium allows writing script in Java, Ruby, Perl, PHP, C#
and Python. Price :
Selenium is an open source tool. Silk and QTP are not free tools
QTP license cost is around $7500 , Silk test license cost is around $8500 USD
Selenium Vs QTP & Silk
Desktop applications:
We can’t automate desktop applications using Selenium.
We can automate using QTP and Silk test.
Object Repository:
Capturing objects properties is a tough task as we need to
prepare the object repository.
Limitations
It has support for multiple operating systems like Windows, Linux and
Mac
Cross browser testing can be done easily on browsers like IE, Firefox,
Safari, Netscape etc.
It supports multiple programming Languages like C#, Java, Ruby,
Python .. etc
It can be used for automating Ajax and swift web pages.
It is easy to use, if are familiar with any one of supported programming
language
It is easy to learn
It is free-open source tool
It has matured community forums to get clarifications
Features of Selenium
Selenium is majorly divided into four components
Selenium CORE
Selenium IDE
Selenium RC(Remote Control)
Selenium Grid
Components
Selenium Core is a test tool for web applications.
Selenium Core tests run directly in a browser.
And they run in Internet Explorer, Mozilla Firefox on Windows, Linux, and Macintosh.
This is deprecated with the advent of Selenium IDE and Selenium RC.
Initially IDE has capability for recording test cases only.
In earlier stages executed recorded script using Test runner.
Later IDE introduced with the execution facility.
Disadvantages:
We cannot record scripts using this.
We can run scripts in only HTML format using Test runner.html
Selenium CORE
Download the Selenium Core zip file (selenium-core-
1.0.1.zip) and extract the files into a folder.
Open Command Prompt and then navigate to the path
where the files are extracted
Configuring Selenium CORE
Configuring Selenium CORE(Contd)
A new “Selenium Functional Test Runner” window will be opened.
We need to enter the test suite path in “Test Suite” edit box on the left hand side .
Click on “Go” and then the screen window changes with the commands.
Configuring Selenium CORE (Contd)
Configuring Selenium CORE (Contd)
Now click on “Run” option on the right hand side of the window
so that the test case executes successfully.
IDE means integrated development environment, which is used for record and run the scripts.
Selenium IDE is add-on for “Mozilla Firefox”.
The recorded scripts in Selenium IDE can be viewed in the following supported languages:
1) HTML 2) Java 3) C# 4) PERL 5) Python 6) Ruby 7) PHP
The recorded scripts in Selenium IDE can be run against other browsers like IE, Safari,
Google, Chrome and Opera by using Selenium RC.
By default the script that is recorded should be saved with the .html extension.
It will keep the user actions as they are performed and store them as a reusable script to play
back.
It supports only for Firefox.
Using Selenium IDE we can record, save, execute and debug the scripts
It can’t support for Database verifications Error handling and Customize reporting.
Selenium IDE
The script is saved as .html file so the run options are active
Observations:
Table is active here because the extension is .html
Run Options are active and we can run the test case
now.
Selenium IDE
Selenium IDE-Sample Recorded Script
These are the statements
generated during record from
IDE
Observations:
Table is not available because the format of the
script is changed to Java.
Run options are graded out because the default
format for Selenium IDE is HTML.
Selenium IDE-Java Format
Selenium IDE (Contd)
Open Firefox and application you want to test Launch Selenium IDE using tools-Selenium IDE By default, you should be in the recording mode, but confirm it by observing the Red button. By default it will be in the HTML format. Otherwise, go to Options-Format-Select HTML Format. Record some actions and make sure that these are coming on Selenium IDE. During recording if you right click on any element it will show all the selenium commands
available. You can also edit existing command, by selecting it and editing on the boxes available. You can also insert/delete commands by choosing appropriate option after right clicking. Choose appropriate run option - i.e walk, run or test runner and review your results. The recorded script can be viewed in two options: 1) Table 2) Source In Table, we can see the script in tabular format. In Source, we can see the script in html code. If we want to see the recorded script in other selenium supported languages, Go to Options,
thenselect Format select the desired language.
Drawback Biggest drawback of Selenium IDE is its limitation in terms of the browser support (only Mozilla
Firefox).
Recording and Executing a Test
Selenium RC comes in two parts i.e. Server and Client libraries
Selenium-RC software comes with seven folders and we need to use
these files to configure RC with the language chosen. 1) selenium-server-1.0.3 -Configuration files for launching the RC server
2) selenium-java-client-driver-1.0.1- Java supported RC files
3) selenium-perl-client-driver-1.0.1-Perl supported RC files
4) selenium-php-client-driver-1.0.1-PHP supported RC files
5) selenium-python-client-driver-1.0.1-Python supported RC files
6) selenium-ruby-client-driver-1.0.1-Ruby supported RC files
7) selenium-dotnet-client-driver-1.0.1-C# supported RC files
Selenium RC uses a server called “Jetty Server” for launching
browsers
Selenium RC
Selenium RC is an Open Source platform and supports only web-
applications for automation.
We can develop scripts using any popular programming
languages
Using Selenium RC with a high-level programming language
allows us to query database, email test results and use iteration
statements to navigate through the result set
Using RC we can execute scripts in different browsers and
platforms
Selenium RC (Contd)
Before starting any tests you must start the server.
Go to the directory where Selenium-RC’s server is located
and run the following from a command-line console. Command to start server is
java -jar selenium-server.jar
Command to stop selenium server http://localhost:4444/selenium-server/driver/?cmd=shutDown
Starting Selenium Server
Starting Selenium Server(Contd)
We can do this in our test code using the SeleniumServer class We need to follow below steps
Create an object for SeleniumServer class
Use “Start” method for starting server.
User “Stop” method for stopping server. Sample code for Starting and Stopping selenium server.
SeleniumServer server = new SeleniumServer(); server.start(); ….. server.stop();
With the server running, we may now use a client also.Selenium selenium = new DefaultSelenium( String seleniumServerHost,
int seleniumServerPort, String browserType, String baseURL);
selenium.open("http://www.somesite.com/somePage.html"); selenium.stop();
Starting Selenium Server(Contd)
Default Selenium is class for creating browser reference
Variables used in the Default Selenium constructor are:
seleniumServerHost is the where the Selenium Server is running. i.e. hostname
seleniumServerPort is the port on which the Selenium Server is listening. The default port
is 4444.
browserType is the type of browser you want to use for testing. Common browser types are *firefox, *iexplore, *opera and etc.
baseURL is the base URL for the site you are testing.
Now, just opening a page isn’t all that useful. We need to interact with the page. To
do this, we use our selenium client’s methods with locators. For example:
selnium.click("link=Text For Some Link");
The string “link=Text For Some Link” is a locator.
Default Selenium Class
In Selenium, the objects are called as locators and they can be identified using Xpath, DOM or CSS.
Locators take the form of
“locatorType=argument”
A locator type can be an element id, an element name, an xpath expression, link text, and more.
A few examples:
selenium.click(“id=idOfThing”); //an id locator
selenium.click(“name=nameOfThing”); //a name locator
selenium.click(“xpath=//img[@alt='The image alt text']”); //an xpath locators
slenium.click(“dom=document.images[56]” ); //a DOM locator
selenium.click(“link=Test Page For Selenium”); //a link locator
selenium.click(“css=span#firstChild”); //a css locator
Locators will be covered in detail in the next session on “Objects Construction”.
Locators
import com.thoughtworks.selenium.*; // This is a package need to be include for using Selenium APIpublic class SampleScript // This is Class name {
public static void main( String args[] ) throws Exception // This is main function{
// This is the statement for Creating Browser ObjectDefaultSelenium Brw= new DefaultSelenium ("localhost",4444,"*chrome", "http://"); Brw.start(); // This statement starts the BrowserBrw.open("www.google.com"); // This statement loads google.com url Brw.waitForPageToLoad("30000"); // This stmt waits till loading complete web pageBrw.windowMaximize(); // This stmt maximizes browser windowBrw.type("//input[@name='q']", "valuelabs"); // This stmt enters Valuelabs text in search fieldBrw.click("//input[@name='btnG']"); // This stmt clicks on google search button
}}
//input[@name='q‘] – Object Identifier for Search Field //input[@name='btnG'] – Object locator for Search Btn
Sample Script
Below are usefull commands in selenium open - opens a page using a URL captureScreenshot - Captures a PNG screenshot to the specified file Check - Check a toggle-button (checkbox/radio) Click - Clicks on a link, button, checkbox or radio button Close – Clicks on the "close" button in the titlebar of a popup window or tab doubleClick - Double clicks on a link, button, checkbox or radio button getEval - Gets the result of evaluating the specified JavaScript code getHtmlSource - Returns the entire HTML source getLocation - Gets the absolute URL of the current page getText - Gets the text of an element getTitle - Gets the title of the current page getValue - Gets the value of an input field goBack - clicks on the "back" button on their browser isChecked - Gets whether a toggle-button (checkbox/radio) is checked isElementPresent - Verifies that the specified element is somewhere on the page isTextPresent - Determines if the specified element is visible Refresh – Clicks on the "Refresh" button on their browser
Selenium Command Reference
Select - Select an option from a drop-down selectFrame - Selects a frame within the current window selectWindow - Selects a popup window Start - Launches the browser with a new Selenium session Stop - Ends the test session, killing the browser Type - Sets the value of an input field Uncheck - Uncheck a toggle-button (checkbox/radio) windowMaximize - Resize currently selected window to
take up the entire screen
Selenium Command Reference (Contd)
Selenium RC with Eclipse Configuration
Prerequisites: If we want to configure Selenium RC with
Eclipse, following files are required Eclipse JDK 1.5 or Later versions. Selenium JAR files a) selenium-server.jar b) selenium-java-client-driver.jar c) junit.jar d) Poi related jar files (For handling excel)
Configuration
Open the Eclipse by double clicking on eclipse.exe, Click on OK for workspace launcher. Click on
workbench, it will allow you to move inside the eclipse.
Click on File->New->Select Java Project. Enter the Project name and then click on
“Configure JRE’s”
Configuration (Contd)
Click on “Add” and give the path upto the jdk directory so that all the JRE files will be added and
click on Finish.
Configuration (Contd)
Now the JRE is successfully configured with our project root directory
Right click on JRE System Library-> Build Path->Configure Build Path
Configuration (Contd)
Click on “Add External JAR’s” and then select the four jar files and add them to our project.
Configuration (Contd)
Now the jar files are also successfully configured with our project.• Click on Run from the Menu--> External Tools-->Select External Tools Configuration--> Double Clickon Program and then enter the following details as shown in the screen.• Click on Apply and then click on “Run”.
Configuration (Contd)
Now the Selenium RC is successfully configured with Eclipse and you are ready to write the scripts and execute them successfully.
On successful running of Selenium server, the eclipse IDE gives an message that the server started successfully on port number 4444(default)
Configuration(Contd)
Objects Identification
For identifying the properties in Selenium, we use the below tools:
• Debug bar for Internet Explorer.• Firebug for Mozilla Firefox.Debug bar Installation:• Download the debug bar software by using the
below link:http://www.debugbar.com/• Click on the exe file and then install the
software.• Once the software is installed, you will find the
debug bar on the left side of the window.
Debug bar
Drag the circle and place it on the object which we want to identify the object.
Debug bar
The properties will be identified in the Attrs field. Using these fields, we can identify the objects.
Firebug
Firebug Installation: Download the debug bar software by using
the below link:https://addons.mozilla.org/en-US/firefox/addon/1843/
Click on the exe file and then install the software.
Once the software is installed, you will find the firebug on the bottom of the window.
Firebug(Contd)
Once we select the object, automatically the objects properties will be displayed in the bottom:
Firebug(Contd)
Selenium Grid is used for launching the multiple browsers with supported operating system in parallel.
We can run the scripts on different browsers in parallel. Selenium Grid is a tool that dramatically speeds up
functional testing of web-apps. It allows you to easily run multiple tests in parallel, on
multiple machines, in a heterogeneous environment. Selenium Grid consists of a component called “Selenium
Hub” which is the unique entry point for all the tests running by using Selenium Grid.
Selenium Grid
Heartbeat Mechanism
To ease the maintenance of a Selenium-Grid deployment:
Selenium Grid Hub sends periodic heartbeat messages to all registered Remote Controls to make sure that they are still up and running.
Selenium Grid Remote Controls send periodic heartbeat messages to the Hub to check that it still considers them as registered.
Selenium Grid-Setup
Questions
Mail me @ [email protected]