Pages

Search This Blog

Tuesday, September 14, 2010

How to share a variable in two test cases

How to share a variable in more than one test case.
We Create a variable in one test case and use in another test cases. If these test cases are running seperately and independetely then those variable
cannot shared. But if we run all test cases in test suite then one varaible declared in one testcase can be used in another
Follow these steps.
Create your test cases saparetly and call them in a test suite.
1.Create first test case
Test1.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Propagate_Negative_API_UK</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">Propagate_Negative_API_UK</td></tr>
</thead><tbody>
<!------------------ Initialization steps ---------->
<tr>
 <td>store</td>
 <td>varaible from first test1</td>
 <td>vartest1</td>
</tr>


<tr>
 <td>open</td>
 <td>http://www.google.com</td>
 <td></td>
</tr>

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

2.Create second test case
Test2.html
==========

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Propagate_Negative_API_UK</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">Propagate_Negative_API_UK</td></tr>
</thead><tbody>
<!------------------ Initialization steps ---------->
<tr>
 <td>store</td>
 <td>varaible from first test2</td>
 <td>vartest2</td>
</tr>

<tr>
 <td>open</td>
 <td>http://www.yahoo.com</td>
 <td></td>
</tr>
<tr>
 <td>echo</td>
 <td>${vartest1}</td>
 <td></td>
</tr>

3.Create third test case
Test3.html
=========

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Propagate_Negative_API_UK</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">Propagate_Negative_API_UK</td></tr>
</thead><tbody>
<!------------------ Initialization steps ---------->
<tr>
 <td>store</td>
 <td>varaible from first test3</td>
 <td>vartest3</td>
</tr>

<tr>
 <td>open</td>
 <td>http://www.gmail.com</td>
 <td></td>
</tr>
<tr>
 <td>echo</td>
 <td>${vartest2}</td>
 <td></td>
</tr>
</tbody></table>
</body>
</html>

4.Create test suite
TestSuite.html
=========


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

     <tr>
       <td>Test suite for sharing variable among test cases </td>
     </tr>

     <tr>
      <td><a target="testFrame" href="test1.html">test1</a></td>
     </tr>

    <tr>
      <td><a target="testFrame" href="test2.html">test2</a></td>
    </tr>

    <tr>
      <td><a target="testFrame" href="test3.html">test3</a></td>
    </tr>
  </tbody>
</table>
</body>
</html>


Run your test suite and you will see the variable vartest1 decalred in testcase1 being used in testcases2
and variable vartest2 declared in testcase2 being used in testcase3.

Files are attached here . Right click on this link and save as ...
  Download test case and suite

How to use functions in xpath in selenium

How to use functions in xpath
Automation using selenium is a great experience. It provides many way to identif an object or element on the web page.
But sometime we face the problems of idenfying the objects on a page which have same attributes. When we get more than
one element which are same in attribute and name like multiple checkboxes with same name and same id. More than one button having
same name and ids. There are no way to distingues those element. In this case we have problem to instruct selenium to identify a perticular
object on a web page.
I am giving you a simple example . In the below html source there are 6 checkboxes are there having same type and same name.
It is really tough to select third or fifth.

<html>
<body>
<input type='checkbox' name='chk'>first
<br><input type='checkbox' name='chk'>second
<br><input type='checkbox' name='chk'>third
<br><input type='checkbox' name='chk'>forth
<br><input type='checkbox' name='chk'>fifth
<br><input type='checkbox' name='chk'>sixth
</body>
</html>


Thare are some function we can use in Xpath to identify the abject in above cases.
An XPath expression can return one of four basic XPath data types:

* String
* Number
* Boolean
* Node-set

XPath Type : Functions
Node set : last(), position(), count(), id(), local-name(), namespace-uri(), name()
String : string(), concat(), starts-with(), contains(), substring-before(), substring-after(), substring(), string-length(), normalize-space(), translate()
Boolean : boolean(), not(), true(), false(), lang()
Number : number(), sum(), floor(), ceiling(), round()

I will show you how we can use some of these above functions in xpath to identify the objects.

Node Set : last()


In the above html file there are six checkboxes and all are having same attributes (same type and name)
How we can select the last checkbox based on the position. We can use last() function to indentify the last object among all similar objects.
Below code will check or uncheck the last checkbox.

selenium.click("xpath=(//input[@type='checkbox'])[last()]");

How we can select the second last checkbox and third last checkbox. We can use last()- function to indentify the last object among all similar objects.
Below code will check or uncheck the second last checkbox and thrid last checkbox respectively.

selenium.click("xpath=(//input[@type='submit'])[last()-1]");
selenium.click("xpath=(//input[@type='submit'])[last()-2]");


Node Set : position()

If you want to select any object based on their position using xpath then you can use position() function in xpath.
You want to select second checkbox and forth checkbox then use below command

selenium.click("xpath=(//input[@type='checkbox'])[position()=2]");
selenium.click("xpath=(//input[@type='checkbox'])[position()=4]");

above code will select second and forth checkbox respectively.

String : starts-with()

Many web sites create dynamic element on their web pages where Ids of the elements gets generated dynamically.
Each time id gets generated differently. So to handle this situation we use some JavaScript functions.

XPath: //button[starts-with(@id, 'continue-')]  


Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
To demonstrate, the element can be located based on the ‘suggest’ class without having
to couple it with the ‘top’ and ‘business’ classes using the following

XPath: //input[contains(@class, 'suggest')].



For rest of the function please keep reading my blogs i will be posting very soon.