Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Selenium and WebDriver

No description

Evgeny Tkachenko

on 7 September 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Selenium and WebDriver

What is Selenium?
The name comes from the Greek σελνη selene meaning "Moon"
Selenium 1, bye-bye!
Selenium occurs in three distinct forms: as a non-crystalline (gray);
it can form
as a deep
red to black
powder; and it can form as red crystals.
Selenium is a chemical element with atomic number 34, chemical symbol Se, and an atomic mass of 78.96. It is a nonmetal, whose properties are intermediate between those of adjacent chalcogen elements sulfur and tellurium.
In nature
In testing
The tests can then be run against most modern web
browsers. Selenium deploys on
Windows, Linux,
and Macintosh platforms.
Thank you!
Selenium is a portable software testing framework for web applications. Selenium provides a record/playback tool for authoring tests without learning a test scripting language (Selenium IDE). It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including C#, Java, Groovy, Perl, PHP, Python and Ruby.
В презентации использовались картинки с сайтов:
7.WebDriver is on its way to
becoming a browser standard.
What is it?
What does it do?
WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that's easy to explore and understand, which will help make your tests easier to read and maintain. It's not tied to any particular test framework, so it can be used equally well with JUnit, TestNG or from a plain old "main" method.
And then?!
And what's the difference between
WebDriver and Selenium?
The main difference is hidden in the architecture of these tools
Let's look at these differences
a bit more closely!
Here the presentation comes to an end...
5. Window
An interface for managing size and
position of window browser
Interface WebDriver
The main interface used for testing, which represents an idealised web browser.

The methods in this class fall into three categories:
- Control of the browser itself (get(), navigate(), close(), etc)

- Selection of WebElements (findElement(), findElements(), etc)

- Debugging aids (getCurrentUrl(), getPageSource(), getWindowHandles(), etc)
WebDriver has six major Interfaces :
0. ImeHandler
An interface for managing input methods
1. Navigation
Used to move between sites in the current window
2. Options
An interface for managing stuff you would do in a browser menu
3. TargetLocator
Used to locate a given frame or window
4. Timeouts
An interface for managing timeout behavior for WebDriver instances.
Let's start!
Locating UI Elements
2. Once you’ve finished filling out the form, you probably want to submit it. One way to do this is would be to find the “submit” button and click it:

Alternatively, WebDriver has the convenient "submit" method on every element:

Select multiple items


WebElement item1 = driver.findElement(By.xpath("//li[.='Item 1']"));
WebElement item2 = driver.findElement(By.xpath("//li[.='Item 2']"));

//First, configure it:
Actions builder = new Actions(driver);
.click() ;

//Then get the action:
Action selectMultiple = builder.build();

//And execute it:
Tips, Hacks & Hidden Features
WebDriver supported Javascript alerts

// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();

// Get the text of the alert or prompt

// And acknowledge the alert (equivalent to clicking "OK")
Drag And Drop
Drag an element to an offset

WebElement dragItem = driver.findElement(By.id("draggable"));
(new Actions(driver)).dragAndDropBy(dragItem, 5, 10).build().perform();
Press control+...

textBox.sendKeys(Keys.LEFT_CONTROL + "a");
textBox.sendKeys(Keys.LEFT_CONTROL + "c");
textBox.sendKeys(Keys.LEFT_CONTROL + "v");
textBox.sendKeys(Keys.LEFT_CONTROL, Keys.F5);
Sequence of actions
Proxy configuration

Proxy proxy = new Proxy();
DesiredCapabilities capabilities =
capabilities.setCapability(CapabilityType.PROXY, proxy);
Scrolls a window
It is possible to use Javascript execution abilities:

