ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:23KB ,
资源ID:7416270      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-7416270.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(selenium20中文帮助文档.docx)为本站会员(b****6)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

selenium20中文帮助文档.docx

1、selenium20中文帮助文档Selenium2.0帮助文档第1章 Webdirver基础 21.1下载selenium2.0的lib包 21.2用webdriver打开一个浏览器 21.3打开测试页面 21.4GettingStarted 2第2章 Webdirver对浏览器的支持 42.1HtmlUnit Driver 42.2FireFox Driver 42.3InternetExplorer Driver 4第3章 使用操作 43.1如何找到页面元素 43.1.1 By ID 53.1.2 By Name 53.1.3 By XPATH 53.1.4 By Class Name 5

2、3.1.5 By Link Text 53.2如何对页面元素进行操作 63.2.1 输入框(text field or textarea) 63.2.2 下拉选择框(Select) 63.2.3 单选项(Radio Button) 63.2.4 多选项(checkbox) 73.2.5 按钮(button) 73.2.6 左右选择框 73.2.7 弹出对话框(Popup dialogs) 73.2.8 表单(Form) 83.2.9 上传文件 (Upload File) 83.2.10Windows 和 Frames之间的切换 83.2.11拖拉(Drag andDrop) 83.2.12导航

3、 (Navigationand History) 83.3高级使用 93.3.1 改变user agent 93.3.2 读取Cookies 93.3.3 调用Java Script 93.3.4 Webdriver截图 103.3.5 页面等待 10第4章 RemoteWebDriver 104.1使用RemoteWebDriver 104.2SeleniumServer 114.3How to setFirefox profile using RemoteWebDriver 11第5章 封装与重用 12第6章 在selenium2.0中使用selenium1.0的API 14第1章 Web

4、dirver基础1.1下载selenium2.0的lib包官方UserGuide:http:/seleniumhq.org/docs/1.2用webdriver打开一个浏览器我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器。但是做页面的测试,速度通常很慢,严重影响持续集成的速度,这个时候建议使用HtmlUnit,不过HtmlUnitDirver运行时是看不到界面的,对调试就不方便了。使用哪种浏览器,可以做成配置项,根据需要灵活配置。打开firefox浏览器: /Create a newinstance of the Firefox driver

5、 WebDriver driver = newFirefoxDriver();打开IE浏览器 /Create a newinstance of the Internet Explorer driver WebDriver driver = newInternetExplorerDriver ();打开HtmlUnit浏览器 /Createa new instance of the Internet Explorer driver WebDriverdriver = new HtmlUnitDriver();1.3打开测试页面对页面对测试,首先要打开被测试页面的地址(如:),web driver

6、 提供的get方法可以打开一个页面: / And now use thedriver to visit Google driver.get();1.4GettingStartedpackage 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.sel

7、enium.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 dr

