Introduction to selenium_grid_workshop
-
Upload
seleniumconf -
Category
Technology
-
view
2.876 -
download
0
Transcript of Introduction to selenium_grid_workshop
Introduction to
Selenium Grid
Kevin Menard
SeleniumConf 2012
What is Selenium Grid?
• Maintain a cluster of Selenium nodes
• Configure tests for d ifferent
environments
• Parallelize your tests
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
Starting up our Hub
• Prerequisites:
• A Java 5+ JRE
• selenium-server-standalone.jar
• Run:
$ java –jar selenium-server-standalone.jar –role hub
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
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
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
• 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
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
Starting up a Node
• Prerequisites:
• A Java 5+ JRE
• selenium-server-standalone.jar
• Run:
$ java –jar selenium-server-standalone.jar –role node
–hubHost localhost
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
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
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
• 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
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
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.
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
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
Altering Grid’s Execution
• org.openqa.grid .internal.listeners.Prioritizer
• org.openqa.grid .internal.u tils.CapabilityMatcher
• org.openqa.grid .internal.RemoteProxy
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
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
RemoteProxy
• Hub’s interface to a node
• Further refined through:
• RegistrationListener
• TestSessionListener
• CommandListener
• SelfHealingProxy
• TimeoutListener
• HtmlRenderer
RegistrationListener
• Run just before a node is registered on
the grid
• Typical uses:
• Modify node’s configuration
• Facilitate mass node management
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
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
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
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
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
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
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
The End
• Questions?
• Kevin Menard
• Twitter: @nirvdrum
• IRC: “nirvdrum” on FreeNode
channel #selenium