Introduction to selenium_grid_workshop

31
Introduction to Selenium Grid Kevin Menard SeleniumConf 2012

Transcript of Introduction to selenium_grid_workshop

Page 1: Introduction to selenium_grid_workshop

Introduction to

Selenium Grid

Kevin Menard

SeleniumConf 2012

Page 2: Introduction to selenium_grid_workshop

What is Selenium Grid?

• Maintain a cluster of Selenium nodes

• Configure tests for d ifferent

environments

• Parallelize your tests

Page 3: Introduction to selenium_grid_workshop

How Does it Work?

Selenium Server

host_a

host_b

Selenium Grid Node

Client

Selenium Grid Hub

Client Client Client

WebDriver WebDriver

Browser Browser Browser

Selenium Server Selenium Grid Node

Browser Browser Browser

Page 4: Introduction to selenium_grid_workshop

Starting up our Hub

• Prerequisites:

• A Java 5+ JRE

• selenium-server-standalone.jar

• Run:

$ java –jar selenium-server-standalone.jar –role hub

Page 5: Introduction to selenium_grid_workshop

The Grid Console

• Web console to see grid state and config

• Open a web browser to:

• http://localhost:4444/grid/console

• Beta console available at:

• http://localhost:4444/grid/beta/console

Page 6: Introduction to selenium_grid_workshop

Configuring the Hub

• Three options (in order of precedence):

• Grid 1 compatible YAML file

• JSON configuration file

• Command-line flags

• View current hub config in console

• Click “View Config” link

Page 7: Introduction to selenium_grid_workshop

CLI Configuration

• Find config option in console

• Hover over config name for valid options

• Use that name as a CLI option

• Example to block until a capability is present:

$ java –jar selenium-server-standalone.jar –role hub

–throwOnCapabilityNotPresent false

Page 8: Introduction to selenium_grid_workshop

• Each option in console is a JSON key with

appropriate data type.

• E.g.,

{ “throwOnCapabilityNotPresent” : false }

• Start the hub with –hubConfig option:

JSON Configuration

$ java –jar selenium-server-standalone.jar –role hub

–hubConfig hub_config.json

Page 9: Introduction to selenium_grid_workshop

Grid1 YAML Config

• Same config file format as Selenium Grid 1

• If you’ve never used grid1, don’t worry

about it

• Start the hub with -grid1Yml option:

$ java –jar selenium-server-standalone.jar –role hub

–grid1Yml grid_configuration.yml

Page 10: Introduction to selenium_grid_workshop

Starting up a Node

• Prerequisites:

• A Java 5+ JRE

• selenium-server-standalone.jar

• Run:

$ java –jar selenium-server-standalone.jar –role node

–hubHost localhost

Page 11: Introduction to selenium_grid_workshop

Running a Browser Session

• WebDriver

• Always use a RemoteWebDriver

• RC

• No special considerations

• Treat hub as your remote server

• Let’s start up Firefox on our grid

Page 12: Introduction to selenium_grid_workshop

Configuring the Node

• Two options (in order of precedence):

• JSON configuration file

• Command-line flags

• View current node config in beta console

• Click “Configuration” tab for node

Page 13: Introduction to selenium_grid_workshop

CLI Configuration

• Find config option by running with “-h” flag

• Example to change the port the node binds to:

$ java –jar selenium-server-standalone.jar –role node

–hubHost localhost –port 5556

Page 14: Introduction to selenium_grid_workshop

• Each option in console is a JSON key with

appropriate data type.

• E.g.,

{ “port” : 5556 }

• Start the hub with –nodeConfig option:

JSON Configuration

$ java –jar selenium-server-standalone.jar –role node

–nodeConfig node_config.json

Page 15: Introduction to selenium_grid_workshop

Parallelizing Tests

• Grid helps you organize resources and

spread load

• You still need to parallelize your tests

• TestNG makes this pretty straightforward

• Junit is more work, but doable

• See article Adam Goucher wrote for

SauceLabs’s blog

• Outside the scope of this workshop

Page 16: Introduction to selenium_grid_workshop

Extending Grid

• Primary ways to extend grid :

• Contribute a new servlet

• Provide a new Prioritizer impl.

• Provide a new CapabilityMatcher impl.

