Thursday, November 27, 2008

Why Selenium scripts fail when run outside the web server for Selenium Core?

Hi All,

As we know selenium scripts will fail when we place our scripts outside the AUT (Application Under Test) web server for Selenium Core, this is due to the reason that Selenium core is built with DHTML / JavaScript. When we are using javaScript we should bound to Same Origin Policy of JavaScript.

What is Same Origin Policy?

Same origin Policy states that javaScript is only allowed to read / Modify the HTML content from the same origin as source i.e, javaScript can able to read / Modify the HTML only when the source and destination of the scripts are from same web server.

For Example:

Suppose a person is working on crutial page and some unsecured webpage simultaneously which are from different sources. If JavaScript is not bound to same Origin Policy, then the other user / Owner of unsecured web page can access / Modify the content of crutial web page. In order to avoid this JavaScript should be bound to Same Origin Policy.

But when we install Selenium IDE, this acts as a trusted FireFox extension and therefore we can violate the Same Origin Policy.

Wednesday, November 26, 2008

Handling Dropdown elements using Selenium IDE

Hi All,

Here is an example on how to handle Dropdown elements in selenium IDE.

Example:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Select From Dropdown</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">Select From Dropdown</td></tr>
</thead><tbody>
<tr>
<td>store</td>
<td>58058</td>
<td>option1</td>
</tr>
<tr>
<td>store</td>
<td>30</td>
<td>index1</td>
</tr>
<tr>
<td>open</td>
<td>http://pages.ebay.com/sitemap.html</td>
<td></td>
</tr>
<tr>
<td>select</td>
<td>category0</td>
<td>label=Toys &amp; Hobbies</td>
</tr>
<tr>
<td>storeSelectedLabel</td>
<td>category0</td>
<td>label1</td>
</tr>
<tr>
<td>assertSelectedIndex</td>
<td>category0</td>
<td>${index1}</td>
</tr>
<tr>
<td>assertSelectedLabel</td>
<td>category0</td>
<td>${label1}</td>
</tr>
<tr>
<td>select</td>
<td>category0</td>
<td>label=Computers &amp; Networking</td>
</tr>
<tr>
<td>storeSelectedLabel</td>
<td>category0</td>
<td>label2</td>
</tr>
<tr>
<td>assertSelectedLabel</td>
<td>category0</td>
<td>${label2}</td>
</tr>
<tr>
<td>assertSelectedValue</td>
<td>category0</td>
<td>${option1}</td>
</tr>
</tbody></table>
</body>
</html>

Monday, November 24, 2008

Example for gotoLabel command in Selenium IDE

Hi All,

Here is the example for gotoLabel command. When Selenium IDE encounters gotoLabel command, it searches for targetted label name in the rest of the script and passes the control to targetted Label and the executes the steps from Label command and skips the steps that are in between gotoLabel and Label commands. When the targetted label is not found it throws error.

Extension for Label Command:

Selenium.prototype.doLabel = function(){};

Example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://www.google.co.in/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>http://www.google.co.in/</td>
<td></td>
</tr>
<tr>
<td>verifyText</td>
<td>link=Go to Google.com</td>
<td>Go to Google.com</td>
</tr>
<tr>
<td>gotolabel</td>
<td>label1</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>link=Go to Google.com</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>this wont execute</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>link=Go to Google India</td>
<td></td>
</tr>
<tr>
<td>label</td>
<td>label1</td>
<td></td>
</tr>
<tr>
<td>verifyTitle</td>
<td>Google</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>

User extension for gotoLabel command

Hi All,

We can use selenium for loops. Here is the extension for gotoLabel:

Selenium.prototype.doGotolabel = function( label )
{
if( undefined == gotoLabels[label] ) {
throw new Error( "Specified label '" + label + "' is not found." );
}
this.continueFromRow( gotoLabels[ label ] );
};

Friday, November 21, 2008

Locating an Element in Selenium using XPath

xpath=xpathExpression: Locate an element using an XPath expression

Examples:

xpath=//img[@alt='The image alt text']
xpath=//table[@id='table1']//tr[1]/td[2]
xpath=//a[contains(@href,'#id')]
xpath=//a[contains(@href,'#id')]/@class
xpath=(//table[@class='Selenium'])//th[text()='IDE']/../td
xpath=//input[@name='name' and @value='yes']
xpath=//*[text()="Selenium"]

How to Verify Number of Rows in a Table using Selenium


Hi All,

We can compare number of rows in table with predefined number using verifyTableRows extension. If success it will return true, else it will return false.

user-extension.js:


Selenium.prototype.getTableRows = function(locator) {
/**
* Gets the number of rows in a table.
*
* @param locator element locator for table
* @return number of rows in the table, 0 if none
*/

var table = this.browserbot.findElement(locator);

return table.rows.length.toString();

};

Consider test_get_table_rows.html is the page to test.

<html>
<head>
</head>
<body>
<table id="tableWith3Rows">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
</table>
<table id="tableWithNoRows">
</table>
</body>
</html>

by using verifyTableRows we can verify Number of Rows as follows:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TestTableRows</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">TestTableRows</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>./tests/html/test_table_rows.html</td>
<td></td>
</tr>
<tr>
<td>verifyTableRows</td>
<td>tableWith3Rows</td>
<td>3</td>
</tr>
<tr>
<td>verifyTableRows</td>
<td>tableWithNoRows</td>
<td>0</td>
</tr>

</tbody></table>

</body>

</html>

Selenium Core Playback in IE - HTA Mode

Selenium Core provides an additional mechanism for running automated tests called "HTA mode." HTA Mode only works on Windows, and only with Internet Explorer; you can't use HTA mode to test any other browser. An HTA file is a special type of HTML file that is allowed to violate the same origin policy and to write files to disk. When running in HTA mode, you don't have to install Selenium Core on the same webserver as the AUT. HTA files are also allowed to save test results directly to disk, rather than POSTing the test results to a webserver.

To run Selenium Core in HTA mode, just double-click on TestRunner.hta, in the /core directory of your Selenium installation. (As you can see, TestRunner.hta is just a copy of TestRunner.html; but that ".hta" extension makes a big difference!) You can then run your test suite just like any other Selenium Core test, but since the tests are being run in HTA mode, you're free to run your tests against any website.

You can also run TestRunner.hta from the command line, passing arguments to it just like you would pass arguments to an HTML file, like this:

C:\selenium\core>TestRunner.hta "test=..%2Ftests%2FTestSuite.html&auto=true&close=true&resultsUrl=results.html&save=true"