您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium2.0功能測試之文件上傳(Java版)
作者:網(wǎng)絡轉(zhuǎn)載 發(fā)布時間:[ 2013/10/25 15:32:29 ] 推薦標簽:

  對于上傳文件的用例,首先需要找到上傳文件的對象,然后直接往這個對象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>

 

相關鏈接:
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd