简体   繁体   中英

Selenium webdriver upload download files using relative path

I would like to upload a file with selenium webdriver. I can do this in local machine using the absolute path of the file : String filename = "C:\\Windows\\Temp\\"+"templatePMT.html";. I'm using Eclipse and a maven project with a pom.xml and. I commit this project on SVN. I use Jenkins software to run the test of the Web app on IE8. Jenkins is deployed on Red Hat 5.0. ---- The problem is this : ---- how can I upload a file using a relative path instead of the absolute path ? The file is in resource folder of my project.

You should look at file detectors. http://saucelabs.com/resources/selenium-file-upload

You can use following code to get absolute path of the file and upload the content

String filePath = System.getProperty("user.dir") + "/src/res/test.pdf; driver.findElement(By.id("elementID")).sendKeys(filePath);

Also you can use find the element using cssSelector as well. Then the code would be; driver.findElement(By.cssSelector("input[id='elementId']")).sendKeys(filePath); See more regex here : Finding an element by partial id

Ref: Relative path of a file to upload a file

You could get the absolute path of the resource file:

URL resource = Main.class.getResource("/templatePMT.html");
String absolutePath = Paths.get(resource.toURI()).toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM