Top Banner
Be#er Page Object Handling with Loadable Component Pa#ern Sargis Sargsyan
49

Better Page Object Handling with Loadable Component Pattern

Jan 15, 2017

Download

Technology

Sargis Sargsyan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Better Page Object Handling with Loadable Component Pattern

Be#er Page Object Handling with Loadable Component Pa#ern

Sargis Sargsyan

Page 2: Better Page Object Handling with Loadable Component Pattern

‹ ›2

Loadable Component

4

Introduction

1

Page Object Pattern

2

Wait in Selenium

3

Slow Loadable Component

5

Common Failures

6

Q&A

7

Main Topics

Page 3: Better Page Object Handling with Loadable Component Pattern

‹ ›3

Demos

• Demos are in Java!

• Everything we discuss is applicable strongly-typed languages (e.g. C#)

• Most things are applicable to dynamically-

typed languages (e.g. Ruby, Python, Perl)

Page 4: Better Page Object Handling with Loadable Component Pattern

‹ ›4

Demos will use also

• Maven

• TestNG

• Selenium

Page 5: Better Page Object Handling with Loadable Component Pattern

Page Object Pattern

`

Page 6: Better Page Object Handling with Loadable Component Pattern

‹ ›6

What is Page Object Pattern

• Page Objects Framework is a design pattern which has become

popular in test automation for making easy test maintenance and

reducing code duplication. This design pattern, to interact or work

with a web page, we have an object-oriented class for that web. Then

the tests calls the methods of this page class by creating a page

object whenever they need to interact or work with that web page.

7

q 1Z

a

Page 7: Better Page Object Handling with Loadable Component Pattern

‹ ›7

What is Page Object Pattern

• For example web application or website that has multiple web pages

and each page offers different services and functionalities.

• There are different pages like

• Home page,

• Login page,

• Registration page.

• Each page offers a specific set of services. Services offered by

Login page Login by entering user name and password We can

get page title A class to represent Login page is like

Page 8: Better Page Object Handling with Loadable Component Pattern

‹ ›8

What is Page Object Pattern

• In page objects framework:

• Each page in the web application/website we consider as an

object.

• Each webpage in the web application is represented by a Class

• Each service/functionality offered by a webpage is represented

by a method in the respective page class

We will be having our tests calling these methods by creating objects

of page classes

Page 9: Better Page Object Handling with Loadable Component Pattern

‹ ›9

Why we should use Page Objects Pattern

1 Promotes reuse and refuses duplication

2 Makes tests more readable and robust

3 Makes tests less brittle

4 Improves maintainability, Particularly when there is frequent changes

5 If UI change, tests don’t need to be change, only the code within the page object need to be changed.

6 Handle of each page using its instance

Page 10: Better Page Object Handling with Loadable Component Pattern

‹ ›10

How does it structured?

• Each page is defined as it’s own class.

• Actions (including navigation) are represented as functions for a class.

• Each function can return a new Page object (navigating between

pages),

• Tests only talk to the page objects.

• Page objects only talk to the Base Object.

• Base Object talks to only driver.

• Elements on the page are stored as variables for the page object

Page 11: Better Page Object Handling with Loadable Component Pattern

‹ ›11

Selenium Model

Test Case 1

Test Case 2

Test Case …

ReportSelenium

Web app

Web PageWeb PageWeb PageWeb PageBrowsers

Page 12: Better Page Object Handling with Loadable Component Pattern

‹ ›12

Page Object Model

Page Objects

Test Case 1

Test Case 2

Test Case …

Report

Selenium

Web app

Web PageWeb PageWeb PageWeb PageBrowsers

Page 13: Better Page Object Handling with Loadable Component Pattern

‹ ›13

Page Object Model & Base Page

Page Objects

Test Case 1

Test Case 2

Test Case …

Report

Base Page

Selenium

Web app

Web PageWeb PageWeb PageWeb PageBrowsers

Page 14: Better Page Object Handling with Loadable Component Pattern

‹ ›14

Architecture

Framework/ PageObjectsL

Browser/Web Application

Ħ

Selenium Tests X

Selenium Web Driver

Ä

Page 15: Better Page Object Handling with Loadable Component Pattern

‹ ›15

How it looks like

Page 16: Better Page Object Handling with Loadable Component Pattern

‹ ›16

Base Object Page

Page 17: Better Page Object Handling with Loadable Component Pattern

‹ ›17

Base Object Page

Page 18: Better Page Object Handling with Loadable Component Pattern

‹ ›18

Selenium Setup

Page 19: Better Page Object Handling with Loadable Component Pattern

‹ ›19

Base Test

Page 20: Better Page Object Handling with Loadable Component Pattern

‹ ›20

Test

Page 21: Better Page Object Handling with Loadable Component Pattern

Wait in Selenium

p

Page 22: Better Page Object Handling with Loadable Component Pattern

‹ ›22

Why to wait?

• When automating an application you must wait for a transaction to

complete before proceeding to your next action.

• Sleeps should never be a version for an alternative to waits. Sleeps

will ALWAYS wait the exact amount of time even if the application is

ready to continue the test.

Page 23: Better Page Object Handling with Loadable Component Pattern

‹ ›23

Wait as long as necessary

• We should wait as long as necessary which saves you precious time

on your execution.

• Selenium Offers 3 wait types:

• Implicit Waits

• Explicit Waits

• Fluent Waits[

Page 24: Better Page Object Handling with Loadable Component Pattern

‹ ›24

Implicit Waits

• Implicit waits apply globally to every find element call.

• undocumented and practically undefined behavior

• Runs in the remote part of selenium (the part controlling the browser).

• Only works on find element(s) methods.

• Returns either element found or (after timeout) not found.

• If checking for absence of element must always wait until timeout.

• Cannot be customized other than global timeout.

• Best practice is to not use implicit waits if possible.

Page 25: Better Page Object Handling with Loadable Component Pattern

‹ ›25

Explicit Waits

• Documented and defined behavior

• Explicit waits ping the application every 500ms checking for the

condition of the wait to be true

• Runs in the local part of selenium (in the language of your code)

• Works on any condition you can think of

• Returns either success or timeout error

• Can define absence of element as success condition

• Can customize delay between retries and exceptions to ignore

Page 26: Better Page Object Handling with Loadable Component Pattern

‹ ›26

Explicit Waits

Page 27: Better Page Object Handling with Loadable Component Pattern

‹ ›27

Fluent Waits

• Fluent waits require that you define the wait between checks of the

application for an object/condition as well as the overall timeout of

the transaction. Additionally you must tell fluent waits not to throw an

exception when they don’t find an object as best practice.

Page 28: Better Page Object Handling with Loadable Component Pattern

Loadable Component

p

Page 29: Better Page Object Handling with Loadable Component Pattern

‹ ›29

What is the LoadableComponent?

• The LoadableComponent is a base class that aims to make writing

PageObjects less painful. It does this by providing a standard way of

ensuring that pages are loaded and providing hooks to make

debugging the failure of a page to load easier. You can use it to help

reduce the amount of boilerplate code in your tests, which in turn

make maintaining your tests less tiresome.

• There is currently an implementation in Java that ships as part of

Selenium 2, but the approach used is simple enough to be

implemented in any language.

Page 30: Better Page Object Handling with Loadable Component Pattern

‹ ›30

What is it?

• LoadableComponent is a base class in Selenium, which means that

you can simply define your Page Objects as an extension of the

LoadableComponent class. So, for example, we can simply define a

LoginPage object as follows:

Page 31: Better Page Object Handling with Loadable Component Pattern

‹ ›31

How to use

• The Loadable Component Pattern also allows you to model your

page objects as a tree of nested components.

• Allows better way to manage navigations between pages

• Uses the “load” method that is used to navigate to the page and the

“isLoaded” method which is used to determine if we are on the right

page.

Page 32: Better Page Object Handling with Loadable Component Pattern

‹ ›32

LoadableComponent class

Page 33: Better Page Object Handling with Loadable Component Pattern

‹ ›33

load() and inLoaded() methods

Page 34: Better Page Object Handling with Loadable Component Pattern

‹ ›34

PageLoadHelper Class

Page 35: Better Page Object Handling with Loadable Component Pattern

‹ ›35

isLoaded() with PageLoadHelper

Page 36: Better Page Object Handling with Loadable Component Pattern

‹ ›36

Extend LoadableComponent in Base Object class

Page 37: Better Page Object Handling with Loadable Component Pattern

‹ ›37

Custom Loadable Component Implementation

Page 38: Better Page Object Handling with Loadable Component Pattern

Slow Loadable

Component

`

Page 39: Better Page Object Handling with Loadable Component Pattern

‹ ›39

What is the SlowLoadableComponent?

• The SlowLoadableComponent is a sub class of LoadableComponent.

• get() for SlowLoadableComponent will ensure that the component is

currently loaded.Ensure that the component is currently loaded.

• isError() method will check for well known error cases, which would

mean that loading has finished, but an error condition was seen.

• waitFor() method will wait to run the next time.

After a call to load(), the isLoaded() method will continue to fail until the

component has fully loaded.

Page 40: Better Page Object Handling with Loadable Component Pattern

‹ ›40

SlowLoadableComponent class

Page 41: Better Page Object Handling with Loadable Component Pattern

‹ ›41

SlowLoadableComponent Implementation

Page 42: Better Page Object Handling with Loadable Component Pattern

Common Failures

l

Page 43: Better Page Object Handling with Loadable Component Pattern

‹ ›43

Recorded Brittle Test

Page 44: Better Page Object Handling with Loadable Component Pattern

‹ ›44

Not Building a Framework

Page 45: Better Page Object Handling with Loadable Component Pattern

‹ ›45

Use unique selectors

Page 46: Better Page Object Handling with Loadable Component Pattern

‹ ›46

Do not use Retry

y

Page 47: Better Page Object Handling with Loadable Component Pattern

‹ ›47

Do not try to Automate Hard Things

Page 48: Better Page Object Handling with Loadable Component Pattern

‹ ›48

Run test in Continuous Integration

1 Have a plan and stick to it

2 Run test as part of build

3 Run test locally

4 Report results

5 Break builds

Page 49: Better Page Object Handling with Loadable Component Pattern

✉ !

EMAIL TWITTER LINKEDIN

[email protected] mrsargsyan sargissargsyan

Contacts

ą

Thank You!