Webdriver, Maven, Selenium Grid 2 and File Upload on Remote Node (Selenium 2.16.1)

2,302 views
Skip to first unread message

Andreas

unread,
Jan 25, 2012, 12:21:20 PM1/25/12
to Selenium Users
Hi,
I have been surfing around attempting to look for an answer of how I
can do FileUpload on a Remote Node. I have found numerous suggestions;
using a Webdriver backed Selenium (http://code.google.com/p/selenium/
source/detail?r=10610), using a FileDetector and the (http://
groups.google.com/group/webdriver/browse_thread/thread/
2fb93423c8eae233) BUT I have not found a single full example for my
set up and am struggling to get it to work.

I have the following setup:

1) Images are in Maven test/resources and are available on the
classpath for the server running the unit tests
2) Hudson executes my mvn test target and the hub is available on the
same server as hudson.
3) A remote node running firefox is used to execute the job (I do get
logs like shown below on the node)

Logs on node
22:42:02.374 INFO [14] org.openqa.selenium.remote.server.DriverServlet
- Executing: [send keys: 5
org.openqa.selenium.support.events.EventFiringWebDriver
$EventFiringWebElement@a6919592, [/tmp/1327357083300/
upload5349320589334893179file/banner.jpg]] at URL: /session/
1327357083300/element/5/value)
22:42:02.389 DEBUG [44] org.apache.http.wire - >> "{"id":"{6af68b85-
b66f-4349-80ec-e7dd47d7eb32}","value":["/tmp/1327357083300/
upload5349320589334893179file/banner.jpg"]}"

4) The tests work fine locally and I also attempted to download them
using http in the test code but they images I am trying to upload
never makes it out to the node and stays on the hub.

I know there are a lot of file upload questions out there but it would
be great to hear if someone could point me to the "right" or even a
working solution.

Thanks in advance.

Krishnan Mahadevan

unread,
Feb 3, 2012, 4:36:03 AM2/3/12
to seleniu...@googlegroups.com
Andreas,
Here's a working example that could help you get started :

public static void showUploadDemo(URL remoteWebDriverURL, DesiredCapabilities dc){
RemoteWebDriver.setLogLevel(Level.INFO);
RemoteWebDriver rwd = new RemoteWebDriver(remoteWebDriverURL,dc);
LocalFileDetector detector = new LocalFileDetector();
File file = detector.getLocalFile("C:/Users/krmahadevan/testfile.txt");
rwd.setFileDetector(detector);
rwd.findElementByName("upfile").sendKeys(file.getAbsolutePath());
rwd.findElementByName("note").sendKeys("Test note");
rwd.findElementByXPath("//input[@value='Press']").click();
System.out.println("Page Title ==> " + rwd.getTitle());
System.out.println("Page source contents :\n " + rwd.getPageSource());
rwd.quit();
}

The output :

Feb 03, 2012 3:04:22 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: newSession
Feb 03, 2012 3:04:26 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: get
Feb 03, 2012 3:04:26 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: findElement
Feb 03, 2012 3:04:26 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: uploadFile
Feb 03, 2012 3:04:27 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: sendKeysToElement
Feb 03, 2012 3:04:27 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: findElement
Feb 03, 2012 3:04:27 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: sendKeysToElement
Feb 03, 2012 3:04:28 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: findElement
Feb 03, 2012 3:04:28 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: clickElement
Feb 03, 2012 3:04:28 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getTitle
Page Title ==> File Upload Results
Feb 03, 2012 3:04:29 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getPageSource
Page source contents :
 <html xmlns="http://www.w3.org/1999/xhtml"><head>

<title>File Upload Results</title>
</head><body>
<h1>File Upload Results</h1>

<p>You've uploaded a file.  Your notes on the file were:<br />
</p><blockquote>Test note</blockquote><br />
<p>The file's contents are:
</p><pre>dummy
</pre>
</body></html>
Feb 03, 2012 3:04:29 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: quit


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Krishnan Mahadevan

unread,
Sep 13, 2012, 1:46:21 AM9/13/12
to seleniu...@googlegroups.com
http://code.google.com/p/selenium/wiki/Grid2#Using_grid_to_run_tests 


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


