Vous êtes sur la page 1sur 30
Presented At SQuAD Colorado Meetup at Denver, Feb 09, 2016 Browser Automation with Selenium WebDriver
Presented At SQuAD Colorado Meetup at Denver, Feb 09, 2016 Browser Automation with Selenium WebDriver

Presented At SQuAD Colorado Meetup at Denver, Feb 09, 2016

Browser Automation with Selenium WebDriver and Page Objects

Harsh Murari

with Selenium WebDriver and Page Objects Harsh Murari harsh@rhoynar.com Software Architect Rhoynar Software
with Selenium WebDriver and Page Objects Harsh Murari harsh@rhoynar.com Software Architect Rhoynar Software

harsh@rhoynar.com Software Architect Rhoynar Software Consulting, Boulder, CO www.rhoynar.com

About Me

Co-founder and Software Architect at Rhoynar Software Consulting

Co-founder and Principal Instructor at DestinationJ Software Technologies

Working on Build, Infrastructure and Process Improvement Solutions

12+ years of Software Engineering Experience

• Working on Build, Infrastructure and Process Improvement Solutions • 12+ years of Software Engineering Experience
• Working on Build, Infrastructure and Process Improvement Solutions • 12+ years of Software Engineering Experience
• Working on Build, Infrastructure and Process Improvement Solutions • 12+ years of Software Engineering Experience
• Working on Build, Infrastructure and Process Improvement Solutions • 12+ years of Software Engineering Experience
About This Presentation: Objectives • Understand Web Automation Testing Landscape • Overview of Selenium WebDriver

About This Presentation: Objectives

Understand Web Automation Testing Landscape

Overview of Selenium WebDriver

Moving from Manual Test to Selenium WebDriver

Common Problems with Test Automation

Understand Page Object Models Design Pattern

Test Automation Best Practices

Page Factories

Test Auto-Generation

Demo of Rhoynar’s AutoTestR Solution for Automatic Test Generation

Automation Testing Problems • Automation Tests are hard to write – large initial investment •

Automation Testing Problems

Automation Tests are hard to write – large initial investment

Automation Tests are broken most of the time – large upkeep costs

Changes to UI break tests – not sure if tests are broken or UI is broken

Unsatisfactory Automation Quality – poorly written tests

Scripts take too long to run

Scripts can’t run unattended

Reports are difficult to analyze

Tests have no documentation

Manual Test Example – CRM Application

Manual Test Example – CRM Application

Manual Test Example – CRM Application

Manual Test Example – CRM Application

Requirements

Add a New Lead

Import Leads

View Leads

View Lead Details

Contact A Lead

Contact Multiple Leads

Update a Lead

Delete a Lead

Tests

Add a New Lead

Create a Valid Lead

Create an Invalid Lead

Create a Duplicate Lead

Import Leads

Valid Import

Import with Duplicates

Import with esoteric fields

Support for different files like .csv, .xls, .xlsx etc.

• Import with esoteric fields • Support for different files like .csv, .xls, .xlsx etc. •
• Import with esoteric fields • Support for different files like .csv, .xls, .xlsx etc. •
• Import with esoteric fields • Support for different files like .csv, .xls, .xlsx etc. •
• Import with esoteric fields • Support for different files like .csv, .xls, .xlsx etc. •

Manual Test Example – CRM Application

Testing once – fine

Efficient – no upfront investment

Allows for exploratory testing

Allows to look at general performance issues

Testing multiple times – ???

4hrs * 8 Modules = 4days

• Testing multiple times – ??? 4hrs * 8 Modules = 4days AUTOMATION • Testing at

AUTOMATION

Testing at every major release?

Testing at every minor release?

Testing at every bug-fix?

Testing at every pre-flight?

every major release? • Testing at every minor release? • Testing at every bug-fix? • Testing
every major release? • Testing at every minor release? • Testing at every bug-fix? • Testing
every major release? • Testing at every minor release? • Testing at every bug-fix? • Testing

Selenium WebDriver Automation

What does Selenium WebDriver Provide?

Framework for Browser Automation

StandardsCompliant,Open Source, Free-to-use

All Major Browsers Support

All Major Operating Systems Support

All Major Languages Support

Remote Invocation Support

Mobile Platforms Support

Cross-browser compatible scripts

Object oriented

