Take the 2-minute tour ×

I have the path to my XML file on my computer, but how can I use selenium (web automation tool) to inject the XML file ?

Usually how it is done (manually) is navigate to the URL and COPY AND PASTE the entire XML text into the provided text box..

Any ideas how to inject the file using automation ? There is no way to "drag" the XML file to the text box and I believe the way I'm thinking that it will work is very complicated.

share|improve this question
What's it being used for? I highly doubt that physically injecting it into the page is the way to go. –  Arran Dec 18 '12 at 19:14
add comment

3 Answers

up vote 1 down vote accepted

I think this is actually what you want -

File xml = new File("xmlpath");

String url = xml.getAbsolutePath();
url = url.replace('\\', '/');
url = url.replace(" ", "%20");

String actual = "file:/" + url;


Then you should be able to get the xml using String theXML = selenium.getText("//rootxmlnode"); Then do what you will with it.

share|improve this answer
Author of question asked, how he could inject XML file, not evaluate it. –  Andremoniy Dec 18 '12 at 14:50
Strange and complicated method. Your first variant of answer was closer to the idea. –  Andremoniy Dec 18 '12 at 15:00
And this was accepted as the answer. Noted @Andremoniy . –  sircapsalot Dec 21 '12 at 1:40
add comment

Selenium tool allows you create automatic generated code in Java. So, you need to place in the provided text box any text and generate this Java-test code. Next step is a modifying of generated test. You have to manually write simplest code, which will read your XML file, get it contents and paste into text box. Last thing is replacement (in generated Java code of test!) of mentioned above text-block to the contents of read XML.

Simplest way for reading file in String is using Apache commons-io library. For example: FileUtils.readFileToString(File file, String encoding) gives your String object with contents of file.

share|improve this answer
add comment

Check out the topic of Data Driven Testing to get you started. Something like this should get you going.

share|improve this answer
add comment

Your Answer


