Michael SalvucciJanuary 4, 2011
Selenium RC:Introduction
What is Selenium RC?• It is a program that allows test automation• It simulates a user navigating through pages
and then verifies that content against expectations
• It alerts the user to discrepancies via console or email
• Its Selenese language allows phpunit to connect to a selenium server
• The RC stands for Remote Control
How does it work?
• First, launch Selenium server:java –jar selenium-server.jar
• Second, run a Selenese script:phpunit mytest.php
Where to get it?• Selenium RC:
http://seleniumhq.org/download/
• PHP Unit:http://www.phpunit.de/manual/current/en/installation.html
What does the script look like? (1 of 2)<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class Example extends PHPUnit_Extensions_SeleniumTestCase{
protected function setUp(){
$this->setBrowser("*chrome");$this->setBrowserUrl("http://www.nationalguard.com");
}
continued…
What does the script look like? (2 of 2)
public function testMyTestCase(){
$this->open("/");sleep(5);$this->verifyTitle(“The National Guard");
// put the rest of your tests here
}}?>
Selenium RC:Common Tests
Now that I’ve got it… What can I do with it?• Open up a page:
$this->open("/products");
• Click on a link:$this->click(“link=Log In”);
• Verify a title:$this->verifyTitle(“The National Guard”);
• Fill-out a form:$this->type(“signin_username”, “[email protected]”);
$this->type(“signin_password”, “123456”);
$this->select(“gender”, “male”);
$this->click(“//a[@onclick=\”$(this).parents(‘form’).eq(0).submit(); return false;\”]”);
Wait a minute… What the heck is $this->click(“//a[@onclick=\”$(this).parents(‘form’).eq(0).submit(); return false;\”]”); ?
• It’s an XPathXPath is a language that describes how to locate specific elements in a document
• How do I find the XPath for something?The easiest way is via a Firefox plug-in called XPather. Xpather extracts XPaths
while browsing or inspecting HTML/XML/*ML documents.
Then, when viewing a webpage, highlight the element you want, right-click for the submenu, then left-click on “Show in XPather”.
• XPather is available at:
http://xpath.alephzarro.com
XPather
What else can I do?• Verify text appears on a page:
$this->verifyText(“//html/body/div[@id=‘wrapper’]/div[@id=‘content’]/h2”, “Thank you Iostudio Tester”);
• Verify an element is present:$this->verifyElementPresent(“//html/body[@id=‘profile’]/div[@id=‘wrap’]/div”);
• Verify a URL:$this->verifyLocation(“http://www.nationalguard.com/chat”);
• Switch over to a pop-up window so you can examine it:$windowName = $this->getAllWindowNames();
$this->waitForPopUp($windowName[1], “45000”);
$this->selectWindow($windowName[1]);
Locating Elements• “For many Selenium commands, a target is required. This target identifies
an element in the content of the web application, and consists of the location strategy followed by the location in the format locatorType=location.” I’ve shown you the XPath way. Here’s some other examples:
• Locating by identifier:
verifyElementPresent(“identifier=loginForm”);
• Locating by DOM:
verifyElementPresent(“dom=document.getElementById(‘loginForm”);
• Locating by CSS selector:
verifyElementPresent(“css=form#loginForm”);
• Refer to: http://seleniumhq.org/docs/04_selenese_commands-html for more information.
Selenium RC:Caveats
Caveats - verifyText() vs. verifyTextPresent()
• Use verifyText() instead of verifyTextPresent() because the latter does not work properly
• verifyTextPresent() is also weak because it doesn’t pinpoint what you want to look at exactly (eg. $this->verifyTextPresent(“Frequently Asked Questions”); )
• Proper usage (N.B. This should all be on one line):
$this->verifyText(“//html/body/div[@id='wrapper']/div[@id='content_wrp']/div[@id='sidebar']/div[@id='side_tab_stack']/div[@id='tab_content_container']/div[@id='flag_content']/div/div[@id='newWidget_flag_1']”, “Frequently Asked Questions”);
Caveats - XPather• XPather reports elements with a single leading slash. However, Selenium
RC expects to see two leading slashes for XPaths.
$this->verifyElement(“//html/body/div[@id=‘wrapper’]/div[@id=‘content_wrp’]/div[@id=‘sidebar’]/div”);
Caveats - False Positives• Selenium RC test alerts can result in false positives
Caveats - Pop-up Windows• A problem with grabbing a pop-up window is that the window’s name is
dynamic, and you have to know the window’s name in order to select it.
• The pop-up could be named “selenium_blank45375”. The next time, it could be named “selenium_blank41821”, so we have to use the getAllWindowNames() function to get the name of the pop-up we need.
$this->click("link=Take the tour.");$windowName = $this->getAllWindowNames();$this->waitForPopUp($windowName[1], "45000");$this->selectWindow($windowName[1]);sleep(7);$this->verifyTitle("Army National Guard Virtual Career Fair");
Selenium RC:Hudson Integration
Hudson Integration• With Hudson, you can automate Selenium (eg. run hourly tests) and
receive email alerts when problem arise.• You can get Hudson here:
http://hudson-ci.org
Selenium RC:References
References• http://seleniumhq.org/download/• http://seleniumhq.org/docs/index.html• http://seleniumhq.org/docs/04_selenese_commands.html• http://release.seleniumhq.org/selenium-remote-
control/0.9.2/doc/php/Selenium/Testing_Selenium.html• PHP Unit: http://www.phpunit.de/manual/current/en/installation.html• XPather (A Firefox Plugin for generating XPaths):
http://xpath.alephzarro.com/• Hudson: http://hudson-ci.org