• Remote Invocation Support • Mobile Platforms Support • Cross-browser compatible scripts • Object oriented
• Remote Invocation Support • Mobile Platforms Support • Cross-browser compatible scripts • Object oriented
• Remote Invocation Support • Mobile Platforms Support • Cross-browser compatible scripts • Object oriented
• Remote Invocation Support • Mobile Platforms Support • Cross-browser compatible scripts • Object oriented

Selenium WebDriver: How does it work

Selenium WebDriver: How does it work • Lead Creation • Get a handle to the browser/window/tab

Lead Creation

Get a handle to the browser/window/tab

Load the webpage

Click on Leads Link

Click on Create Leads

Fill in the information (Text Boxes, Dropdowns,Check-Boxes etc.)

Hit Save Button

Selenium WebDriver: How does it work

Selenium WebDriver: How does it work
Selenium WebDriver: How does it work
So What’s Wrong with this example • Procedural/Repetitive • Hard -Coded, no data providers •

So What’s Wrong with this example

Procedural/Repetitive

Hard-Coded, no data providers

Prone to break:

New fields are added

Xpath Changes

Field Name/Property name changes

CSS properties change

Not re-entrant

Most times we are fixing tests to keep up with code…

• Not re-entrant • Most times we are fixing tests to keep up with code… Need

Need for Abstraction on Top of WebDriver

Need For Abstraction – Page Objects

Need For Abstraction – Page Objects

Need For Abstraction – Page Objects
Need For Abstraction – Page Objects • Pages are people too, my friend! • Every

Need For Abstraction – Page Objects

Need For Abstraction – Page Objects • Pages are people too, my friend! • Every page

Pages are people too, my friend!

Every page has feelings and emotions attributes and actions associated with it

How these are implemented is abstracted

What’s exported are the actions this webpage can perform

How these are implemented is abstracted • What’s exported are the actions this webpage can perform

PAGE OBJECT MODEL

Need For Abstraction – Page Objects

Test

Code

• Test/

Business

Logic

Separate Business Logic from Browser Handling

Each Page has an Object

Exported methods: Page’s Functionality

Abstracted methods: Browser, UI, WebElement handling

• Abstracted methods: Browser, UI, WebElement handling Page Objects • Driver/ Config/ UI Logic B r

Page

Objects

methods: Browser, UI, WebElement handling Page Objects • Driver/ Config/ UI Logic B r o w

• Driver/

Config/

UI Logic

Browser

• Browser

Browser, UI, WebElement handling Page Objects • Driver/ Config/ UI Logic B r o w s
Browser, UI, WebElement handling Page Objects • Driver/ Config/ UI Logic B r o w s
Browser, UI, WebElement handling Page Objects • Driver/ Config/ UI Logic B r o w s
Browser, UI, WebElement handling Page Objects • Driver/ Config/ UI Logic B r o w s
Browser, UI, WebElement handling Page Objects • Driver/ Config/ UI Logic B r o w s
Page Objects - Example

Page Objects - Example

Page Objects - Example
Page Objects - Example
Page Objects - Example

Page Objects – Few Things to Note

AttributesActions
AttributesActions

Notes:

Annotations: Object is assumed to be initialized through PageFactory.

@FindBy can support: id, name, tagName, linkText, partialLinkText, css, xpath

@CacheLookup annotation: Object is assumed to never change in the DOM.

All buttons/actions are usually translated into functions.

Functions use data providers to provide configurable, re-entrant data.

are usually translated into functions. • Functions use data providers to provide configurable, re-entrant data.
are usually translated into functions. • Functions use data providers to provide configurable, re-entrant data.
are usually translated into functions. • Functions use data providers to provide configurable, re-entrant data.
are usually translated into functions. • Functions use data providers to provide configurable, re-entrant data.

Page Object Paradigm

Expose the service you’re interacting with, not the implementation.

- Selenium Wiki

If you’re using WebDriver API in your test methods…You are doing it wrong.

- Simon Stewart

- Selenium Wiki • If you’re using WebDriver API in your test methods …You are doing
- Selenium Wiki • If you’re using WebDriver API in your test methods …You are doing
- Selenium Wiki • If you’re using WebDriver API in your test methods …You are doing
- Selenium Wiki • If you’re using WebDriver API in your test methods …You are doing
Business Logic D r i v e r L o g i c

