Pages

Thursday, May 23, 2013

Master Selenium: Selenium IDE - Part-II




In the previous article "Master Selenium: Selenium IDE - Part-I", we learned how to created how to create a simple test case with Selenium.
In this part we will make it a little bit more complex :).

We will create a test case that will:
1- Opens the Delicious home page.
2- Searches by the keyword 'Selenium'.
3- Reads the number of bookmarks from the result page.

Now, this may not look like a test case for you but it will  but it's just an exercise that will surely apply in a real test case.


PREREQUISITES:

Downloading and installing Selenium RC
Selenium RC is a Java based command line server that starts browsers and runs commands you pass from your tests.
1. First make sure you have a Java runtime installed on your machine.
2. Download Selenium RC.
3. After extracting the files from the archive copy the ‘selenium-server.jar’ file to any directory you feel appropriate. I copied it to my PHP installations bin directory.
4. Start the Selenium RC server from the command-line by issuing the following command:
java -jar selenium-server.jar
This will start the server on port 4444.
5. Now the server is ready to accept test commands from your PHP script. Make sure you keep this server running till you finish testing.
Installing PHPUnit
1. An easy way to install PHPUnit is to use the PEAR installer. The PEAR channel (pear.phpunit.de) is used to distribute PHPUnit so make sure that it is registered with your local PEAR environment:
pear channel-discover pear.phpunit.de
After the channel is registered install PHPUnit:
pear install phpunit/PHPUnit

So let's get started!!


Start the Selenium IDE and put http://www.delicious.com/ as a base url.




















TIP: If you have a slow internet connection, you can slow the test speed.

Hit he execute button, the Delicious website will open.
Now simply, type the keyword "selenium" into the search bar and hit Enter.

This is what you'll get:









Is it all?? Definitely not!!

Running the same test from PHP in our goal.

If this was all there is, I wouldn't be really writing this post. The interesting part comes now.

From the file menu, select "Export Test Case As..." and ,as we are using PHP, select 'PHP - Selenium RC'.
Save the file.

















This is what you will get in the file:
<?php
 
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
 
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
  function setUp()
  {
    $this->setBrowser("*chrome");
    $this->setBrowserUrl("http://delicious.com/");
  }
 
  function testMyTestCase()
  {
    $this->open("/");
    $this->type("homepage-searchinput", "selenium");
    $this->click("homepage-searchsubmit");
    $this->waitForPageToLoad("30000");
    $this->getText("xpath=/html/body[@id='index']/div
                         [@id='doc3']/div[@id='bd']/div
                         [@id='yui-main']/div[@id='content']/h3/div/em");
  }
}
?>


huh!! What's the complex looking line.

Everything looks fine until you get to '$this->getText…’. 
The function gettext() returns the text found at the element specified in the expression. The complex looking line is the XPath to the element:

You don't have to rack your brain to find the xpath of an element. An easier way is the install the firefox add-on XPATHER.



Let's run the test

In the command line type phpunit Delicious to run the test.
Now the above test does basically nothing, let's add some conditional statements:

$bookmarks = $this->getText("xpath=/html/body[@id='index']/div
                       [@id='doc3']/div[@id='bd']/div
                       [@id='yui-main']/div[@id='content']/h3/div/em");
 
    echo ($bookmarks > 3000)? "On the top" : "Still not there";
  }
}
 
?>
Now the execution result will be like this:










What I think About it!


This concludes part II.
Selenium, in my opinion, one of best test automation tools that works perfectly with web-based applications.

No comments:

Post a Comment