Sie sind auf Seite 1von 12

Basics of Test Automation with Selenium

11/17/2014

Shiva Shanmugam

TTH/British Airways - C&CSF/Web Service

s.shiva@tcs.com

Confidentiality Statement
Include the confidentiality statement within the box provided. This has to be legally
approved
Confidentiality and Non-Disclosure Notice
The information contained in this document is confidential and proprietary to TATA
Consultancy Services. This information may not be disclosed, duplicated or used for any
other purposes. The information contained in this document may not be released in
whole or in part outside TCS for any purpose without the express written permission of
TATA Consultancy Services.

Tata Code of Conduct


We, in our dealings, are self-regulated by a Code of Conduct as enshrined in the Tata
Code of Conduct. We request your support in helping us adhere to the Code in letter and
spirit. We request that any violation or potential violation of the Code by any person be
promptly brought to the notice of the Local Ethics Counselor or the Principal Ethics
Counselor or the CEO of TCS. All communication received in this regard will be treated
and kept as confidential.

Table of Content
1.

Introduction ............................................................................................................................................................... 4

2.

Selenium ................................................................................................................................................................... 4
2.1

Selenium Features............................................................................................................................................ 4

2.2

Selenium Components ..................................................................................................................................... 4

2.2.1

Selenium IDE .............................................................................................................................................. 5

2.2.2

Selenium RC ............................................................................................................................................... 6

2.2.3

Selenium Web Driver .................................................................................................................................. 7

2.2.4

Selenium Grid ........................................................................................................................................... 10

1.

Introduction

Web Browser Automation is a process where certain steps in the web browser are performed repetitively
to ensure the correct operation of the web applications functionality.
With Web Browser Automation Tools it is possible to check accessibility and performance by
periodically running some transaction scenarios for certain services. The main purpose of these tools is to
record a specific transaction scenario for a browser, play it back by injecting JavaScript into web pages and
then to provide the corresponding results.
Why and When To Automate?

Frequent regression testing

Repeated test case Execution is required

User Acceptance Tests

Faster Feedback to the developers

Reduce the Human Effort

Test same application on multiple environments

2.

Selenium

Selenium is a robust set of tools that supports rapid development of test automation for web-based
applications.
It provides a rich set of testing functions specifically geared to the needs of testing of a web application.
It operations are highly flexible, allowing many options for locating UI elements and comparing
expected test results against actual application behavior.
2.1 Selenium Features

Supports Cross Browser Testing. The Selenium tests can be run on multiple browsers.
Allows scripting in several languages like Java, C#, PHP and Python.
Assertion statements provide an efficient way of comparing expected and actual results.
Inbuilt reporting mechanism.

2.2 Selenium Components


Selenium IDE
Selenium Remote Control
Selenium Grid

2.2.1

Selenium IDE

Selenium IDE is an integrated development environment for Selenium tests.


It is implemented as a Firefox extension, and allows you to record, edit, and replay the test in firefox
Selenium IDE allows you to save tests as HTML, Java, Ruby scripts, or any other format
It allows you to automatically add assertions to all the pages.
Allows you to add selenese commands as and when required
Installation
Using Firefox, first, download the IDE from the SeleniumHQ downloads page When downloading from
Firefox, youll be presented with the Window A. (See Next Slide)
Select Install Now. The Firefox Add-ons window pops up, first showing a progress bar, and when the
download is complete, displays the Window B. (See Next Slide)
Restart Firefox. After Firefox reboots you will find the Selenium-IDE listed under the Firefox Tools
menu.

Recording a Selenium Test Case

Open Firefox that has the IDE installed

Open the base URL of the application to record.

Keep the application in a common base state.

Go To Tools a Selenium IDE and the IDE will be opened

Now perform the operations on the application as you are testing the application.

Once you are done with the recording click on the stop recording button and save the test case
through the file menu. By default it will be saved as a selenese script (HTML format)

2.2.2

Selenium RC

A solution to cross browser testing.


A server, written in Java and so available on all the platforms.
Acts as a proxy for web requests from them.
Client libraries for many popular languages.
Bundles Selenium Core and automatically loads into the browser
Architecture of Selenium RC

Selenium Server

Selenium server receives selenium commands from the test program , interprets them, and report back to
the program the results of running those tests.
The Browser receives the selenese commands from Selenium server using simple HTTP GET/POST
requests.
We can use any programming languages that can send HTTP requests to automate selenium tests in the
browser.(pearl python etc..)
Selenium Client
Also known as Client Libraries(CL) provides a programming interface(API), is a set of functions , which
runs selenese commands from the program.
The CL takes the selenese command and passes it to the selenium server for processing and also receives
the result of the command and passes back to the program.
2.2.3

Selenium Web Driver

WebDriver is a tool for automating testing web applications.


WebDriver is designed to accurately simulate the way that a user will interact with a web application.
Selenium-WebDriver makes direct calls to the browser using each browsers native support for
automation.
There are several implementations:
-

HtmlUnitDriver

FirefoxDriver

InternetExplorerDriver

Chrome Driver

OperaDriver

Commands and Operation

Fetching a Page
WebDriver navigates to a page by using get command by passsing URL. It will wait until the page
has fully loaded.
driver.get("http://www.google.com");

Interacting With the Page


Webdriver interacts with the HTML elements through WebElement.
element = driver.findElement(By.id("passwd-id"));
element = driver.findElement(By.name("passwd"));

Locating UI Elements (WebElements)


-

By id: Finds elements based on the value of the id" attribute.

By name:Finds elements based on the value of the name" attribute.

By xpath: The xpath to use

By tagName: The element's tagName

By className: Finds elements based on the value of the "class"


attribute.

