Karate: error in uploading a file through selenium webdriver in a Selenium grid architecture

Created on 22 Mar 2020  路  6Comments  路  Source: intuit/karate

Karate UI fails when trying to upload a file through selenium webdriver in a Selenium grid architecture.

All the details, the description of the issue, the instructions and a possible fix are in the README file inside the test project in attachment: test-upload-files-ui-karate.zip

The issue is linked to the question made in [https://stackoverflow.com/questions/60673922/]

help wanted

Most helpful comment

@forlixdev I'm using the following one-liner helper to determine the full path:

* def getAbsolutePath = function(fname) { p = karate.properties['user.dir']+"\\"+fname; return (karate.os.type == 'windows') ? p : p.replace(/\\/g, "/") }
* def example = getAbsolutePath("uploads/files.jpg")

@ptrthomas I have no direct experience with Selenium grid, but the LocalFileDetector does little more than validating the given path is an existing local file.

The RemoteWebElement object of Selenium here triggers an upload for each valid file that sends an "uploadFile" command with the zipped contents of the file. I guess this will send it across the grid to the node. See here: https://github.com/SeleniumHQ/selenium/blob/2a8d807214bf024c4897e97a5dd92bdc725b0b56/java/client/src/org/openqa/selenium/remote/RemoteWebElement.java#L111

Since Karate talks to (grid) webdriver directly by sending commands to the webDriverUrl, it never uploads anything to the node.

All 6 comments

@forlixdev thanks for the details. for now I'm tagging this as help wanted - I did a quick check as to how this LocalFileDetector works, but could not, so if anyone can provide pointers on how to implement this in Karate - that would help. Of course ideally we would love a PR :)

That said - note that Karate has an alternative architecture for distributed (or grid based) testing and you can find details here: https://github.com/intuit/karate/wiki/Distributed-Testing - and the advantage over Selenium is that your test always runs on the same node where the browser is.

Also note that we are working on karate-robot which can be used as a work-around when traditional file-upload automation fails, see: https://twitter.com/ptrthomas/status/1215534821234995200

I'll try to have a look on the source code and try to help even if to be honest my knowledge of Java is not as complete a developer one (I'm a tester).

Anyway in my java test I had to separate the behaviour when using a local or a remote webdriver, instantiating two different objects (example below). Maybe this workaround can by applied to karate as well depending on the webDriverUrl property in karate driver config, i.e. if webDriverUrl property is set it can be instantiated a RemoteWebDriver otherwise a proper webdriver (ChromeDriver, FirefoxDriver, etc)

WebDriver wd;
String grid = System.getProperty("grid", "false");
ChromeOptions chromeOptions = new ChromeOptions();
if (grid.equalsIgnoreCase("true")) {
   RemoteWebDriver rwd = new RemoteWebDriver(new URL(gridUrl), DesiredCapabilities.chrome());
   rwd.setFileDetector(new LocalFileDetector());
   wd = rwd;
} else {
   wd = new ChromeDriver(chromeOptions);
}

@forlixdev thanks ! tagging @paaco and @djangofan if they have any experience in this area that can help. I am trying to understand what exactly does rwd.setFileDetector(new LocalFileDetector()); do - since Karate doesn't use a RemoteWebDriver at all.

Yes, I remember that I was forced to introduce a RemoteWebDriver in my class to let it work. I'll dig into the Selenium library to see if there's a workaround anyway.

@forlixdev I'm using the following one-liner helper to determine the full path:

* def getAbsolutePath = function(fname) { p = karate.properties['user.dir']+"\\"+fname; return (karate.os.type == 'windows') ? p : p.replace(/\\/g, "/") }
* def example = getAbsolutePath("uploads/files.jpg")

@ptrthomas I have no direct experience with Selenium grid, but the LocalFileDetector does little more than validating the given path is an existing local file.

The RemoteWebElement object of Selenium here triggers an upload for each valid file that sends an "uploadFile" command with the zipped contents of the file. I guess this will send it across the grid to the node. See here: https://github.com/SeleniumHQ/selenium/blob/2a8d807214bf024c4897e97a5dd92bdc725b0b56/java/client/src/org/openqa/selenium/remote/RemoteWebElement.java#L111

Since Karate talks to (grid) webdriver directly by sending commands to the webDriverUrl, it never uploads anything to the node.

closing for now, note that karate.toAbsolutePath() has been implemented and will be available in 0.9.6.RC3 onwards

we have the following options for file upload:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sadiqkassamali picture sadiqkassamali  路  4Comments

ianrenauld picture ianrenauld  路  4Comments

IsaoTakahashi picture IsaoTakahashi  路  3Comments

khanguyen88 picture khanguyen88  路  3Comments

JuliaSv picture JuliaSv  路  5Comments