Wednesday, September 23, 2009

Selenium IDE Parametrization

Parametrization isn't a big deal if you are using selenium RC, But there are people who uses only Selenium IDE for the automation. And they face the problem when they need to do the parametrizationof the test case. so let's see how to do parametrization in selenium IDE.

You just need to make a JavaScript file which contains all the parameters with their values. And then you can easily access this parameter through selenium IDE.let's see this thing by an example.

let's first make the JavaScript file .

searchKey = "selenium IDE Parameterization"

copy the above line into a file and save it as data.js

Now as we have created the data file. We need some way for selenium to identify this data.js file and access the variables inside it. So for that you need to import it as Selenium IDE Extenstion. let's see how to do it.
First open the selenium IDE.
navigate to options->options
Under the selenium IDE extension browse the data.js file.
Restart the selenium IDE.

Now selenium IDE is able to access the data.js file. So let's see how to do it.
Following is the code that will open the google browser and will search the for the keyword we have sepecied in the data.js file.

<!--
Purpose: To show Prameterization using Selenium IDE
Created by: Gaurang Shah
Email: gaurangnshah@gmail.com
-->
<?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>Parameterization_Example</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">Parameterization_Example</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>searchKey</td>
<td>searchKey</td>
</tr>
<tr>
<td>type</td>
<td>q</td>
<td>${searchKey}</td>
</tr>
<tr>
<td>click</td>
<td>btnG</td>
<td></td>
</tr>

</tbody></table>
</body>
</html>

Monday, September 21, 2009

Selenium - Verify Element Properties

While doing the automation through selenium we also need to verify the tooltip of the image or it's path(location, URL). selenium has provided API to check the attirbutes of any element. Now let's see how to do this.

let's first see how to do this in selenium. The following script will navigate to http://qtp-help.blogspot.com. Then it will fetch the URL of the profile photo and will open the photo.

storeAttribute //img[@alt='My Photo']@src imagePath
open ${imagePath}


<!-- Purpose:- To show how to verfiy the Properties of a image
Created By:- Gaurang Shah
Email:- gauragnnshah@gmail.com
-->

<?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://qtp-help.blogspot.com/" />
<title>Verify Image Property</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>/</td>
<td></td>
</tr>
<tr>
<td>storeAttribute</td>
<td>//img[@alt='My Photo']@src</td>
<td>imagePath</td>
</tr>
<tr>
<td>open</td>
<td>${imagePath}</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>

Friday, September 4, 2009

Selenium - Element not found


Why Element not found error Occurs ??
Solution.


Why Element not found error occurs ??
This is most simple but most occurred error. There can be only one of two things why this error appears
  1. Locator(ID, Name, XPath, DOM or CSS) you have given is wrong
  2. Element is not visible
Now there can be two reason why element is not visible
  1. Element is not present on that page, may be wrong page has opened or element has been removed. Or
  2. Element is not visible element has not been loaded yet (Page hasn't loaded fully)

Solution
Solution is also easy. First check manually if element is present on the page or not. If it is present try to wait until that element appears. If your element is not being loaded as a ajax call you can simple use following function, which will wait until page loads.
selenium.waitForPageToLoad("3000");
you can increase the time if page is taking more time to load.

But if your element is being loaded as a part of AJAX call, you can use the following function to wait until that element appears or timeout occurs.

The function will return true if element will be found on page within giving time else will return false.

/**
  * @author Gaurang
  * @param locator 
  * @param timeout (minutes)
  * @throws InterruptedException
  * @return returns true if element found in given time else false
  *  wait until specified element appears or timeout occurs
  */
 public boolean waitForElementPresent(String xpath, int timeout) throws InterruptedException{
  int count = 0;
  while(selenium.isElementPresent(xpath) != true){
   Thread.sleep(10*1000); //Wait 10 seconds
   if(count++ > timeout*6 ) break;
  }
  
  if(selenium.isElementPresent(xpath))
   return true;
  else
   return false;
    
 }