Selenium2.0功能测试之文件上传(Java版)

Isoke ·
更新时间:2024-09-21
· 548 次阅读

  对于上传文件的用例,首先需要找到上传文件的对象,然后直接往这个对象sendKeys,传入需要上传文件的正确路径,路径和相对路径都可以的,但是上传的文件必须存在,否则会报错。下面直接看例子吧:

package org.coderinfo.demo; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; /** * @author Coderinfo * @Email: coderinfo@163.com */ public class UpdateFile { private static final String URL = "file:///C:/Documents and Settings/user/Desktop/Selenium/update_file.html"; public static void main(String[] args) { WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get(URL); // Use API:sendKeys to update file driver.findElement(By.id("update")).sendKeys("C:/Documents and Settings/user/Desktop/Selenium/update_file.html"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } driver.close(); } }

  本测试的页面源码:

<!DOCTYPE html> <html> <head> <title>Update File</title> <style> h2 { text-align : center } </style> </head> <body> <h2>Update File Demo</h2> <form> Update File:<input type="file" id="update"/> </form> </body> </html>
Selenium2.0功能测试之Close browser



java版 JAVA 功能测试 selenium 文件上传 测试

需要 登录 后方可回复, 如果你还没有账号请 注册新账号