Chaoui05
Chaoui05

Reputation: 288

How to upload a file in a test through Open Windows using Selenium::Remote::Driver Perl package

Below are the additional bullets : I can get my "button" with following code :

 $elem = $driver->find_element('//*[@id="file_uploader"]/div/div/div/div[1]/div[1]/div/span');
     $driver->mouse_move_to_location(element => $elem); # xoffset => x, yoffset => y
     $driver->click_ok('LEFT');
     $driver->pause(3000);

But after that, I can't get anything in Opened Window which appears . How is it possible to upload a file?

PS: Here screenshot of my Developper Tools.

In Blue

Thanks in advance !

Upvotes: 0

Views: 140

Answers (2)

Chaoui05
Chaoui05

Reputation: 288

In fact, it's more specifical. I have two ways to upload a file in my web application :

First user can use a button 'select file'.

Second you can drop directly file . By second way, and using "upload_file" method in Selenium::Remote::Driver package as it was proposed here, it's possible. I do that :

my $fname = "D:/dev/tests/selenium/phantomjs-2.1.1-windows.zip";
my $remote_fname = $driver->upload_file( $fname );
    my $element = $driver->find_element( '//*[@id="file_uploader"]/div/div/div/div[1]/div[2]/input[@class="dx-fileuploader-input"]' );
    $element->send_keys( $remote_fname );

But not with my "button".

How is it possible ? I'm forwarding you additional screenshots here :

In red 'button' and blue 'drop file'

Upvotes: 0

RemcoW
RemcoW

Reputation: 4336

Selenium can not deal with OS dialogs. Therefore there is no way to deal with this dialog. This means you need to upload a file without opening it. Luckily Selenium allows the user to send the filepath to the file input. This will upload the file.

In order to make it work you first need to find the input element instead of the button. It'll look something like:

<input type='file'>

Once you've found this element you can send the filepath to it like you would send any text to a textfield. I'm not sure how to do this in perl, but in Python you can achieve it like this:

element.send_keys('path/to/file')

You'll probably know yourself what the perl equivalent is.

Upvotes: 2

Related Questions