Skip to content
Daniel Gempesaw edited this page Feb 4, 2016 · 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()");

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

use Selenium::Remote::Driver;
use Selenium::Waiter qw/wait_until/;

my $driver = Selenium::Remote::Driver->new;

$driver->get('https://www.google.com');
wait_for_page_to_load();

sub wait_for_page_to_load {
    my ($timeout) = @_;
    
    return wait_until { 
        $driver->execute_script("return document.readyState") eq 'complete' 
    }, timeout => $timeout;
}

Interact with Windows modal dialog

While testing web applications sometimes we encounter Windows popup dialogs (modal windows) that block operation until the dialog is cleared. Below is how I solved this issue. Please note that there are other options out there (e.g. AutoIt), however, I decided against calling an external exe and opted for integrating the tried and true Win32::GuiTest module for this task; mainly because it is written in the same language as the bindings and so there is no need to call external executables as well as learning a new "language", simply call a method.

NOTE: The workhorse of this solution is the SendKeys call.

In your test script:

my $win_id = 0;
my $win_title = 'Test window title';
my $win_class = 'Class name';

$driver->key_press_native( $win_id, $win_title, $win_class, 'ENTER' ); 

In the Driver.pm OR in your page object:

sub key_press_native {

    my ( $self, $win_id, $win_title, $win_class, $keycode ) = @_;

    use Win32::GuiTest qw(FindWindowLike GetWindowText SetForegroundWindow SendKeys);
  
    $Win32::GuiTest::debug = 0; # Set to "1" to enable verbose mode
    # First find the window of interest.
    my @windows = FindWindowLike( $win_id, "^$win_title", "^$win_class\$" );

    # Then we iterate through that list and send the "keys" to any matching window.
    for (@windows) {
        SetForegroundWindow($_);
        SendKeys("{$keycode}");
        Custom::TagSubs::wait_for(2);
    }
    return;
}

As you can see from the above, the solution requires the test developer to know the pertinent details of the window we will be interacting with. Win32::GuiTest comes with a tool found in Recoder\Win32GuiTest.exe which you can use to get "Windo Hints" (i.e. WinClass, WinTitle, etc).

Some caveats:

  • The solution uses the SetForegroundWindow call which means that it does require the window being interacted with to be able to be brought to the foreground.