Create and upload a file on selenium grid
Asked Answered
R

2

7

One of tthe test cases that I have is to upload a file to the application. Witouth grid this can be easily done by creating a bogus file and get absolute path of the file and fill the input field and click upload. However when I am using grid, the file is not on the machine that hosted the node. Is there anyway to either send the file to the node or tell the node to create the file and get the absolute path.

Recline answered 25/4, 2013 at 2:15 Comment(0)
W
13

It's actually pretty simple once you know how, just set a local file detector.

import org.openqa.selenium.remote.LocalFileDetector
import org.openqa.selenium.remote.RemoteWebDriver

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());
driver.setFileDetector(new LocalFileDetector())

Then just upload as normal and Selenium will fire the file across the wire to the node.

Wanitawanneeickel answered 26/4, 2013 at 20:22 Comment(4)
does the file created in the node or locally?Recline
In the above example the file would be on the machine running the tests. The file will then be sent over the wire to the grid, and then on to the node so that the node can upload the file.Wanitawanneeickel
Impressed by developers' humor - there is only two FileDetector implementations: one is mentioned LocalFileDetector, the other is UselessFileDetector "that never finds anything" :)Hassanhassell
nitpicking - setFileDetector can not be called on driver object but driver needs to be downcast to RemoteWebDriver - ((RemoteWebDriver)driver).setFileDetector(new LocalFileDetector());Dealing
M
-2

You can do it in normal way as you create a file on local machine. See below,

File file = new File("\\\\00.00.00.00\\c$\\somefile.txt");
file.createNewFile();

This will create a file called somefile.txt in 'C' drive on the remote machine. Change the IP address to your remote machine.

Maurits answered 26/4, 2013 at 10:56 Comment(3)
hmm.. but when i run it in the selenium grid then the node is kinda random right? I don't know which node it is going to runRecline
You can write a method and call it in test methods where ever required. It's independent of nodes.Maurits
unless remote machine is not windows hence does not have neither the 'C' drive nor samba installedHassanhassell

© 2022 - 2024 — McMap. All rights reserved.