• Provide a new RemoteProxy impl.

Page 17: Introduction to selenium_grid_workshop

Creating a New API Servlet

• Any Servlet class will work

• Subclass RegistryBasedServlet to gain

access to Registry (central grid datastore)

• Make sure it’s on the hub’s classpath

• Start hub like so:

$ java –cp selenium-server-standalone.jar:my_console.jar

org.openqa.grid.selenium.GridLauncher –role hub –servlets

com.example.grid.servlets.Sessions

Page 18: Introduction to selenium_grid_workshop

Let’s Build a Servlet

• Will list all the running test sessions

• Returns results in JSON

• Provides queryable API

• Accessible from:

• http://localhost:4444/grid/admin/Sessions

Page 19: Introduction to selenium_grid_workshop

Altering Grid’s Execution

• org.openqa.grid .internal.listeners.Prioritizer

• org.openqa.grid .internal.u tils.CapabilityMatcher

• org.openqa.grid .internal.RemoteProxy

Page 20: Introduction to selenium_grid_workshop

Prioritizer

• Customize the execution order of tests

• Compares requests by desired

capabilities

• Defaults to FIFO behavior

• Provided as hub configuration:

$ java –cp selenium-server-standalone.jar:my_console.jar

org.openqa.grid.selenium.GridLauncher –role hub –prioritizer

com.example.MyPrioritizer

Page 21: Introduction to selenium_grid_workshop

CapabilityMatcher

• Define custom strategy for matching

clients and nodes

• Default is any node that matches

desired capabilities is a candidate

• Provided as hub configuration:

$ java –cp selenium-server-standalone.jar:my_console.jar

org.openqa.grid.selenium.GridLauncher –role hub

–capabilityMatcher com.example.MyCapabilityMatcher

Page 22: Introduction to selenium_grid_workshop

RemoteProxy

• Hub’s interface to a node

• Further refined through:

• RegistrationListener

• TestSessionListener

• CommandListener

• SelfHealingProxy

• TimeoutListener

• HtmlRenderer

Page 23: Introduction to selenium_grid_workshop

RegistrationListener

• Run just before a node is registered on

the grid

• Typical uses:

• Modify node’s configuration

• Facilitate mass node management

Page 24: Introduction to selenium_grid_workshop

TestSessionListener

• Run just before a test session is created

• Run just after a test session ends

• Typical uses:

• Gather information about what ran

on the node

• Ensure clean state of the OS

Page 25: Introduction to selenium_grid_workshop

CommandListener

• Run just before each command starts

• Run just after each command finishes

• Similar to TestSessionListener but finer grain

• Typical uses:

• Gather information about what ran on the

node

• Ensure clean state of the OS

Page 26: Introduction to selenium_grid_workshop

SelfHealingProxy

• Monitors grid health and takes

corrective action

• Default & typical use:

• Ensure hub & nodes can talk to each

other

• You likely don’t want to change the

check, but maybe the corrective action

• E.g., restart VMs if node is down

Page 27: Introduction to selenium_grid_workshop

TimeoutListener

• Handle client crashes

• Default & typical use:

• Shutdown any session that appears

orphaned

• You likely don’t want to change the

check, but maybe the corrective action

• E.g., restart VMs if node is down

Page 28: Introduction to selenium_grid_workshop

HtmlRender

• Not implemented by RemoteProxy, but

returned from it

• Dictates how node is rendered in Web

console

• Simple method, very free-form

• Mapping a custom servlet may make

more sense

Page 29: Introduction to selenium_grid_workshop

Let’s Build a RemoteProxy

• Start by extending DefaultRemoteProxy

• We’ll interface with BrowserMob Proxy

through TestSessionListener

• Start a new proxy on node the test will run

on based on desired capabilities

• Stop proxy when the test session is over

Page 30: Introduction to selenium_grid_workshop

Recap

• Today we:

• Set up a small grid

• Learned how to configure the grid

• Ran a browser session on the grid

• Added a custom servlet to provide a simple

API

• Created a custom RemoteProxy to work

with BrowserMob Proxy

Page 31: Introduction to selenium_grid_workshop

The End

• Questions?

• Kevin Menard

[email protected]

• Twitter: @nirvdrum

• IRC: “nirvdrum” on FreeNode

channel #selenium