天天看點

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: [email protected]

*/

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/