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"

Running Selenium Scripts Under Multi-Window mode

Hi All,

By default, Selenium runs the application under test in a subframe. (Running the AUT in a subframe gives us a great deal of control over the AUT.) But some apps don't run properly in a subframe, preferring to be loaded into the top frame of the window. In that case, you need to make your application under test run in a seperate window rather than in the default frame. To do that, start selenium server with the -multiWindow parameter:

java -jar selenium-server.jar -multiWindow

Note that multiWindow mode is a little less stable than running in a frame, so you should probably avoid doing this if you can possibly help it.

How to Increase pause for ‘clickAndWait’ command in Selenium IDE

Hi All,

Now we can Increase the pause of clickAndWait command in the Selenium IDE.

setTimeout(timeout)

Arguments:
  • timeout - a timeout in milliseconds, after which the action will return with an error


Specifies the amount of time that Selenium will wait for actions to complete.

Actions that require waiting include "open" and the "waitFor*" actions.

The default timeout is 30 seconds.

e.g. setTimeout | 3600 will make the time out as 3600 millisec

Working with ‘openWindow’ command in SeleniumIDE

Hi All,

Here is an example of how to use 'openWindow' command in SeleniumIDE.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<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>openWindow</td>
<td>http://google.com</td>
<td>myWindow</td>
</tr>
</tbody></table>
</body>
</html>

How we can proceed is :
Command: openWindow
Target: url
Value: windowID

Thursday, November 20, 2008

Assert an Object’s hyperlinked address/URL


Hi All,

Using the Selenium IDE, it is possible


to assert the URL (path/address) of a hyperlink, that is the URL it is pointing

to without left clicking to navigate to that URL.

For example;

If you go to www.google.com

On the top you have the following hyperlinks:

"Image", "Maps", "News", "Shopping",

etc

If you right click on "Image", and

select properties, it gives you

"http://images.google.co.uk/imghp?hl=en&tab=wi".



·

We can assert that the object "Image"

is pointing to "http://images.google.co.uk/imghp?hl=en&tab=wi" on

that www.google.com page.

<tr>

                <td>verifyAttribute</td>
<td>locator@href</td>

<td>url</td>


</tr>        

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="" />
<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>pause</td>
<td>1000</td>
<td></td>
</tr>
<tr>
<td>assertAttribute</td>
<td>link=Images@href</td>
<td>http://images.google.co.in/imghp?hl=en&amp;tab=wi</td>
</tr>
</tbody></table>
</body>
</html>

Verifying Hidden Value in a Page Using Selenium IDE


Hi,

Here is an solution for verifying Hidden Text or Hidden Value in a web page using Selenium IDE.

<tr>


<td>assertValue</td>

<td>"name of the field"</td>

<td>"Value of the text to be verified"</td>

</tr>



For 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>type</td>

<td>q</td>

<td>w3schools</td>

</tr>

<tr>

<td>clickAndWait</td>

<td>btnG</td>

<td></td>

</tr>

<tr>

<td>clickAndWait</td>

<td>link=Learn JavaScript</td>

<td></td>

</tr>

<tr>

<td>pause</td>

<td>1000</td>

<td></td>

</tr>

<tr>

<td>assertValue</td>

<td>sitesearch</td>

<td>www.w3schools.com</td>

</tr>

</tbody></table>

</body>

</html>

Selenium Test Suite

Selenium Test Suite

Most testing frameworks have the concept of grouping a set of text fixtures into a 'suite' so that you can execute a number of related tests together.Creating test suites with Selenium doesn't appear to be obvious at first. Here's a simple guide on how to create and execute suites of functional tests:

You can run test suites via the Selenium web browser application. You'll need to download the Selenium Core (http://selenium-core.openqa.org/) in order to do this. After you have extracted the downloaded file, copy the 'core' directory to somewhere that is accessible under your server web root (E.g the document root).

Browse to http://your server/path to selenium core/TestRunner.html and you should see the Selenium test runner application. In order to create a test suite, all you have to do is create an HTML document that is composed of a table of links to your individual unit tests. For example:


<html>

<head>

<title>Sample Test Suite</title>

</head>

<body>

<table>

<tr>

<td><b>Suite</b></td>

</tr>

<tr>

<td><a href="./Script1.html" mce_href="./Script1.html">Script 1 </a></td>

</tr>

<tr>

<td><a href="./Script2.html" mce_href="./Script2.html">Script 2</a></td>

</tr>

<tr>

<td><a href="./Script3.html" mce_href="./Script3.html">Script 3</a></td>

</tr>

</table>

</body>

</html>

Obviously you'll need to adjust the href's to wherever you have your test cases stored, but essentially that is all there is to creating a test suite. Now return to the Selenium test runner application and alter the unit test path so that it finds the HTML file containing your test suite definition. When the page reloads you should see all of your individual unit tests listed within the frame on the left hand side.

You now have the option of executing all tests within the suite or run selected tests. The test runner reports results in the usual manner, stating the number of passes, failures or number of cases where errors were encountered within the actual test scripts.

Selenium Remote Control

Selenium Remote Control:

Selenium Remote Control provides a Selenium Server, which can automatically start/stop/control any supported browser. It works by using Selenium Core, a pure-HTML+JS library that performs automated tasks in JavaScript.

The Selenium Server communicates directly with the browser using AJAX (XmlHttpRequest). You can send commands directly to the Server using simple HTTP GET/POST requests; that means that you can use any programming language that can make HTTP requests to automate Selenium tests on the browser. To further ease this process, we provide wrapper objects for a number of mainstream programming languages (Java, .NET, Perl, Python, and Ruby).

Finally, the Selenium Server acts as a client-configured HTTP proxy, to stand in between the browser and your website. This allows a Selenium-enabled browser to run JavaScript on arbitrary websites.

The Selenium Server is great for testing complex AJAX-based web user interfaces under a Continuous Integration system. It is also an ideal solution for users of Selenium Core or Selenium IDE who want to write tests in a more expressive programming language than the Selenese HTML table format customarily used with Selenium Core.

Tuesday, November 18, 2008

Selenium Automation

Selenium IDE:


Selenium IDE is an integrated testing Environment for Selenium tests.Selenium IDE is an OpenQA tool available free to download. Visit the http://openqa.org/selenium-ide/download.action link to download the latest version of Selenium IDE.


How to Install Selenium IDE:


  • Open Mozilla FireFox browser, and type the above url in the browser window and click on go.
  • Click on the latest Firefox Extension hyperlink under the download subsection.
  • Click on the ‘Install Now’ button on the popup window opened. Refer to below picture:



  • After successful installation, close the FireFox and reopen the FireFox.
  • Go to ‘Tools’ option in the menu bar of the browser and we can see once option called ‘Selenium IDE’.

Getting Started:


Recording Using Selenium IDE:


  • Open Mozilla FireFox browser, go to ‘Tools’ option under Menu bar and Click on the ‘Selenium IDE’.
  • ‘Selenium IDE’ window will popup.


  • By default, it will be in recording mode.


  • Click on the first row of the ‘Command’ tag in the Table tag.




  • Enter the ‘open’ commands in the corresponding text boxes given below.


  • In Selenium IDE, we need to write the ‘open’ commands, as these commands are not recorded using Selenium IDE. Open the URL typed in the command in Selenium IDE, using the FireFox Browser.

  • Now record the test using selenium IDE. When we click on button or link or type something on the text boxes, it will recognize automatically. When u need to verify particular text to be present in the page, right click on page and choose verifyTextPresent command or verifyTitle command.



Playback Using Selenium IDE:


  • Once the recording is completed, click on the Play button available in the Menu bar of the Selenium IDE.