8、iver = newFirefoxDriver(); / And now use this tovisit Googledriver.get(); / Alternatively thesame thing can be done like this / driver.navigate().to(); / Find the text inputelement by its name WebElement element =driver.findElement(By.name(q); / Enter something tosearch forelement.sendKeys(Cheese!);

9、 / Now submit the form.WebDriver will find the form for us from the element element.submit(); / Check the title ofthe pageSystem.out.println(Page title is: + driver.getTitle(); / Googles search isrendered dynamically with JavaScript. / Wait for the pageto load, timeout after 10 seconds (newWebDriver

10、Wait(driver, 10).until(new ExpectedCondition() public Booleanapply(WebDriver d) returnd.getTitle().toLowerCase().startsWith(cheese!); ); / Should see:cheese! - Google SearchSystem.out.println(Page title is: + driver.getTitle(); /Close the browser driver.quit(); 第2章 Webdirver对浏览器的支持2.1HtmlUnit Driver

11、优点:HtmlUnit Driver不会实际打开浏览器,运行速度很快。对于用FireFox等浏览器来做测试的自动化测试用例,运行速度通常很慢,HtmlUnit Driver无疑是可以很好地解决这个问题。缺点:它对JavaScript的支持不够好,当页面上有复杂JavaScript时,经常会捕获不到页面元素。使用:WebDriver driver = new HtmlUnitDriver();2.2FireFox Driver优点:FireFox Dirver对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFox D

12、river都可以模拟。缺点:启动很慢,运行也比较慢,不过,启动之后Webdriver的操作速度虽然不快但还是可以接受的,建议不要频繁启停FireFox Driver。使用:WebDriver driver = new FirefoxDriver();Firefox profile的属性值是可以改变的,比如我们平时使用得非常频繁的改变useragent的功能,可以这样修改:FirefoxProfile profile = new FirefoxProfile();profile.setPreference(general.useragent.override, some UAstring);We

13、bDriver driver = new FirefoxDriver(profile);2.3InternetExplorer Driver优点:直观地模拟用户的实际操作,对JavaScript提供完善的支持。缺点:是所有浏览器中运行速度最慢的,并且只能在Windows下运行,对CSS以及XPATH的支持也不够好。使用:WebDriver driver = new InternetExplorerDriver();第3章 使用操作3.1如何找到页面元素Webdriver的findElement方法可以用来找到页面的某个元素,最常用的方法是用id和name查找。下面介绍几种比较常用的方法。3.1

14、.1 By ID假设页面写成这样:那么可以这样找到页面的元素:通过id查找:WebElement element = driver.findElement(By.id(passwd-id);3.1.2 By Name或通过name查找:WebElement element = driver.findElement(By.name(passwd);3.1.3 By XPATH或通过xpath查找:WebElement element =driver.findElement(By.xpath(/inputid=passwd-id);3.1.4 By Class Name假设页面写成这样:Chedd

15、arGouda可以通过这样查找页面元素:Listcheeses = driver.findElements(By.className(cheese);3.1.5 By Link Text假设页面元素写成这样:ahref=那么可以通过这样查找:WebElement cheese =driver.findElement(By.linkText(cheese);3.2如何对页面元素进行操作找到页面元素后,怎样对页面进行操作呢?我们可以根据不同的类型的元素来进行一一说明。3.2.1 输入框(text field or textarea) 找到输入框元素:WebElement element = dri

16、ver.findElement(By.id(passwd-id);在输入框中输入内容:element.sendKeys(“test”);将输入框清空:element.clear();获取输入框的文本内容:element.getText();3.2.2 下拉选择框(Select)找到下拉选择框的元素:Select select = new Select(driver.findElement(By.id(select); 选择对应的选择项:select.selectByVisibleText(“mediaAgencyA”);或select.selectByValue(“MA_ID_001”);不选

17、择对应的选择项:select.deselectAll();select.deselectByValue(“MA_ID_001”);select.deselectByVisibleText(“mediaAgencyA”);或者获取选择项的值:select.getAllSelectedOptions();select.getFirstSelectedOption();3.2.3 单选项(Radio Button)找到单选框元素:WebElement bookMode =driver.findElement(By.id(BookMode);选择某个单选项:bookMode.click();清空某个单

18、选项:bookMode.clear();判断某个单选项是否已经被选择:bookMode.isSelected();3.2.4 多选项(checkbox)多选项的操作和单选的差不多:WebElement checkbox =driver.findElement(By.id(myCheckbox.);checkbox.click(); checkbox.clear();checkbox.isSelected();checkbox.isEnabled();3.2.5 按钮(button)找到按钮元素:WebElement saveButton = driver.findElement(By.id(s

19、ave);点击按钮:saveButton.click();判断按钮是否enable:saveButton.isEnabled ();3.2.6 左右选择框也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。例如:Select lang = new Select(driver.findElement(By.id(languages);lang.selectByVisibleText(“English”);WebElement addLanguage =driver.findElement(By.id(addButton);addLanguage.click();3.2.7 弹出对话框(Po

20、pup dialogs)Alert alert = driver.switchTo().alert();alert.accept();alert.dismiss();alert.getText();3.2.8 表单(Form)Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:WebElement approve = driver.findElement(By.id(approve);approve.click();或approve.submit();/只适合于表单的提交3.2.9 上传文件 (Upload File)上传文件的元素操作:WebElement adFi

21、leUpload = driver.findElement(By.id(WAP-upload);String filePath = C:testuploadfilemedia_adstest.jpg;adFileUpload.sendKeys(filePath);3.2.10Windows 和 Frames之间的切换一般来说,登录后建议是先:driver.switchTo().defaultContent();切换到某个frame:driver.switchTo().frame(leftFrame);从一个frame切换到另一个frame:driver.switchTo().frame(mai

22、nFrame);切换到某个window:driver.switchTo().window(windowName);3.2.11拖拉(Drag andDrop)WebElement element =driver.findElement(By.name(source);WebElement target = driver.findElement(By.name(target);(new Actions(driver).dragAndDrop(element, target).perform();3.2.12导航 (Navigationand History)打开一个新的页面:driver.nav

23、igate().to();通过历史导航返回原页面: driver.navigate().forward();driver.navigate().back();3.3高级使用3.3.1 改变user agentUser Agent的设置是平时使用得比较多的操作:FirefoxProfile profile = new FirefoxProfile();profile.addAdditionalPreference(general.useragent.override,some UA string);WebDriver driver = new FirefoxDriver(profile);3.3

24、.2 读取Cookies我们经常要对的值进行读取和设置。增加cookie:/ Now set the cookie. This ones valid for the entire domainCookie cookie = new Cookie(key, value);driver.manage().addCookie(cookie);获取cookie的值:/ And now output all the available cookies for the current URLSet allCookies = driver.manage().getCookies();for (Cookie

25、loadedCookie : allCookies) System.out.println(String.format(%s - %s,loadedCookie.getName(), loadedCookie.getValue();根据某个cookie的name获取cookie的值:driver.manage().getCookieNamed(mmsid);删除cookie:/ You can delete cookies in 3 ways/ By namedriver.manage().deleteCookieNamed(CookieName);/ By Cookiedriver.mana

26、ge().deleteCookie(loadedCookie);/ Or all of themdriver.manage().deleteAllCookies();3.3.3 调用Java ScriptWeb driver对Java Script的调用是通过JavascriptExecutor来实现的,例如:JavascriptExecutor js = (JavascriptExecutor) driver;js.executeScript(function()inventoryGridMgr.setTableFieldValue(+ inventoryId + , + fieldName

27、 + , + value + );)();3.3.4 Webdriver截图如果用webdriver截图是:driver = webdriver.Firefox()driver.save_screenshot(C:error.jpg)3.3.5 页面等待因为Load页面需要一段时间,如果页面还没加载完就查找元素,必然是查找不到的。最好的方式,就是设置一个默认等待时间,在查找页面元素的时候如果找不到就等待一段时间再找,直到超时。Webdriver提供两种方法,一种是显性等待,另一种是隐性等待。显性等待:WebDriver driver =new FirefoxDriver();driver.get(http:/somedomain/url_that_delays_loading);WebElement myDynamicElement = (new WebDriverWait(driver, 10) .until(new ExpectedCondition() Override public WebElement apply(WebDriver d) returnd.findElement(By.id(myDynamicElement); );隐性等待:WebDriver driver = new FirefoxDriver();driver.ma

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2