资源预览内容
第1页 / 共46页
第2页 / 共46页
第3页 / 共46页
第4页 / 共46页
第5页 / 共46页
第6页 / 共46页
第7页 / 共46页
第8页 / 共46页
第9页 / 共46页
第10页 / 共46页
亲,该文档总共46页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Selenium2.0帮助文档第1章 Webdirver基础 31.1 下载selenium2.0的lib包 31.2 用webdriver打开一个浏览器 31.3 打开测试页面 31.4 GettingStarted 3第2章 Webdirver对浏览器的支持 52.1 HtmlUnit Driver 52.2 FireFox Driver 52.3 InternetExplorer Driver 5第3章 使用操作 53.1如何找到页面元素 53.1.1 By ID 63.1.2 By Name 63.1.3 By XPATH 63.1.4 By Class Name 63.1.5 By Link Text 63.1.6 By tagName 73.1.7 By cssSelector 73.2如何对页面元素进行操作 73.2.1 输入框(text field or textarea) 73.2.2 下拉选择框(Select) 73.2.3 单选项(Radio Button) 83.2.4 多选项(checkbox) 83.2.5 按钮(button) 83.2.6 左右选择框 93.2.7 弹出对话框(Popup dialogs) 93.2.8 表单(Form) 93.2.9 上传文件 (Upload File) 93.2.10 Windows 和 Frames之间的切换 93.2.11 拖拉(Drag andDrop) 103.2.12 导航 (Navigationand History) 103.3 高级使用 103.3.1 改变user agent 103.3.2 读取Cookies 103.3.3 调用Java Script 113.3.4 Webdriver截图 113.3.5 页面等待 11第4章 RemoteWebDriver 124.1 使用RemoteWebDriver 124.2 SeleniumServer 134.3 How to setFirefox profile using RemoteWebDriver 13第5章 封装与重用 13第6章 在selenium2.0中使用selenium1.0的API 15第7章 鼠标事件 17第1章 Webdirver基础1.1 下载selenium2.0的lib包http:/docs.seleniumhq.org/download/ 官方UserGuide:http:/seleniumhq.org/docs/1.2 用webdriver打开一个浏览器我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器。但是做页面的测试,速度通常很慢,严重影响持续集成的速度,这个时候建议使用HtmlUnit,不过HtmlUnitDirver运行时是看不到界面的,对调试就不方便了。使用哪种浏览器,可以做成配置项,根据需要灵活配置。打开firefox浏览器:/Create a newinstance of the Firefox driverWebDriver driver = new FirefoxDriver(); 打开IE浏览器/Create a newinstance of the Internet Explorer driverWebDriver driver = new InternetExplorerDriver (); 打开HtmlUnit浏览器/Createa new instance of the Internet Explorer driverWebDriverdriver = new HtmlUnitDriver();1.3 打开测试页面对页面对测试,首先要打开被测试页面的地址(如:http:/www.google.com),web driver 提供的get方法可以打开一个页面:/ And now use thedriver to visit Googledriver.get(http:/www.google.com);1.4 GettingStartedpackage org.openqa.selenium.example;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait; public class Selenium2Example public static voidmain(String args) / Create a newinstance of the Firefox driver/ Notice that theremainder of the code relies on the interface, / not the implementation.WebDriver driver = newFirefoxDriver(); / And now use this tovisit Google driver.get(http:/www.google.com);/ Alternatively thesame thing can be done like this/ driver.navigate().to(http:/www.google.com); / Find the text inputelement by its nameWebElement element =driver.findElement(By.name(q); / Enter something tosearch for element.sendKeys(Cheese!); / Now submit the form.WebDriver will find the form for us from the elementelement.submit(); / Check the title ofthe page System.out.println(Page title is: + driver.getTitle();/ Googles search isrendered dynamically with JavaScript./ Wait for the pageto load, timeout after 10 seconds(newWebDriverWait(driver, 10).until(new ExpectedCondition() public Booleanapply(WebDriver d) returnd.getTitle().toLowerCase().startsWith(cheese!);); / Should see:cheese! - Google Search System.out.println(Page title is: + driver.getTitle();/Close the browserdriver.quit();第2章 Webdirver对浏览器的支持2.1 HtmlUnit Driver优点:HtmlUnit Driver不会实际打开浏览器,运行速度很快。对于用FireFox等浏览器来做测试的自动化测试用例,运行速度通常很慢,HtmlUnit Driver无疑是可以很好地解决这个问题。缺点:它对JavaScript的支持不够好,当页面上有复杂JavaScript时,经常会捕获不到页面元素。使用:WebDriver driver = new HtmlUnitDriver(); 2.2 FireFox Driver优点:FireFox Dirver对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFox Driver都可以模拟。缺点:启动很慢,运行也比较慢,不过,启动之后Webdriver的操作速度虽然不快但还是可以接受的,建议不要频繁启停FireFox Driver。使用:WebDriver driver = new FirefoxDriver();Firefox profile的属性值是可以改变的,比如我们平时使用得非常频繁的改变useragent的功能,可以这样修改:FirefoxProfile profile = new FirefoxProfile();profile.setPreference(general.useragent.override, some UAstring);WebDriver driver = new FirefoxDriver(profile);2.3 InternetExplorer Driver优点:直观地模拟用户的实际操作,对JavaScript提供完善的支持。缺点:是所有浏览器中运行速度最慢的,并且只能在Windows下运行,对CSS以及XPATH的支持也不够好。使用:WebDriver driver = new InternetExplorerDriver();2.4 Chrome Driver预先下载Chrome驱动,下载地址:http:/chromedriver.storage.googleapis.com/index.html程序通过全路径调用chrome驱动程序。WebDriver webdriver;String driverLoc = D:TDownloadChromeDriverchromedriver.exe; String driver = webdriver.chrome.driver; System.setProperty(driver, driverLoc); webdriver = new ChromeDriver(); webdriver.get(website);第3章 使用操作3.1 如何找到页面元素Webdriver的findElement方法可以用来找到页面的某个元素,最常用的方法是用id和name查找。下面介绍几种比较常用的方法。3.1.1 By ID假设页面写成这样:
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号