Business Logic

Business Logic D r i v e r L o g i c
Business Logic D r i v e r L o g i c

Driver Logic

Business Logic D r i v e r L o g i c
Return value for exported services Service Methods Returning void public class LoginPage { public void

Return value for exported services

Return value for exported services Service Methods Returning void public class LoginPage { public void login(){

Service Methods Returning void

public class LoginPage { public void login(){ //Selenium Code

}

}

public class AccountsPage { public void viewAccountDetails(){ //Selenium Code

}

}

Service Methods Returning correctObject

public class LoginPage { public AccountsPage login(){ //Selenium Code

}

}

public class AccountsPage { public AccountsDetailsPage viewAccountDetails(){ //Selenium Code

}

}

Code } } public class AccountsPage { public AccountsDetailsPage viewAccountDetails(){ //Selenium Code } }

Return Value - continued

Accounts Page

Return Value - continued Accounts Page public class LoginPage { public AccountsPage login(){ //Selenium Code }
Return Value - continued Accounts Page public class LoginPage { public AccountsPage login(){ //Selenium Code }

public class LoginPage { public AccountsPage login(){ //Selenium Code

}

}

Login Page

AccountsPage login(){ //Selenium Code } } • Login Page Return a generic Page Interface Object Instead
AccountsPage login(){ //Selenium Code } } • Login Page Return a generic Page Interface Object Instead
AccountsPage login(){ //Selenium Code } } • Login Page Return a generic Page Interface Object Instead

Return a generic Page Interface Object Instead

Timeouts

void testApplication() { loginPage = new LoginPage(); accountsPage = loginPage.login();

Thread.sleep(3000);

accountsPage.viewAccounts();

}

Thread.sleep(3000); accountsPage.viewAccounts(); } void testApplication() { loginPage = new LoginPage();
Thread.sleep(3000); accountsPage.viewAccounts(); } void testApplication() { loginPage = new LoginPage();
Thread.sleep(3000); accountsPage.viewAccounts(); } void testApplication() { loginPage = new LoginPage();
Thread.sleep(3000); accountsPage.viewAccounts(); } void testApplication() { loginPage = new LoginPage();
Thread.sleep(3000); accountsPage.viewAccounts(); } void testApplication() { loginPage = new LoginPage();

void testApplication() { loginPage = new LoginPage(); accountsPage = loginPage.login();

accountsPage.waitForPage();

accountsPage.viewAccounts();

}

Page Factory

Page Factory public class LoginPage { private WebElement email; private WebElement password; //Constructor public
Page Factory public class LoginPage { private WebElement email; private WebElement password; //Constructor public
Page Factory public class LoginPage { private WebElement email; private WebElement password; //Constructor public
Page Factory public class LoginPage { private WebElement email; private WebElement password; //Constructor public

public class LoginPage { private WebElement email; private WebElement password;

//Constructor public LoginPage(WebDriver driver){ driver.findById(“email”); driver.findById(“password”);

}

}

driver.findById(“password”); } } public class LoginPage { @FindBy(id = “email”) private

public class LoginPage { @FindBy(id = “email”) private WebElement email;

@FindBy(id = “password”) private WebElement password;

//Constructor public LoginPage(WebDriver driver){ PageFactory.initElement(driver, this);

}

}

Where do we stand?

Test/Business Logic

Page Object Page Object Page Object Data Provider Data Provider Data Provider
Page Object
Page Object
Page Object
Data Provider
Data Provider
Data Provider

Web App

Logic Page Object Page Object Page Object Data Provider Data Provider Data Provider Web App Auto
Logic Page Object Page Object Page Object Data Provider Data Provider Data Provider Web App Auto
Logic Page Object Page Object Page Object Data Provider Data Provider Data Provider Web App Auto
Logic Page Object Page Object Page Object Data Provider Data Provider Data Provider Web App Auto
Logic Page Object Page Object Page Object Data Provider Data Provider Data Provider Web App Auto

Auto

Generated!

Auto Generation of Page Object Code

Auto Generation of Page Object Code • An advantage of putting structure to the process gives

An advantage of putting structure to the process gives us a model for auto-generating object test code

AutoTestR uses node.js framework to parse the DOM for the page and generate corresponding test object code.

test code • AutoTestR uses node.js framework to parse the DOM for the page and generate
test code • AutoTestR uses node.js framework to parse the DOM for the page and generate
One step further… - Each node represents a page-object (or rather business object ) -

One step further…

One step further… - Each node represents a page-object (or rather business object ) - Each
One step further… - Each node represents a page-object (or rather business object ) - Each
One step further… - Each node represents a page-object (or rather business object ) - Each

- Each node represents a page-object (or rather business object)

- Each node specifies what are its exported interfaces, what are its next states

- Based on this graph, AutoTestR auto-generates test case skeletons as well!

- Example Test Cases:

- Login->Unregistered Page

- Login->Main Page->CreateUser->End

- Login->Main Page->SearchUser->End

- Login->Main Page->DeleteUser->End

- Login->Main Page->Unregistered Page

Auto Generated Test Cases: AutoTestR Demo

Auto Generated Test Cases: AutoTestR Demo
Auto Generated Test Cases: AutoTestR Demo
Auto Generated Test Cases: AutoTestR Demo
Auto Generated Test Cases: AutoTestR Demo

CI with AutoTestR

CI with AutoTestR
CI with AutoTestR
CI with AutoTestR
CI with AutoTestR
CI with AutoTestR

AutoTestR – Roadmap and Upcoming Features

Open-source and release a community version by end of the year.

More graph based intelligent testing, configurable edge weights.

Integrating with Cloud BasedTesting Infrastructure

Separate Test Assertions from Auto-Generated Code

Support for business logic testing

Auto generateTestNG files for regression and performance testing

Database based test key-value generation

• Auto generateTestNG files for regression and performance testing • Database based test key-value generation
• Auto generateTestNG files for regression and performance testing • Database based test key-value generation
• Auto generateTestNG files for regression and performance testing • Database based test key-value generation
• Auto generateTestNG files for regression and performance testing • Database based test key-value generation

Summary

Separate Business Logic from Implementation Logic

Return the Page Object (generic Page superclass) from exported services

Page Load Wait time is a property of the page – keep it in the page object!

Keep common utilities in the base class

Use Data Providers

Keep Exported methods Re-entrant

AutoTestR – To Auto Generate Page Objects and Test Cases

Use Data Providers • Keep Exported methods Re-entrant • AutoTestR – To Auto Generate Page Objects
Use Data Providers • Keep Exported methods Re-entrant • AutoTestR – To Auto Generate Page Objects
Use Data Providers • Keep Exported methods Re-entrant • AutoTestR – To Auto Generate Page Objects
Use Data Providers • Keep Exported methods Re-entrant • AutoTestR – To Auto Generate Page Objects
Services: - QA Automation Consulting - Continuous Integration - Web and Mobile Testing - Security

Services:

- QA Automation Consulting

- Continuous Integration

- Web and Mobile Testing

- Security Testing

- IOT Testing

- QA Staffing

- Web Development (MEAN stack)

- Mobile App Development (iOS/Android)

Contact Us:

Rhoynar Software Consulting Web: www.rhoynar.com

Us: Rhoynar Software Consulting Web: www.rhoynar.com Ph.: + 1-855-5-RHOYNAR Email: contact@rhoynar.com Courses:
Us: Rhoynar Software Consulting Web: www.rhoynar.com Ph.: + 1-855-5-RHOYNAR Email: contact@rhoynar.com Courses:
Us: Rhoynar Software Consulting Web: www.rhoynar.com Ph.: + 1-855-5-RHOYNAR Email: contact@rhoynar.com Courses:

Ph.: +1-855-5-RHOYNAR

Email: contact@rhoynar.com

Ph.: + 1-855-5-RHOYNAR Email: contact@rhoynar.com Courses: - QA Masters (Manual testing, HP ALM/QC, Selenium

Courses:

- QA Masters (Manual testing, HP ALM/QC, Selenium IDE, SQL)

- QA Advanced (Java, SQL, Selenium WebDriver, POM, BDD, Cucumber)

- Full-StackWeb Development using NodeJS, AngularJS, ExpressJS and MongoDB

- Corporate training (various topics) (1-5 days)

Contact Us:

DestinationJ Software Technologies Web: www.destinationj.com

Ph.: +1-303-408-9848

Email: contact@destinationj.com