Saturday, March 6, 2010

Invalid argument error on Selenium RC Server while selecting option from drop down in IE

If you try to select a label in a drop down using type command in selenium, what do you expect it to do??

Throw error??

or work fine??

Well, both the statements are true. The first one holds true for IE, and second one holds true for Firefox.

If you try to use the following command for to select option from the drop down using a type command, Firefox will work cool, but IE will throw an Invalid argument error.

//Here type function sends the type command to selenium incorrectly to select a label from drop down.
$object->type('id=id_for_drop_down', 'label_in_a_drop_down');

This will throw the said error in IE and we can not blame it on IE all the times.

So just change it to send the command to select the label and it will work fine for you. So the new code will look like this:

//HERE SELECT OPTION FUNCTION SENDS THE SELECT FROM DROP DOWN COMMAND TO SELENIUM
$object->select_option('id=id_for_drop_down', 'label_in_a_drop_down');

Hope it helps.

Thanks,
QuickSilver1183
QuickSilver1183@gmail.com

Permission denied error after clicking on link when using selenium

Selenium throws a Permission Denied error after clicking, if we are not waiting for page to load before accessing the GUI. In most of the cases it works fine for Firefox, but with IE, this fails.

Consider the following piece of code:

//click function here take care of sending click command to selenium RC Server
$object->click('link=www.somelink.com');

//Type command sends USERNAME to the Selenium RC Server to be typed in input box with ID: username
$object->type('id=username', 'USERNAME');

Now the above mentioned code works good for Firefox, but it throws Permission denied error with IE.

Reason for that is as we have clicked on the link, and the page has to refresh, therefore when we try to type into some field on GUI that has not loaded yet, selenium throws Permission denied error.

Simple solution to above is to use clickAndWait command instead of just click, which will give some time to GUI to get refreshed or load before executing the next command.

so our new code will look like this:

//It clicks on the link and then waits for page to load dynamically for default time out value of selenium
$object->click_and_wait('link=www.somelink.com');
$object->type('id=username', 'USERNAME');

Now it works fine for both IE and Firefox.

However for applications that involve AJAX or changing display properties to block or none on click, this solution does not work.

For these, we need to either use explicit sleep($time_in_seconds) command [ Not suggested though ], or we can wait for some element on the refreshed GUI, which will make the page to wait before actually typing in the field.

Modified code will look something similar to as mentioned below:

$object->click('link=www.somelink.com');

//This will make the page to wait to load dynamically till HTML Field id=username becomes accessible and will solve our permission denied problem.

$object->wait_for_element('id=username');

$object->type('id=username');

Hope it helps. Let me know if you are unable to fix any Permission denied error with above mentioned solution.

Thanks,
QuickSilver1183
QuickSilver1183@gmail.com

getAttribute('class') does not work with IE but works with Firefox

While debugging a error in Selenium get_eval function, I faced a strange problem (at least strange to me and probably couple of other folks might be facing this STRANGE problem as well) where my following piece of code was throwing "Expected identifier" exception:

var class = row.getAttribute('class');

Here row was a table row, for which I wanted to get the class attribute to process further.

As class is a reserved word in Javascript and therefore it throws error when used as an identifier in IE, but it works fine on Firefox. Also getAttribute function works good with class in Firefox, but throws error in IE.

So the code was changed to something like below and it worked perfect for IE:

var class_name = row.getAttribute('className');

But now it gave error with Firefox as getAttribute function does not work good with 'className'

So I have to modify code to something like below and it worked perfectly for IE and Firefox:

//Works fine for Firefox
var class_name = row.getAttribute('class');

//If browser is IE, this piece of code returns the correct class name
class_name = class_name ? class_name : row.getAttribute('className');

Hope it helps.

Thanks,
QuickSilver1183
QuickSilver1183@gmail.com