Selenium+Webdriver完整解決方案
3.組合鍵使用方法
//標簽頁的切換,組合按鍵
public void tabSwitch() throws Exception {
for(String windowId: driver.getWindowHandles()){
driver.switchTo().window(windowId);
if(driver.getTitle().contains("title2"))
break;
}
action.keyDown(keys.CONTROL).sendkeys(keys.TAB).keyUp(keys.CONTROL).perform();//按下control,按tab,松開control
}
六、下拉框的操作
Select selectshow = new Select(driver.findElement(By.id("test")));
int selectTotal = selectshow.getOptions().size(); //取得selectshow總長度,隨機下拉值
int index = random.nextInt(selectTotal);
selectshow.selectByIndex(index);
selectshow.selectByVisibleText("a");
selectshow.selectByIndex(2);
七、截圖
//現(xiàn)場截圖
//設置保存路徑
String path = System.getProperty("user.dir") + "/screenshot/";
//設置截圖的文件名:隨機數(shù),使用當前時間,建議把模塊名或測試用例編號附加上去
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd_hhmmss");
String time = formater.format(new Date().getTime());
String name = "Userstory(模塊名)_add(測試用例)_" + time +".png";
TakesScreenshot tss = (TakesScreenshot)driver; //強制轉換為TakesScreenshot類型
File image = tss.getScreenshotAs(OutputType.FILE);//java i/o的類
FileUtils.copyFile(image, new File(path + name));
八、上傳文件
1.用webDriver的sendKeys
//driver.findElement(By.id("upload")).click(); //不執(zhí)行打開命令,直接執(zhí)行下面這句
driver.findElement(By.id("upload")).sendkeys("c://a.txt");//直接輸入到無素里
2.用JAVA自帶的Robot按鍵功能操作
driver.findElement(By.id("upload")).click();//打開上傳窗口
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_C);//用鍵盤輸入C
robot.keyRelease(KeyEvent.VK_C);//用鍵盤輸入C
robot.keyPress(KeyEvent.VK_COLON); //輸入完整的路徑,再按回車進行上傳。
robot.keyRelease(KeyEvent.VK_COLON);
3.使用AutoIt工具上傳
上傳詳解:http://www.cnblogs.com/baihuitestsoftware/articles/5730133.html
1)用AutoIt生成exe
;ControlFocus("title","text",controlID) Edit1=Edit instance 1
ControlFocus("選擇要加載的文件", "","Edit1")
; Wait 10 seconds for the Upload window to appear
WinWait("[CLASS:#32770]","",10)
; Set the File name text on the Edit field
ControlSetText("選擇要加載的文件", "", "Edit1", "D:\upload_file.txt")
Sleep(2000)
; Click on the Open button
ControlClick("選擇要加載的文件", "","Button1");
2)python調用例子
#coding=utf-8
from selenium import webdriver
import os
driver = webdriver.Firefox()
#打開上傳功能頁面
file_path = 'file:///' + os.path.abspath('upfile.html')
driver.get(file_path)
#點擊打開上傳窗口
driver.find_element_by_name("file").click()
#調用upfile.exe上傳程序
os.system("D:\upfile.exe")
driver.quit()
九、WebDriver執(zhí)行JavaScript
1)簡單實現(xiàn)調用js
WebDriver driver = new FireFoxDriver();
JavaScriptExecutor jse = (JavaScriptExecutor)driver;
jse.executeScript("usernameVar=document.getElementById('username');usernameVar.value='admin';");
jse.executeScript("passwordVar=document.getElementById('password');passwordVar.value='admin';");
jse.executeScript("document.getElementById('login').click();");
2)優(yōu)化后調用js
WebDriver driver = new FireFoxDriver();
JavaScriptExecutor jse = (JavaScriptExecutor)driver;
String jsContent = "usernameVar=document.getElementById('username');"
+"usernameVar.style.borderWidth='3px';" //在輸入用戶名時加入,邊框3像素,紅色
+"usernameVar.style.borderColor='#f30';" //
+"usernameVar.value='admin';"
+"passwordVar=document.getElementById('password');"
+"passwordVar.value='admin';"
+"document.getElementById('login').click();";
jse.executeScript(jsContent);
//------------------
jsContent = "setKEContent('content', 'This is content!')"; //輸入內容時,調用js里面的方法進行操作,content是ID
jse.executeScript(jsContent);
十、webdriver在不同瀏覽器上運行
public class BrowserUsage{
WebDriver driver;
public static void main (String[] args){
BrowserUsage bu = new BrowserUsage();
bu.firefox();
bu.ie();
bu.chrome();
}
public void firefox(){
System.setProperty("webdriver.firefox.bin", "C:\firefoxdir\firefox.exe");
driver = new FireFoxDriver();
}
public void ie(){
String path = System.getProperty("user.dir") + "lib"; //取得當前項目的目錄
System.setProperty("webdriver.ie.driver", path + "IEDriverServer.exe"); //設置IEDriver的路徑
DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
dc.setCapability("ignoreProtectedModeSettings",true);
driver = new InternetExplorerDriver(dc);
}
public void chrome(){
String path = System.getProperty("user.dir") + "lib";
System.setProperty("webdriver.chrome.driver", path + "/chromedriver.exe"); //設置IEDriver的路徑
driver = new ChromeFoxDriver();
}
}