On Thu, Sep 13, 2012 at 11:14 AM, Neeraj Sharma <mintu...@gmail.com> wrote:
Hi  Krishnan,

I am facing the same problem to upload a file in gmail and found your solution. Can you please explain how to define the  "URL remoteWebDriverURL, DesiredCapabilities dc" and how we can pass the firefox driver in below method. I am unable to pass the parameters in the method when calling it in the main method. Thanks in advance.

Neeraj   
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/Zdf5xxvMy94J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Neeraj Sharma

unread,
Sep 13, 2012, 2:01:35 AM9/13/12
to seleniu...@googlegroups.com
Thanks Krishnan Ji, will let you know if I am able to run the code or not.

Neeraj Sharma

unread,
Sep 13, 2012, 4:47:08 AM9/13/12
to seleniu...@googlegroups.com
Hi Krishnan,

I have setup the remote web driver as it is described in the links, but it is again giving me the exception "Error forwarding the new session Empty pool of VM for setup {platform=ANY, javascriptEnabled=true, browserName=firefox, version=}
Command duration or timeout: 607 milliseconds"..

Here is my main method code:

public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
String remoteWebDriverURL = null;
showUploadDemo(remoteWebDriverURL, capability);
}
Please correct me if i am doing anything wrong.
Thanks,
Neeraj

Krishnan Mahadevan

unread,
Sep 13, 2012, 4:58:16 AM9/13/12
to seleniu...@googlegroups.com
Please go through my blog post again. I have explained the reason behind this error showing up.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/sz08CGW8e58J.

Neeraj Sharma

unread,
Sep 13, 2012, 5:29:55 AM9/13/12
to seleniu...@googlegroups.com
I have gone through the blog again and i have a little confusion about  grid-startup-batch.bat , how we can create this file and in the above link google chrome driver are not available. There is one more thing which i am facing in java if i use this code
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName(DesiredCapabilities.internetExplorer());
URL url = new URL("http://localhost:4444/grid/register");
RemoteWebDriver driver = new RemoteWebDriver(url, capabilities);
then it display a error message on 2nd line. How we can remove this error message.
Neeraj

Krishnan Mahadevan

unread,
Sep 13, 2012, 5:33:13 AM9/13/12
to seleniu...@googlegroups.com
Use any editor to create a batch file on windows. What is the confusion in that ?

ChromeDriver should be downloaded from here :  http://code.google.com/p/chromedriver/downloads/list 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/gAs474lPEXUJ.

Peter Gale

unread,
Sep 13, 2012, 5:38:35 AM9/13/12
to Selenium Users
Neeraj

I think you should be using:

   capabilities.setCapability...

and not

 capabilities.setBrowser name.

The latter will accept a string, not a "capability" type object.

Peter


Date: Thu, 13 Sep 2012 02:29:55 -0700
From: mintu...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Webdriver, Maven, Selenium Grid 2 and File Upload on Remote Node (Selenium 2.16.1)
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/gAs474lPEXUJ.

Krishnan Mahadevan

unread,
Sep 13, 2012, 5:42:44 AM9/13/12
to seleniu...@googlegroups.com
Neeraj,
That was indeed an error in my post.

It should have been :

capabilities.setBrowserName(DesiredCapabilities.internetExplorer().getBrowserName());

Thanks for bringing it up. I have rectified it in my blog

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



For more options, visit https://groups.google.com/groups/opt_out.
 
 

Neeraj Sharma

unread,
Sep 13, 2012, 5:57:09 AM9/13/12
to seleniu...@googlegroups.com
Is it necessary to create a batch file, i mean we can also start the selenium server by using this command in command line "java -jar selenium-server-standalone-2.14.0.jar -role hub", I am new in testing to so idea about syntax of batch file.

Krishnan Mahadevan

unread,
Sep 13, 2012, 5:59:00 AM9/13/12
to seleniu...@googlegroups.com
Its totally upto you. Batch file is merely as a means for convenience.

Even running the command on the command prompt is also fine.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/qrjleWH2vtAJ.

Neeraj Sharma

unread,
Sep 13, 2012, 6:03:22 AM9/13/12
to seleniu...@googlegroups.com
 Used the command line server start and now i am getting this exception.

