selenium-webdriver基础语法.doc

上传人:灯火****19 文档编号:134929987 上传时间:2020-06-10 格式:DOC 页数:9 大小:227.62KB
返回 下载 相关 举报
selenium-webdriver基础语法.doc_第1页
第1页 / 共9页
selenium-webdriver基础语法.doc_第2页
第2页 / 共9页
selenium-webdriver基础语法.doc_第3页
第3页 / 共9页
selenium-webdriver基础语法.doc_第4页
第4页 / 共9页
selenium-webdriver基础语法.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《selenium-webdriver基础语法.doc》由会员分享,可在线阅读,更多相关《selenium-webdriver基础语法.doc(9页珍藏版)》请在金锄头文库上搜索。

1、笔记内容:(元素定位、元素操作、鼠标键盘操作、浏览器操作、等待、多选、拖放、显示所有链接)一、selenium webdriver简介:Watir-webdriver:是一种基于Ruby语言开发的Ruby库。它能够使用chrome、firefox、IE浏览器进行web测试,它可以模拟用户点击链接,填写表单,点击按钮等。相对于其他商业工具来说,小巧、灵活的watir-webdriver所提供的功能已经足够。二、用eclipse编写selenium脚本的方法:1、创建项目-创建包-导入selenium的俩个jar包之后就可以开始进行代码编写2、常用的语法:下表列出了webdriver的最常用的命令

2、以及它的语法,这将有助于我们开发webdriver脚本。Commmand描述driver.get(URL)导航到应用程序element.sendKeys(inputtext)输入一些文本输入框element.clear()从输入框清空内容select.deselectAll()这将取消选择页面上的第一个选择所有选项:select.selectByVisibleText(some text)select the OPTION with the input specified by the user.driver.switchTo().window(windowName)Moving the fo

3、cus from one window to anotherdriver.switchTo().frame(frameName)swing from frame to framedriver.switchTo().alert()Helps in handling alertsdriver.navigate().to(URL)Navigate to the URLdriver.navigate().forward()To Navigate forwarddriver.navigate().back()To Navigate backdriver.close()Closes the current

4、 Browser associated with the driverdriver.quit()Quits the driver and closes all the associated window of that driver.driver.refresh()Refreshes the current page.3、webdriver元素定位方法:原理:通过调用类:web driver和webelement的findelement方法来定位元素Findelements():搜索符合搜索条件的列表,如果没有找到返回空Findelement():搜索复合条件的元素,如果没有找到则抛出异常Me

5、thodSyntax描述By IDdriver.findElement(By.id()定位元素使用ID属性By namedriver.findElement(By.name()定位使用Name属性的元素By class namedriver.findElement(By.className()定位使用类属性的元素By tag namedriver.findElement(By.tagName()定位使用HTML标记元素By link textdriver.findElement(By.linkText()定位使用的链接文字链接By partial link textdriver.findEle

6、ment(By.partialLinkText()定位链接使用链接的文字部分By CSSdriver.findElement(By.cssSelector()定位使用CSS选择器的元素By XPathdriver.findElement(By.xpath()定位使用XPath查询元素4、对复选框/单选框的互动:操作:通过点击click()获取状态,返回true则是选中driver.findElement(By.id(cpayoff1).isDisplayed()driver.findElement(By.id(cpayoff1).isSelected()driver.findElement(B

7、y.id(cpayoff1).isEnabled()5、同步应用程序做完一步操作时需要等待应用程序同步1)静态等待:Thread.Sleep(1000);/Will wait for 1 second.2)显示等待个明确的等待,等待某个条件进一步处理之前发生。它主要用于当我们想要点击或采取行动的对象,一旦它是可见的。WebElementDynamicElement=(newWebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(By.id(DynamicElement);3)隐式等待直接等待指定时间,

8、如果时间到了就直接执行下一步骤,下一步骤无法执行直接抛出异常弊端:会延迟应用程序的执行时间driver.manage().timeouts().implicitlyWait(时间单位为S,TimeUnit.SECONDS);4)流利等待:FluentWait用于当webelement可以出现在5秒或者甚至它可以采取90秒。在这种情况下,我们定义的时间等待的状态的最大数量,以及与该查询的对象状态的是否存在等的频率。Wait wait =newFluentWait(driver).withTimeout(60, SECONDS).pollingEvery(10, SECONDS).ignoring

9、(NoSuchElementException.class);WebElement dynamicelement = wait.until(newFunction()publicWebElement apply(WebDriver driver)return driver.findElement(By.id(dynamicelement););6、拖放WebElementFrom= driver.findElement(By.xpath(./*id=j3_7/a);WebElementTo= driver.findElement(By.xpath(./*id=j3_1/a);Actions b

10、uilder =newActions(driver);Action dragAndDrop = builder.clickAndHold(From).moveToElement(To).release(To).build();dragAndDrop.perform();7、键盘操作:有时,我们会在一个情况输入一些组合键。例如: 按Ctrl键或Shift键。下面是用键盘操作交互的方法。sendKeys -发送键,在浏览器的键盘表示。特殊键都没有文字,表示按键都为字符,或单独序列的一部分的认可。pressKey -按键盘上不是文字的按键。键等功能键“F1”,“F2”或“Tab”或“Control”

11、等,如果keyToPress是一个字符序列,不同的驱动程序实现可以选择抛出一个异常,或者在序列中读取的第一个字符。releaseKey -执行按键事件后松开键盘上的一个键。它通常是拥有良好的非文本字符。void sendKeys(java.lang.CharSequence keysToSend)void pressKey(java.lang.CharSequence keyToPress)void releaseKey(java.lang.CharSequence keyToRelease)8、鼠标操作:Click -进行点击。我们还可以执行基于坐标的点击。contextClick -执行上

12、下文点击/右键单击一个元素或基于坐标doubleClick -执行双击webelement或基于坐标。如果留空它执行双击当前位置。mouseDown -执行一个元素上按下鼠标操作或基于坐标。mouseMove -执行元素上的鼠标移动操作或基于坐标。mouseUp -释放鼠标通常伴随着鼠标按下的动作和行为的基础上统筹。void click(WebElement onElement)void contextClick(WebElement onElement)void doubleClick(WebElement onElement)void mouseDown(WebElement onElem

13、ent)void mouseUp(WebElement onElement)void mouseMove(WebElement toElement)void mouseMove(WebElement toElement,long xOffset,long yOffset)9、多选:调用action类的builder.keyDown(Keys.CONTROL).click().build(); 方法import java.util.List;import java.util.concurrent.TimeUnit;import org.openqa.selenium.*;import org.o

14、penqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.interactions.Actions;import org.openqa.selenium.interactions.Action;publicclass webdriverdemopublicstaticvoid main(String args)throwsInterruptedExceptionWebDriver driver =newFirefoxDriver();driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);driver.navigate().to(http:/

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 外语文库 > 英语学习

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号