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

对于上传文件的用例,首先需要找到上传文件的对象,然后直接往这个对象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>


最新内容请见作者的GitHub页:http://qaseven.github.io/

上一篇:《Outlook时间整理术》一让我们开始改变这一切!


下一篇:scponly 密钥无密码scp登录