Skip to content
aivaturi edited this page Mar 2, 2012 · 6 revisions

Here are few snippets that we think might be useful for you while working with these bindings.

IE SSL cert override

$driver->get("javascript:document.getElementById('overridelink').click()");

IE file upload

$driver->find_element($objXId)->send_keys($objValue);

Here, $objXId is the file input element & $objValue is the full path to the file that you're uploading


Workarounds for click sometimes not working on IE

To click link/object send "\n"

$driver->find_element($object, "<locator type>")->send_keys("\n");

or

use Selenium::Remote::WDKeys;
...
$driver->find_element($object, "<locator type>")->send_keys(KEYS->{'enter'});

To generate onclick events on "radio" & "checkbox", send " ":

# notice the extra space between the quotes
$driver->find_element("$objName", "<locator_type>")->send_keys(" ");  # simulates focus & spacebar

To focus on an element:

$driver->find_element("$objName", "<locator_type>")->send_keys("");  # send blank value

Inject JS

Sometimes you want to inject some JS to the existing page, for e.g. to add json support to do some processing. In our particular case, we offload all XPATH based table processing to javascript on the page, which we inject at run time. This way you can speed up processing of data as you minimize the back & forth between the bindings & the remote server.

my $js = (
    "_src=\\"https://raw.github.com/douglascrockford/JSON-js/master/json2.js\\";
    _document=document;
    _my_script=_document.createElement('SCRIPT');
    _my_script.type='text/javascript';_my_script.src=_src;
    return _document.getElementsByTagName('head')[0].appendChild(_my_script);"
);
$driver->execute_script($js);

In the above sample script what you're doing is modifying the DOM & injecting json2.js.


Wait For Page To Load

sub wait_for_page_to_load
{
    my ($self, $timeout) = @_;
    my $ret = 0;
    my $sleeptime = 2000;  # milliseconds

    $timeout = (defined $timeout) ? $timeout : 30000 ;
    do {
        sleep ($sleeptime/1000);      # Sleep for the given sleeptime
        $timeout = $timeout - $sleeptime;
    } while (($driver->execute_script("return document.readyState") ne 'complete') && ($timeout > 0));
    if ($driver->execute_script("return document.readyState") eq 'complete') {
         $ret = 1;
    }
    return $ret;
}