3. 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
4. How does it work?
• First, launch Selenium server:
java –jar selenium-server.jar
• Second, run a Selenese script:
phpunit mytest.php
5. Where to get it?
• Selenium RC:
http://seleniumhq.org/download/
• PHP Unit:
http://www.phpunit.de/manual/current/en/installation.html
6. 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…
7. 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
}
}
?>
9. 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”, “joe@example.com”);
$this->type(“signin_password”, “123456”);
$this->select(“gender”, “male”);
$this->click(“//a[@onclick=”$(this).parents(‘form’).eq(0).submit(); return false;”]”);
10. Wait a minute… What the heck is $this-
>click(“//a[@onclick=”$(this).parents(‘form’).e
q(0).submit(); return false;”]”); ?
• It’s an XPath
XPath 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
12. 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]);
13. 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.
15. 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_s
tack']/div[@id='tab_content_container']/div[@id='flag
_content']/div/div[@id='newWidget_flag_1']”,
“Frequently Asked Questions”);
16. 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”);
17. Caveats - False Positives
• Selenium RC test alerts can result in false positives
18. 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");
20. 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