Exception in thread "main" org.openqa.selenium.WebDriverException: <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 500 Server Error</title>
</head>
<body>
<h2>HTTP ERROR: 500</h2>
<p>Problem accessing /grid/register/session. Reason:
<pre>    Server Error</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
                
</body>
</html>

The main method code is like this now.

public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
RemoteWebDriver driver = new RemoteWebDriver(url, capabilities);
}

Neeraj Sharma

unread,
Sep 13, 2012, 6:06:13 AM9/13/12
to seleniu...@googlegroups.com
And if i change the URL to http://localhost:4444/wd/hub this then it display another exception.

Error forwarding the new session Empty pool of VM for setup {browserName=firefox}

What is the meaning of these two URLs?

Krishnan Mahadevan

unread,
Sep 13, 2012, 6:15:41 AM9/13/12
to seleniu...@googlegroups.com
Neeraj,

Lets go over what we have so far :

1. To work with the Grid, you WOULD need to start both the Grid and the node. If you just have a Grid and NO node, then you are likely to see the error 
Error forwarding the new session Empty pool of VM for setup

2. The URL "http://localhost:4444/grid/register" is used by the node to register itself to the Grid. You shouldnt be using that in your test. If you use, you will see those errors

So I would suggest that you please go back and start everything from the scratch.

If you are not comfortable with batch files, then run the commands to start the grid in one command window, open up another command window and run the command to start the node in it.

Then run your test, with the URL referring to "http://localhost:4444/wd/hub"



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/m5PW-kKtXYwJ.

Neeraj Sharma

unread,
Sep 13, 2012, 6:40:33 AM9/13/12
to seleniu...@googlegroups.com
Thanks Sure, all things gone messy so i have to start again on this problem,
Can you please give me a brief idea about this remotewebdriver, i mean to say, how this remote web driver is able to upload a file but the simple web driver not, can you please provide any good link of remote webdriver and webdriver differences.

Krishnan Mahadevan

unread,
Sep 13, 2012, 6:43:02 AM9/13/12
to seleniu...@googlegroups.com
All flavors of WebDrivers WILL HELP with File upload.

The way in which you do file upload differs between RemoteWebDriver and all of the rest of the flavors of WebDrivers.

RemoteWebDriver is used ONLY when you are involving a grid. A grid is used when you want to abstract the execution environment from the 
local machine i.e., if you have the luxury of having a bunch of machines at your disposal and want to use those machines for running your tests
so that you can use your local machine for your browsing needs, you would opt for Grid.

This is my naive explanation !

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/c5Bi_KeiMQIJ.

Neeraj Sharma

unread,
Sep 13, 2012, 7:20:58 AM9/13/12
to seleniu...@googlegroups.com
I have also tried to use this code but the files is not uploaded,

RemoteWebElement element = (RemoteWebElement) driver.findElement(By.xpath("//div[2]/div/div/div/div/div/div/form/table/tbody/tr[6]/td[2]/span[1]"));
 String filePathToUse = new File("C:\\Users\\Neeraj\\Desktop\\Leh").getAbsolutePath();
 element.setFileDetector( new LocalFileDetector() );
 System.out.println(filePathToUse);
 element.sendKeys(filePathToUse);

it is returning me the absolute xpath but the file is not attaching, 

Krishnan Mahadevan

unread,
Sep 13, 2012, 7:26:24 AM9/13/12
to seleniu...@googlegroups.com
This works for me :

public static void main(String[] args) throws MalformedURLException, InterruptedException {
URL url = new URL("http://localhost:4444/wd/hub");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
RemoteWebDriver driver = new RemoteWebDriver(url, dc);
RemoteWebElement element = (RemoteWebElement)driver.findElement(By.name("upfile"));
File f = new File("C:/Users/krmahadevan/Desktop/text.txt");
element.setFileDetector(new LocalFileDetector());
element.sendKeys(f.getAbsolutePath());
WebElement ele = driver.findElement(By.xpath("//input[@value='Press']"));
ele.click();
System.out.println("Page URL after upload = " + driver.getCurrentUrl());
System.out.println(driver.getPageSource());
driver.quit();
}


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/OSNgp7__9DEJ.

Anji Prassana

