24
SELENIUM WEBDRIVER Yuriy Bezgachnyuk, SSU/ITA October, 2014

Selenium WebDriver

Embed Size (px)

DESCRIPTION

Selenium WebDriver introduction Page object pattern overview

Citation preview

Page 1: Selenium WebDriver

SELENIUM WEBDRIVER

Yuriy Bezgachnyuk, SSU/ITAOctober, 2014

Page 2: Selenium WebDriver

2

Test automationWhat is Selenium WebDriverSelenium WD FeaturesDocument Object Model (DOM)

LocatorsExample task

PageObject pattern introduction

Agenda

Page 3: Selenium WebDriver

3Test automation

In software testing, test automation is the use of special software (separate from the software being tested) to control the execution of tests and the comparison of actual outcomes to predicted outcomes.

Test Automation

Page 4: Selenium WebDriver

4What is Selenium Web Driver

Selenium WebDriver – it’s a compact Object-Oriented API for Internet Browsers control

WebDriver is the name of the key interface against which tests should be written in Java, Python, Ruby, …

What is Selenium Web Driver

Page 5: Selenium WebDriver

5Selenium WD Features

Connects to most modern browsersAllows remote control

Finds elements by selectors Modifies values of HTML elements Interacts with DOM elements Handles modal, popup windows

Selenium WD Features

Page 6: Selenium WebDriver

6DOM Introduction

The Document Object Model (DOM) is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents.

Document Object Model (DOM)

Page 7: Selenium WebDriver

7DOM [Locators]

Select any HTML element(s) from DOM by using tags name attributes of tags

• id, name, … CSS selectors XPath

DOM [Locators]

Page 8: Selenium WebDriver

8Task

A web-page with a HTML form is given [Fig. 1]

User login functionality should be tested Set value for ‘login’ field Set value for ‘password’ field Click submit button Check results

Example Task

Page 9: Selenium WebDriver

9Explanation of practical example

URL for Login formhttp://<HOME_URL>/admin/login

User inputs correct credentials and is redirected to URL:http://<HOME_URL>/adminotherwise user is redirected back to the login form page

Explanation of example

Page 10: Selenium WebDriver

10Template

JUnit 4 testing framework will be used

Implementation [Template]

Initial operations

Our main part of code will be here

Finally operations

Page 11: Selenium WebDriver

11

setUp() method provides pre-test WebDriver configuration.

setUp()

setUp() method

Object for concrete browser

Set time for waiting

Visit to page specified in

URL constant

Page 12: Selenium WebDriver

12

tearDown() method provides post-test actions (WebDriver object disposal, used resources freeing).

tearDown() method

tearDown() method

Close WebDriver and free

resources

Page 13: Selenium WebDriver

13

Page Object pattern use.Selenium WebDriver use for low-level browser API functions only!!!

Definitions of Page Object pattern: Each single web-page is represented

through a Java class User actions for each web page are

implemented as class methods

PageObject pattern [Introduction]

Task implementation

Page 14: Selenium WebDriver

14PageObject Pattern

PageObject Pattern

Page 15: Selenium WebDriver

15Task’s solution

Let investigate our code for test user logon

Task Implementation

Object for HomePage where login form is

placed

Object for ResultPage

URL from address bar of browser

Comparison existing URL address with needed address

Page 16: Selenium WebDriver

16

Class HomePage

Method for set values into HTML form and submit

data to server

Page 17: Selenium WebDriver

17Code investigation

Code sample

Page 18: Selenium WebDriver

18WD Low-level methods

findElement(By arg) – searches and returns an object representation of DOM element. Input parameter arg – some of possible selectors (using static methods of By class)

sendKeys(CharSequence arg) – sends char sequence to a HTML form element

click() – clicks on a DOM element

WebDriver Low level methods

Page 19: Selenium WebDriver

19Moving between windows and frames

Web applications may have frames or multiple windows, that need to be specifically handled. WebDriver supports moving between named windows with switchTo() method:

Windows switching should be used when application has JS modal windows (alerts)

driver.switchTo().window("windowName");

Windows handling

Page 20: Selenium WebDriver

20Moving between windows and frames

When we clicking “Вхід” button without supplying credentials an alert modal window appears [Fig.1]

In order to click ‘OK’ button WebDriver should be switched to modal window

Windows handling

Page 21: Selenium WebDriver

21Example #2 [Code]

Let’s take a look at the code

Example #2 [Code]

Switch to JS alert window and click ‘OK’

button

Page 22: Selenium WebDriver

22

“Main” class source

Page 23: Selenium WebDriver

23

http://www.w3schools.com/https://developer.mozilla.org/http://docs.seleniumhq.org/

References and Sources

Page 24: Selenium WebDriver

QUESTIONS?