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

发表于:2013-10-25 11:01

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:CoderInfo    来源:CoderInfo的专栏

  对于上传文件的用例,首先需要找到上传文件的对象,然后直接往这个对象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功能测试之Web元素的操作
《2023软件测试行业现状调查报告》独家发布~

精彩评论

  • lvzeting
    2013-10-25 14:37:39

    有直接的标准上传元素的操作方法的

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号