WebDriver拾级而上.docx_第1页
WebDriver拾级而上.docx_第2页
WebDriver拾级而上.docx_第3页
WebDriver拾级而上.docx_第4页
WebDriver拾级而上.docx_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

WebDriver拾级而上之一环境部署 (2012-05-16 21:15:28)转载标签:seleniumwebdriver分类:Selenium1.下载安装eclipse和jdk2.下载最新的Selenium Client Drivers/download/3.在eclipse中建立项目中导入所下载的包。(如果selenium-java-2.21.0.jar导入后,运行报错,则把下载的selenium-java-2.21.0.jar包中同一级目录下的libs中的jar包全部导入)4.配置testng4.1Eclipse中点击Help-Install new software-点击Add4.2在Location输入 /eclipse4.3选中Testng版本,点击Next,按照提示安装,安装完之后重启Eclipse4.4新建JavaProject,右键BuildPath,添加testng.jar4.5新建一个sum类,用来计算两整数之和,代码如下:package com.hpp;public class sum private int no1;private int no2;private int mysum;public int add(int no1,int no2)mysum=no1+no2;return mysum;4.6再新建testng class4.7点击finish,代码如下package com.test;import org.testng.annotations.Test;import static org.testng.Assert.assertEquals;import com.hpp.sum;public class NewTest private sum newSum=new sum();Testpublic void f() int mysum=newSum.add(1, 2);assertEquals(3,mysum,Right);testing,xml会自动配置好的,这里不用管项目的文件结构如下:4.8在testing.xml右键点击RunAs-Testng Suite,即可看到结果如果想要换个测试用例,可以修改文件testing.xml中class标签name的值4.9也可以不用在Testng Suite模式下运行新建class,代码如下run as-Java Application即可。注:如果是用chrome浏览器运行脚本,需要下载最新的chromedriver.exe,放在目录C:WINDOWSsystem32 下即可。下载地址: /index.htmlpackage com.test;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class Test_google public static void main(String args) String url = .hk;/String url = /user/CheckLogin.aspx?UserName=aaaaa.aa&Password=123456&key=889;/System.setProperty(webdriver.firefox.bin,D:Program FilesMozilla Firefoxfirefox.exe); /WebDriver driver = new FirefoxDriver();/打开ie WebDriver ie_driver = new InternetExplorerDriver();/打开chromeWebDriver driver = new ChromeDriver();driver.get(url);WebElement element = driver.findElement(B(q);element.sendKeys(hello Selenium!);element.submit();try Thread.sleep(3000); catch (InterruptedException e) e.printStackTrace();System.out.println(页面Title:+driver.getTitle()+n页面URL:+driver.getCurrentUrl();/System.out.println(返回当前的浏览器的窗口句柄:+driver.getWindowHandle();/String s=driver.getPageSource();s=s.substring(s.indexOf(), s.indexOf();/System.out.println(当前页面的源码:+s);driver.quit();4.10 如果要在Myeclipse安装插件Testng直接把eclipse里的org.testng文件夹拷贝到dropins目录重启Myeclipse会提示如下,确定就OK了5.Eclipse中导入jar文件的源码在eclipse中导入jar文件对应的源码,主要是为了方便查看一些接口的源码,可以直接按着Ctrl+鼠标左键跳到对应的源码文件。若无法查看类,函数等信息将下载selenium-java-2.21.0包中的selenium-java-2.21.0-srcs.jar导入WebDriver拾级而上之二浏览器操作(2012-05-16 21:32:24)转载标签:seleniumwebdriver分类:Selenium1.启动浏览器A.firefox/打开默认路径的firefox(路径指的是firefox的安装路径)WebDriver diver = new FirefoxDriver();/打开指定路径的firefox,方法1System.setProperty(webdriver.firefox.bin,D:ProgramFilesMozilla Firefoxfirefox.exe);WebDriver dr = new FirefoxDriver();/打开指定路径的firefox,方法2File pathToFirefoxBinary = new File(D:Program FilesMozilla Firefoxfirefox.exe); FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary); WebDriver driver1 = new FirefoxDriver(firefoxbin,null);B.ie/打开ieWebDriver ie_driver = new InternetExplorerDriver();C.chrome因为Chrome Driver是Chromium 项目自己支持和维护的,所以你必需另外下载chromedriver.exe,放在目录下C:WINDOWSsystem32下载地址:/p/chromedriver/downloads/list/index.html/打开chromeWebDriver driver = new ChromeDriver();另一种启动chrome 的方法wiki介绍:/p/selenium/wiki/ChromeDriver/打开chromeSystem.setProperty(webdriver.chrome.driver, D:chromedriver.exe);System.setProperty(webdriver.chrome.bin,C:Documents and SettingsfyLocal Settings+Application DataGoogleChromeApplicationchrome.exe);Chromium介绍:/p/chromium/2.页面跳转urlString url = ;WebDriver driver = new FirefoxDriver();A/用get方法driver.get(url);B/用navigate方法,然后再调用to方法,chrome不支持这种方法driver.navigate().to(url);3.如果页面文件在本地可以这么写WebDriver dr = new ChromeDriver();/页面文件在项目src下的路径 src/filename.htmlFile file = new File(src/filename.html);String filePath = file:/ + file.getAbsolutePath();System.out.printf(now accesss %s n, filePath);dr.get(filePath);4.关闭浏览器/quit关闭所有页面 close关闭本次执行打开的页面A./用quit方法driver.quit();B./用close方法driver.close();5.浏览器最大化driver.manage().window().maximize();6.获取页面信息/得到titleString title =driver.getTitle();/得到当前页面urlString currentUrl =driver.getCurrentUrl();getWindowHandle()返回当前的浏览器的窗口句柄getWindowHandles()返回当前的浏览器的所有窗口句柄getPageSource()返回当前页面的源码/String s=driver.getPageSource();s=s.substring(s.indexOf(), s.indexOf();/System.out.println(当前页面的源码:+s);7.总结操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。源代码这些方法都是在org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承RemoteWebDriver。selenium-webdriver提供了强大的元素定位方法,支持以下三种方法:单个对象的定位方法多个对象的定位方法层级定位WebDriver拾级而上之三定位页面元素(2012-05-16 22:21:06)转载注意:selenium-webdriver通过findElement()findElements()等find方法调用By对象来定位和查询元素。By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素 List,如果不存在符合条件的就返回一个空的list。一、定位单个元素A.使用className进行定位当所定位的元素具有class属性的时候我们可以通过classname来定位该元素。例:下面的例子定位页面上class为username的li。WebElement element =driver.findElement(By.className(username);System.out.println(element.getTagName();输出结果:LiB.使用id属性定位例:WebElement element = dr.findElement(By.id(passport_user);System.out.println(element.getAttribute(title);输出结果:用户名/邮箱C.使用name属性定位例:WebElement e = dr.findElement(B(passport_user);D.使用css属性定位例:WebElement e1 = dr.findElement(By.cssSelector(#passport_user);详解:1.css之后代选择器location1location2可以通过css=p em这个可以选中文本为location1的em元素css=ol em这个可以选中文本为location2的em元素css后代选择器和xpath中/div/a一样:取得所有div下面所有的a节点。这个是忽略了父子节点location1location2location3location4可以通过css=pem来定位location1css之父子节点选择器给后代选择器加了一个限制,类似xpath中/div/p/em:所有div下的子元素p的子元素em。css=li+li em来定位location3,location4的emcss=li+strong+em来定位文本为location2的em2.css之id选择器通过css=#location1来定位type为button的按钮通过css=#location2来定位type为radio的单选框3.css之类选择器通过css=input.location1来选择value值为确定的按钮通过css=input.location2来选择value值为取消的按钮4.css之属性选择器通过css=class=location1可以定位第一个按钮通过css=class=1可以定位第一个按钮通过css=value=确定可以定位第一个按钮通过css=inputclass=location可以定位第二个按钮E.按标记(tag)名称查找元素的DOM标记名称WebElement frame = driver.findElement(By.tagName(iframe);F.按链接文本查找cheeseWebElement cheese = driver.findElement(By.linkText(cheese);按部分链接文本查找search for cheeseWebElement cheese = driver.findElement(By.partialLinkText(cheese);G.使用 XPATH定位例:WebElement element =dr.findElement(By.xpath(/inputid=passport_user);parent:返回父节点following:返回此节点后面的兄弟节点preceding:返回此节点前面的兄弟节点div通过id为location1可以很随意的定位到兄弟节点/div/inputid=location1/following:input也可以通过location1很随意的定位到父类节点div。/div/inputid=location1/parent:div。也可以通过索引为2的input定位到id为location1的input/div/input2/preceding-sibling:input有几个非常有用的Firefox插件,有助于发现一个元素的XPath:XPath Checker - suggests XPath and can be used to test XPath results.Firebug - XPath suggestions are just one of the many powerful features of this very useful add-on.XPath Checker - 建议XPath并可以用于测试XPath的结果Firebug - XPath建议仅仅是这非常有用的插件的许多强有力特征中的一个。参考网站:/xpath/index.asp二、定位多个元素/定位到所有标签的元素,然后输出他们的idList element = driver.findElements(By.tagName(input);for (WebElement e : element)System.out.println(e.getAttribute(id);三、层级定位层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。下面的代码演示了如何使用层级定位class为login的div,然后再取得它下面的所有label,并打印出他们的文本/定位class为login的div,然后再取得它下面的所有label,并打印出他们的值WebElement element = driver.findElement(By.className(login);List el = element.findElements(By.tagName(label);for(WebElement e : el)System.out.println(e.getText();四、使用Javascript你可以执行任何Javascript,以找到一个元素,只要你找到一个DOM元素,它将自动转换为WebElement对象。WebElement element = (WebElement) (JavascriptExecutor)driver).executeScript(return$(.cheese)0);WebDriver拾级而上之四操作页面元素(2012-05-16 22:48:00)转载标签:seleniumwebdriverit分类:Selenium一、输入框(text field or textarea)/找到输入框元素:WebElement element = driver.findElement(By.id(passwd-id);/将输入框清空:element.clear();/在输入框中输入内容:element.sendKeys(“test”);/获取输入框的文本内容:element.getText();二、下拉选择框(Select)/找到下拉选择框的元素:Select select = new Select(driver.findElement(By.id(select);/选择对应的选择项:select.selectByVisibleText(“mediaAgencyA”);或select.selectByValue(“MA_ID_001”);/不选择对应的选择项:select.deselectAll();select.deselectByValue(“MA_ID_001”);select.deselectByVisibleText(“mediaAgencyA”);或者获取选择项的值:select.getAllSelectedOptions();select.getFirstSelectedOption();对下拉框进行操作时首先要定位到这个下拉框,new 一个Selcet对象,然后对它进行操作例如:以/reg2.5p这个页面为例。这个页面中有4个下拉框,下面演示4种选中下拉框选项的方法。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.Select;public class SelectsStudy public static void main(String args) System.setProperty(webdriver.firefox.bin,D:Program FilesMozilla Firefoxfirefox.exe);WebDriver dr = new FirefoxDriver();dr.get(/reg2.5p);/通过下拉列表中选项的索引选中第二项,即2011年Select selectAge = new Select(dr.findElement(By.id(User_Age);selectAge.selectByIndex(2);/Select.selectByIndex/通过下拉列表中的选项的value属性选中上海这一项Select selectShen = new Select(dr.findElement(By.id(User_Shen);selectShen.selectByValue(上海);/Select.selectByValue/通过下拉列表中选项的可见文本选 中浦东这一项Select selectTown = new Select(dr.findElement(By.id(User_Town);selectTown.selectByVisibleText(浦东);Select.selectByVisibleText/这里只是想遍历一下下拉列表所有选项,用click进行选中选项Select selectCity = new Select(dr.findElement(By.id(User_City);for(WebElement e : selectCity.getOptions()/Select.getOptions()e.click();三、单选项(Radio Button)/找到单选框元素:WebElement bookMode =driver.findElement(By.id(BookMode);/选择某个单选项:bookMode.click();/清空某个单选项:bookMode.clear();/判断某个单选项是否已经被选择:bookMode.isSelected();四、多选项(checkbox)/多选项的操作和单选的差不多:WebElement checkbox =driver.findElement(By.id(myCheckbox.);checkbox.click();checkbox.clear();checkbox.isSelected();checkbox.isEnabled();五、按钮(button)/找到按钮元素:WebElement saveButton = driver.findElement(By.id(save);/点击按钮:saveButton.click();/判断按钮是否enable:saveButton.isEnabled ();六、左右选择框也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。例如:Select lang = new Select(driver.findElement(By.id(languages);lang.selectByVisibleText(“English”);WebElement addLanguage =driver.findElement(By.id(addButton);addLanguage.click();七、弹出对话框(Popup dialogs)Alert alert = driver.switchTo().alert();alert.accept();alert.dismiss();alert.getText();八、表单(Form)Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:WebElement approve = driver.findElement(By.id(approve);approve.click();或approve.submit();/只适合于表单的提交九、上传文件 (Upload File)上传文件的元素操作:WebElement adFileUpload = driver.findElement(By.id(WAP-upload);String filePath = C:testuploadfilemedia_adstest.jpg;adFileUpload.sendKeys(filePath);十、拖拉(Drag andDrop)WebElement element =driver.findElement(B(source);WebElement target = driver.findElement(B(target);(new Actions(driver).dragAndDrop(element, target).perform();例如:下面这个页面是一个演示拖放元素的页面,你可以把左右页面中的条目拖放到右边的div框中。/demo/html/drag-drop/drag-drop.htmlimport org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import eractions.Actions;public class DragAndDrop public static void main(String args) System.setProperty(webdriver.firefox.bin,D:Program FilesMozilla Firefoxfirefox.exe);WebDriver dr = new FirefoxDriver();dr.get(/demo/html/drag-drop/drag-drop.html);/首先new出要拖入的页面元素对象和目标对象,然后进行拖入。WebElementelement = dr.findElement(By.id(item1);WebElementtarget = dr.findElement(By.id(drop);(new Actions(dr).dragAndDrop(element, target).perform();/利用循环把其它item也拖入String id=item ;for(int i=2;i=6;i+)String item = id+i;(new Actions(dr).dragAndDrop(dr.findElement(By.id(item), target).perform();代码很简单,需要注意的是(new Actions(dr).dragAndDrop(element, target).perform();这句话中,dragAndDrop(element, target)这个方法是定义了“点击element元素对象,然后保持住,直到拖到目标元素对象里面才松开”这一系列动作的Actions,如果你不调用perform()方法,这个Actions是不会执行的。十一、导航 (Navigationand History)/打开一个新的页面:driver.navigate().to();/通过历史导航返回原页面:driver.navigate().forward();driver.navigate().back();十二、获取页面CSS属性1.获取文字颜色dr.findElement(By.id(tooltip).getCssValue(color)2.获取文字字号dr.findElement(By.tagName(h3).getCssValue(font)WebDriver拾级而上之五iframe的处理 (2012-05-17 13:39:53)转载标签:seleniumwebdriverit分类:Selenium有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代码也没有任何问题。这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一。如果你在一个default content中查找一个在iframe中的元素,那肯定是找不到的。反之你在一个iframe中查找另一个iframe元素或default content中的元素,那必然也定位不到。selenium webdriver中提供了进入一个iframe的方法:WebDriver org.openqa.selenium.WebDriver.TargetLocator.frame(String nameOrId)也提供了一个返回default content的方法:WebDr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论