Pages

Search This Blog

Friday, December 3, 2010

How to automate onblur through selenium RC .

How to automate onblur through selenium RC .
How to automate lost focus through selenium.


I faced the problem while automating the form submit. All of the form fields are having some action on their lost focus.
The onblur event occurs when an object loses focus.
For example : I have two fields First Name and Last Name. When i enter first name and press tab then it loses its focus and onblur function gets called.
and its calls upperCase and it changes the first name in Upper case.
the same case with Last Name. When i enter last name and press tab it calls blur function lowerCase and changes the letters in lower case.

But the problem in automation is i cant automate lost focus. when we type in first name and last name it simply types in first name and last name text box.
It does not call onblur function upper case and lower case and does not change the letter respectively.

so, fireEvent is a special command in selenium which helps in automating onblur function.

this is html of the element and blur javascript functions


<html>
<head>
<script type="text/javascript">
function upperCase()
{
var x=document.getElementById("fname").value;
document.getElementById("fname").value=x.toUpperCase();
}
function lowerCase()
{
var x=document.getElementById("lname").value;
document.getElementById("lname").value=x.toLowerCase();
}
</script>
</head>
<body>
Enter your First Name: <input type="text" id="fname" onblur="upperCase()" />
<br />
Enter your Last Nast: <input type="text" id="lname" onblur="lowerCase()" />
</body>
</html>


when we write simple selenium RC code

 selenium.type("fname", "niraj");
 selenium.type("lname", "KUMAR");
 
it doest call blur functions to change cases of the first name and last name.

But by using fireEvent fuction of selenium we can call blur function.

 selenium.type("fname", "niraj");
 selenium.fireEvent("fname", "blur");
 selenium.type("lname", "KUMAR");
 selenium.fireEvent("lname", "blur");

How this works is :

First this will type "niraj" in First Name and then selenium.fireEvent("fname", "blur"); this will call the onblur fuction "upperCase()" which changes the First Name in upper case "NIRAJ".
then it types in Last Name and then selenium.fireEvent("lname", "blur"); which means it will press tab and lost the function and on the lost focus it calls
blur function lowerCase which changes the Last Name in lower case.

10 comments:

  1. thanks for sharing this - had exactly the same problem and your solution worked a treat.

    ReplyDelete
  2. Hey Niraj,

    Thanks for the Post , this saved ma day . It worked perfectly fine for me .

    Let me explain the issue i faced .

    I have two text field , were when i enter some text in the first text field and by tabbing or clicking the second text field there is a action to execute which will enable the Publish Button .

    but when i used the type and typeKeys and tried clicking on the text field as well .. nothing helped ..


    i just found this onblur function in the HTMl function and i did follow the above post ..

    I was able to solve ma issue ..

    Thanks once again Niraj

    ReplyDelete
  3. Very helpful post! Thanks for taking the time to write it.

    ReplyDelete
  4. Thank you, more over thank you for taking the time to provide the steps of your discovery. I finally had the AHA moment while reading your post and I read 7 other posts before this one!

    ReplyDelete
  5. This helped me figure out that you need to leave the "on" part of code out of the Value argument in Selenium IDE. Thx.

    ReplyDelete
  6. Thanks for sharing this informative blog about Over-Automation. It is very nice blog.

    ReplyDelete
  7. These tips are very useful for us and through these, we can get many benefits. We should follow these tips and enjoy success. I hope, many users will get benefits. Dissertation writing service.

    ReplyDelete