selenium20中文帮助文档.docx
《selenium20中文帮助文档.docx》由会员分享,可在线阅读,更多相关《selenium20中文帮助文档.docx(18页珍藏版)》请在冰豆网上搜索。
selenium20中文帮助文档
Selenium2.0帮助文档
第1章Webdirver基础2
1.1 下载selenium2.0的lib包2
1.2 用webdriver打开一个浏览器2
1.3 打开测试页面2
1.4 GettingStarted2
第2章Webdirver对浏览器的支持4
2.1 HtmlUnitDriver4
2.2 FireFoxDriver4
2.3 InternetExplorerDriver4
第3章使用操作4
3.1 如何找到页面元素4
3.1.1ByID5
3.1.2ByName5
3.1.3ByXPATH5
3.1.4ByClassName5
3.1.5ByLinkText5
3.2 如何对页面元素进行操作6
3.2.1输入框(textfieldortextarea)6
3.2.2下拉选择框(Select)6
3.2.3单选项(RadioButton)6
3.2.4多选项(checkbox)7
3.2.5按钮(button)7
3.2.6左右选择框7
3.2.7弹出对话框(Popupdialogs)7
3.2.8表单(Form)8
3.2.9上传文件(UploadFile)8
3.2.10 Windows和Frames之间的切换8
3.2.11 拖拉(DragandDrop)8
3.2.12 导航(NavigationandHistory)8
3.3 高级使用9
3.3.1改变useragent9
3.3.2读取Cookies9
3.3.3调用JavaScript9
3.3.4Webdriver截图10
3.3.5页面等待10
第4章RemoteWebDriver10
4.1 使用RemoteWebDriver10
4.2 SeleniumServer11
4.3 HowtosetFirefoxprofileusingRemoteWebDriver11
第5章封装与重用12
第6章在selenium2.0中使用selenium1.0的API14
第1章Webdirver基础
1.1 下载selenium2.0的lib包
官方UserGuide:
http:
//seleniumhq.org/docs/
1.2 用webdriver打开一个浏览器
我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器。
但是做页面的测试,速度通常很慢,严重影响持续集成的速度,这个时候建议使用HtmlUnit,不过HtmlUnitDirver运行时是看不到界面的,对调试就不方便了。
使用哪种浏览器,可以做成配置项,根据需要灵活配置。
打开firefox浏览器:
//CreateanewinstanceoftheFirefoxdriver
WebDriverdriver=newFirefoxDriver();
打开IE浏览器
//CreateanewinstanceoftheInternetExplorerdriver
WebDriverdriver=newInternetExplorerDriver();
打开HtmlUnit浏览器
//CreateanewinstanceoftheInternetExplorerdriver
WebDriverdriver=newHtmlUnitDriver();
1.3 打开测试页面
对页面对测试,首先要打开被测试页面的地址(如:
),webdriver提供的get方法可以打开一个页面:
//AndnowusethedrivertovisitGoogle
driver.get("");
1.4 GettingStarted
packageorg.openqa.selenium.example;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;
publicclassSelenium2Example {
publicstaticvoidmain(String[]args){
//CreateanewinstanceoftheFirefoxdriver
//Noticethattheremainderofthecodereliesontheinterface,
//nottheimplementation.
WebDriverdriver=newFirefoxDriver();
//AndnowusethistovisitGoogle
driver.get("");
//Alternativelythesamethingcanbedonelikethis
//driver.navigate().to("");
//Findthetextinputelementbyitsname
WebElementelement=driver.findElement(By.name("q"));
//Entersomethingtosearchfor
element.sendKeys("Cheese!
");
//Nowsubmittheform.WebDriverwillfindtheformforusfromtheelement
element.submit();
//Checkthetitleofthepage
System.out.println("Pagetitleis:
"+driver.getTitle());
//Google'ssearchisrendereddynamicallywithJavaScript.
//Waitforthepagetoload,timeoutafter10seconds
(newWebDriverWait(driver,10)).until(newExpectedCondition(){
publicBooleanapply(WebDriverd){
returnd.getTitle().toLowerCase().startsWith("cheese!
");
}
});
//Shouldsee:
"cheese!
-GoogleSearch"
System.out.println("Pagetitleis:
"+driver.getTitle());
//Closethebrowser
driver.quit();
}
}
第2章Webdirver对浏览器的支持
2.1 HtmlUnitDriver
优点:
HtmlUnitDriver不会实际打开浏览器,运行速度很快。
对于用FireFox等浏览器来做测试的自动化测试用例,运行速度通常很慢,HtmlUnitDriver无疑是可以很好地解决这个问题。
缺点:
它对JavaScript的支持不够好,当页面上有复杂JavaScript时,经常会捕获不到页面元素。
使用:
WebDriverdriver=newHtmlUnitDriver();
2.2 FireFoxDriver
优点:
FireFoxDirver对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFoxDriver都可以模拟。
缺点:
启动很慢,运行也比较慢,不过,启动之后Webdriver的操作速度虽然不快但还是可以接受的,建议不要频繁启停FireFoxDriver。
使用:
WebDriverdriver=newFirefoxDriver();
Firefoxprofile的属性值是可以改变的,比如我们平时使用得非常频繁的改变useragent的功能,可以这样修改:
FirefoxProfileprofile=newFirefoxProfile();
profile.setPreference("general.useragent.override","someUAstring");
WebDriverdriver=newFirefoxDriver(profile);
2.3 InternetExplorerDriver
优点:
直观地模拟用户的实际操作,对JavaScript提供完善的支持。
缺点:
是所有浏览器中运行速度最慢的,并且只能在Windows下运行,对CSS以及XPATH的支持也不够好。
使用:
WebDriverdriver=newInternetExplorerDriver();
第3章使用操作
3.1 如何找到页面元素
Webdriver的findElement方法可以用来找到页面的某个元素,最常用的方法是用id和name查找。
下面介绍几种比较常用的方法。
3.1.1ByID
假设页面写成这样:
那么可以这样找到页面的元素:
通过id查找:
WebElementelement=driver.findElement(By.id("passwd-id"));
3.1.2ByName
或通过name查找:
WebElementelement=driver.findElement(By.name("passwd"));
3.1.3ByXPATH
或通过xpath查找:
WebElementelement=driver.findElement(By.xpath("//input[@id='passwd-id']"));
3.1.4ByClassName
假设页面写成这样:
Cheddar