unread,
Sep 13, 2012, 7:32:07 AM9/13/12
to seleniu...@googlegroups.com
Hi Krishnan,
  can you please explain the use of
  element.setFileDetector(new LocalFileDetector());
 Does this mean we are telling the script to look into a Local file system?What exactly is it's use and what others we may supply as an argument to setFileDetector() function?

Thanks&Regards
Anjaneyulu P
"The way you see the things is the way to think and is the way you react upon!!"

Krishnan Mahadevan

unread,
Sep 13, 2012, 7:34:39 AM9/13/12
to seleniu...@googlegroups.com
I would suggest that you please take a look at the implementation for the method :

org.openqa.selenium.remote.RemoteWebElement.sendKeys(CharSequence...)

That should help you understand more than what I can possibly explain :)


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



Anji Prassana

unread,
Sep 13, 2012, 7:39:15 AM9/13/12
to seleniu...@googlegroups.com
Sure thanks Krishnan.

Neeraj Sharma

unread,
Sep 14, 2012, 8:02:08 AM9/14/12
to seleniu...@googlegroups.com
Hi Krishnan ji,

I have updated the batch file with the help of my friend and now  the below code is running till entering the absolute path in the text box, after that it is not able to press the button. There are some confusions in my mind:-

1. In the batch file you have used the google chrome driver but in the program you are using the firefox and internet explorer driver. Is this is the main reason of the below code is not running properly.

2. I have also tried this solution to my current problem (To upload a file in gmail compose box) but  file is not uploading and it send the email without the attachment. What is the main reason now for that problem.

Neeraj Sharma.

Krishnan Mahadevan

unread,
Sep 14, 2012, 8:14:50 AM9/14/12
to seleniu...@googlegroups.com
I think I have shared all that I know.. so its time to give up ! Hopefully some else would be able to answer you.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


On Fri, Sep 14, 2012 at 5:32 PM, Neeraj Sharma <mintu...@gmail.com> wrote:
Hi Krishnan ji,

I have updated the batch file with the help of my friend and now  the below code is running till entering the absolute path in the text box, after that it is not able to press the button. There are some confusions in my mind:-

1. In the batch file you have used the google chrome driver but in the program you are using the firefox and internet explorer driver. Is this is the main reason of the below code is not running properly.
The batch file was to ensure that even automation runs for Google chrome would be supported on the node. That batch file and my standalone demo code has got nothing in common.
 

2. I have also tried this solution to my current problem (To upload a file in gmail compose box) but  file is not uploading and it send the email without the attachment. What is the main reason now for that problem.

Unless you are working for google, I dont think you should be trying to automate anything on google or gmail for that matter.
I would suggest that you try out on your Application under test. If you are just using all of this for learning purposes, then I would suggest that you either create a local web application
and host it on your local tomcat server and then play around with it, or choose a simpler website. Gmail is much more complicated than you think and I am running out of options as suggestions

 
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/JRqgOMhb76wJ.

Neeraj Sharma

unread,
Sep 14, 2012, 8:18:20 AM9/14/12
to seleniu...@googlegroups.com
Thanks a lot Sir for your replies, Can I use this URL for demo purpose  http://tinypic.com/ ?

Pavan

unread,
Sep 14, 2012, 8:21:07 AM9/14/12
to seleniu...@googlegroups.com
How is the node connected to Hudson. I mean slave can be started automatically through the Services and also by slave headless start [ by typing java jar -slave.jar XXXXXXXXXX command ] in a command prompt.[https://wiki.jenkins-ci.org/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+machines]
Sometimes, files do not get copied to a node from a shared network path.

Please try to connect the node through JNLP command line option and re execute the tests.

Thanks,
Pavan

Krishnan Mahadevan

unread,
Sep 14, 2012, 8:22:44 AM9/14/12
to seleniu...@googlegroups.com
Pavan,
I dont think you need any of that. RemoteWebElement by itself can do this automatically for you, to the best of my knowledge !

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/yMEZwbodXmcJ.

Neeraj Sharma

unread,
Sep 18, 2012, 6:48:49 AM9/18/12
to seleniu...@googlegroups.com
I have tried to automate the another site other than gmail and its works fine without using the remote driver too. May be gmail is very much complex.:0


On Friday, 14 September 2012 17:45:02 UTC+5:30, Krishnan wrote:
Reply all
Reply to author
Forward
0 new messages