int pixel = 500;
((JavascriptExecutor) driver)
.executeScript("if (window.screen){window.scrollBy(0," + pixel + ");};");
Start Firefox with an extension installed
File file = new File("firebug-1.9.0-fx.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(firefoxProfile);

This is the most efficient and prefered way to locate an element. Common pitfalls that UI developers make is having non-unique id’s on a page or auto-generating the id, both should be avoided. A class on an html element is more appropriate than an auto-generated id.

WebElement captchaBox = driver.findElement(By.id("ConfirmMainEmailForm_captcha"));
By Name

Finds the element with matching name attribute.

WebElement captchaBox = driver.findElement(By.name("ConfirmMainEmailForm[captcha]"));
By Link Text

Find the link element with matching visible text.

WebElement signInButton =
driver.findElement(By.linkText("Sign in and register"));
By Partial Link Text

Find the link element with partial matching visible text.

WebElement signInButton =
driver.findElement(By.partialLinkText("Sign in"));

Like the name implies it is a locator strategy by css.
Please,beware that not all browsers were created equal, some css that may work in one version - may not work in another.

WebElement signInButton = driver.findElement(By.cssSelector(".bUserBar .LikeRealBtn"));

At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible.

List<WebElement> radioButtons =
List<WebElement> radioButtonsVisible = driver.findElements(By.xpath("//input[@class='bTicketPaper__eRadioButton' and not(@disabled='disabled')]"));
Let's solve the problem:
Find all radio buttons circled in the picture
The same result we get if we use as a locator class name.

By Class Name

“Class” in this case refers to the attribute on the DOM element. Often in practicle use there are many DOM elements with the same class name, thus finding multiple elements becomes the more pratical option over finding the first element.

List<WebElement> radioButtons =
I almost forgot ...
By Tag Name

The DOM Tag Name of the element.

Let's resolve the issue: How do We get the entire text of the page?
WebElement bodyOfPage = driver.findElement(By.tagName("body"));
String bodyText = bodyOfPage.getText();
0. Locating UI Elements
Each of the language bindings expose a “Find Element” and “Find Elements” method.

The “Find” methods take a locator or query object called “By”. “By” strategies are listed further.
1. Typing into an element
If you want to enter some text into a text field, then you can use:

element.sendKeys("some text");

A side-effect of this is that typing something into a text field won’t automatically clear it.
Instead, what you type will be appended to what’s already there. You can easily clear the contents of a text field or textarea:

Work with a slider

WebElement dragItem =
driver.findElement(By.xpath("//a[@class='ui-slider-handle ui-state-default ui-corner-all']"));
(new Actions(driver)).dragAndDropBy(dragItem, 10, 0).build().perform();
Drag an element to drop on to another element

WebElement fromItem1 = driver.findElement(By.id("draggable"));
WebElement toItem2 = driver.findElement(By.id("droppable"));
(new Actions(driver)).dragAndDrop(fromItem1, toItem2).build().perform();
Executing JavaSript

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("alert('Selenium1 bye-bye')");
1. Selenium works through proxy using Javascript.
1. WebDriver works natively with Browser
(Native automation faster and a little less
prone to error and browser configuration).

2. Selenium supports major browsers.
(IE, Firefox, Chrome, Safari, Opera)
2. WebDriver supports IE, Firefox, Opera,
Chrome, and also Android and IPhone Drivers.
3. Selenium API is quite complicated
3. Great Simple API.
4. Selenium supports multiple
languages to write the test.
(C#, Java, Groovy, Perl, PHP,
Python and Ruby)
4. Perl and PHP are not supported in WebDriver
(December 2011).
A pure JS API support is also in the planning stage.
5. Selenium-RC Server's start-up
is required.

5. Does not Requires Selenium-RC Server
to be running.
6. Access to headless HTMLUnit
can allow really fast tests.
Bugs are found by tests, not by automation
Presentation Agenda
0. What is Selenium?
1. Comparison:
Selenium RC vs WebDriver
2. Interface WebDriver.
Tips, Hacks & Hidden Features
3. Migrating from
Selenium RC to WebDriver
The most important Commands
and Operations...
Also, you can simulate pressing the arrow keys by using the “Keys” class:

element.sendKeys("s", Keys.ARROW_DOWN);

It is possible to call sendKeys on any element, which makes it possible to test keyboard shortcuts such as those used in GMail.
WebElement loginElement = driver.findElement(By.name("AuthForm[loginOrEmail]"));

driver.findElement(By.xpath("//input[contains(@value,'Sign in')]")).click();
Evgeny Tkachenko
Senior Software Automation Testing Engineer
Evgeny Tkachenko
Senior Software Automation Testing Engineer
our blog:
How to migrate to WebDriver ?
Just "to Cut off"
//Get the screen size
java.awt.Dimension screenSize =

//Define the desired size
Dimension maxWindowSize =
new Dimension(screenSize.width -4, screenSize.height-4);

//Set the desired size
WebDriver.Window window = driver.manage().window();
WebDriver. Hidden Features.
Maximize the window
Just start to use WebDriver API.
But you need to rework all your methods in one massive push...
Selenium 1, bye-bye!
WebDriver. Hidden Features.
Use WebDriver-Backed Selenium-RC...
The Java version of WebDriver provides an implementation of the Selenium-RC API.
This means that you can use the underlying WebDriver technology using the Selenium-RC API.
File profileDir = new File("SeleniumProf");
FirefoxProfile firefoxProfile = new FirefoxProfile(profileDir);
WebDriver driver = new FirefoxDriver(firefoxProfile);
WebDriver’s API is more Object Oriented than the original Selenium RC API.
Setting the path to Firefox
You have write this line before opening the browser
WebDriver (Selenium 2.0) is the merging of the Selenium and WebDriver projects.
Taking a Screenshot
//Start Selenium Server with Java Code
RemoteControlConfiguration rcc = new RemoteControlConfiguration();
SeleniumServer seleniumServer = new SeleniumServer(rcc);
if (!seleniumServer.getServer().isStarted())

// customize your capabilities here
DesiredCapabilities capabilities = DesiredCapabilities.firefox();

// RemoteWebDriver does not implement the TakesScreenshot class
// but We can create MyRemoteWebDriver by extending RemoteWebDriver
// and implementing TakesScreenshot interface.
driver = new MyRemoteWebDriver(new URL("http://machineIP:port/wd/hub"),capabilities);
class MyRemoteWebDriver extends RemoteWebDriver implements TakesScreenshot {


public MyRemoteWebDriver(URL remoteAddress,
Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);

public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
if ((Boolean) getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT)) {
String base64Str = execute(DriverCommand.SCREENSHOT).getValue().toString();
return target.convertFromBase64Png(base64Str);
return null;

Full transcript