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> |
posted on 2013-10-31 11:21 順其自然EVO 閱讀(247) 評論(0) 編輯 收藏 所屬分類: selenium and watir webdrivers 自動化測試學習