By cssSelector: Finds elements via the driver's underlying W3


Selector engine. If the browser does not implement the Selector API,
a best effort is made to emulate the API. In this case, we strive for at
least CSS2 support, but offer no guarantees.

By linkText: The exact text to match against

By partialLinkText: The text to match against

User Input - Filling In Forms


WebDriver support Select class, which provides useful methods for interacting with a form.
Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText(two");
select.selectByValue(2);
select.selectByIndex(2);
Once the form is filled, we can submit it by clicking on SUBMIT button.
driver.findElement(By.name("submit")).click();

Moving Between Windows and Frames


WebDriver supports moving between named windows using the switchTo method
driver.switchTo().window("windowName");
We can pass a window handle to the switchTo().window() method
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

We can also swing from frame to frame (or into iframes):


driver.switchTo().frame("frameName");
Its possible to access subframes by separating the path with a dot,
and you can specify the frame by its index too
driver.switchTo().frame("frameName.0.child");

Popup Dialogs
There is built in support for handling popup dialog boxes.(alerts, confirms, prompts)
After youve triggered an action that opens a popup, you can access the alert with the following:
Alert alert = driver.switchTo().alert();

This will return the currently open alert object. With this object you can now accept, dismiss, read its
contents or even type into a prompt.
alert. accept();
alert. dismiss();
alert. getText();
alert. sendKeys(String keysToSend);

Navigation: History and Location


Navigate to a page by using get() or navigate().to()
driver.get ("http://www.example.com");
driver.navigate().to("http://www.example.com");
The navigate interface also exposes the ability to move backwards
and forwards in your browsers history
driver.navigate().forward();
driver.navigate().back();
Refresh the page by using navigate interface.
driver.navigate().refresh();

Handle SSL certificate warnings


SSL certificate secure connections from a web server to a browser.
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile myProfile = allProfiles.getProfile("My Profile");
myProfile.setAcceptUntrustedCertificates(true);
myProfile.setAssumeUntrustedCertificateIssuer(false);
9

Execution
Follow below steps to execute web driver code from eclipse
From selenium IDE, File -> Export Test case -> Junit 4(Web Driver)
In Eclipse, Create a Class in Test script name inside a Package
Put the Test program inside the class
Download the selenium java client jar(http://www.seleniumhq.org/download/) and add to class
path
Right click on the program and select Run As -> Junit Test
2.2.4

Selenium Grid

Grid allows to
scale by distributing tests on several machines ( parallel execution )
manage multiple environments from a central point, making it easy to run the tests against a vast
combination of browsers / OS.
minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage
virtual infrastructure for instance.
A grid consists of a single hub, and one or more nodes.Both are started using the selenium-server.jar
executable.
The Hub is the central point that will receive all the test request and distribute them to right nodes.

To start the hub

Open a command prompt and navigate to the directory where you copied the selenium-server-standalone
file.
Type the following command:
java -jar selenium-server-standalone-2.14.0.jar -role hub
The hub will automatically start-up using port 4444 by default. To change the default port, we can add
the optional parameter -port when we run the command.
We can view the status of the hub by opening a browser window and navigating to:
http://localhost:4444/grid/console
To start the node

Regardless on whether you want to run a grid with new WebDriver functionality, or a grid with
Selenium 1 RC functionality, or both at the same time, you use the same selenium-server-standalone jar file
to start the nodes.
java -jar selenium-server-standalone-2.14.0.jar -role node -hub http://localhost:4444/grid/register
Using grid to run tests
Configure RC node by

Selenium selenium = new DefaultSelenium(localhost, 4444, *firefox,


http://www.google.com);
For WebDriver nodes, use the RemoteWebDriver and the DesiredCapabilities object to define which
browser, version and platform.

10

Create the target browser capabilities to run the tests


DesiredCapabilities capability = DesiredCapabilities.firefox();
Pass that into the RemoteWebDriver object:
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
The hub will then assign the test to a matching node.
A node matches if all the requested capabilities are met. To request specific capabilities on the grid,
specify them before passing it into the WebDriver object.
capability.setBrowserName();
capability.setPlatform();
capability.setVersion()
capability.setCapability(,);
Example: A node registered with the setting:
-browser browserName=firefox,version=3.6,platform=LINUX
will be a match for: capability.setBrowserName(firefox );
capability.setPlatform(LINUX);
capability.setVersion(3.6);
and would also be a match for
capability.setBrowserName(firefox );
capability.setVersion(3.6);

11

Contact
For more information, contact gsl.cdsfiodg@tcs.com (Email Id of ISU)

About Tata Consultancy Services (TCS)


Tata Consultancy Services is an IT services, consulting and business solutions
organization that delivers real results to global business, ensuring a level of certainty no
other firm can match. TCS offers a consulting-led, integrated portfolio of IT and ITenabled infrastructure, engineering and assurance services. This is delivered through its
unique Global Network Delivery ModelTM, recognized as the benchmark of excellence in
software development. A part of the Tata Group, Indias largest industrial conglomerate,
TCS has a global footprint and is listed on the National Stock Exchange and Bombay
Stock Exchange in India.
For more information, visit us at www.tcs.com.

IT Services
Business Solutions
Consulting
All content / information present here is the exclusive property of Tata Consultancy Services Limited (TCS). The content /
information contained here is correct at the time of publishing. No material from here may be copied, modified, reproduced,
republished, uploaded, transmitted, posted or distributed in any form without prior written permission from TCS.
Unauthorized use of the content / information appearing here may violate copyright, trademark and other applicable laws,
and could result in criminal or civil penalties. Copyright 2011 Tata Consultancy Services Limited

12

Das könnte Ihnen auch gefallen