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

本文介绍使用Selenium进行文件上传的方法,通过示例代码展示了如何定位上传按钮并发送文件路径,适用于自动化测试中文件上传场景。
对于上传文件的用例,首先需要找到上传文件的对象,然后直接往这个对象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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值