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);
七、截图
//现场截图
//设置保存路径
String path = System.getProperty("user.dir") + "/screenshot/";
//设置截图的文件名:随机数,使用当前时间,建议把模块名或测试用例编号附加上去
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(); //不执行打开命令,直接执行下面这句
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执行JavaScript
1)简单实现调用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)优化后调用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();
}
}