selenium20中文帮助文档.docx

上传人:b****6 文档编号:7416270 上传时间:2023-05-11 格式:DOCX 页数:18 大小:23KB
下载 相关 举报
selenium20中文帮助文档.docx_第1页
第1页 / 共18页
selenium20中文帮助文档.docx_第2页
第2页 / 共18页
selenium20中文帮助文档.docx_第3页
第3页 / 共18页
selenium20中文帮助文档.docx_第4页
第4页 / 共18页
selenium20中文帮助文档.docx_第5页
第5页 / 共18页
selenium20中文帮助文档.docx_第6页
第6页 / 共18页
selenium20中文帮助文档.docx_第7页
第7页 / 共18页
selenium20中文帮助文档.docx_第8页
第8页 / 共18页
selenium20中文帮助文档.docx_第9页
第9页 / 共18页
selenium20中文帮助文档.docx_第10页
第10页 / 共18页
selenium20中文帮助文档.docx_第11页
第11页 / 共18页
selenium20中文帮助文档.docx_第12页
第12页 / 共18页
selenium20中文帮助文档.docx_第13页
第13页 / 共18页
selenium20中文帮助文档.docx_第14页
第14页 / 共18页
selenium20中文帮助文档.docx_第15页
第15页 / 共18页
selenium20中文帮助文档.docx_第16页
第16页 / 共18页
selenium20中文帮助文档.docx_第17页
第17页 / 共18页
selenium20中文帮助文档.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

selenium20中文帮助文档.docx

《selenium20中文帮助文档.docx》由会员分享,可在线阅读,更多相关《selenium20中文帮助文档.docx(18页珍藏版)》请在冰点文库上搜索。

selenium20中文帮助文档.docx

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

Gouda

可以通过这样查找页面元素:

Listcheeses=driver.findElements(By.className("cheese"));

 

3.1.5ByLinkText

假设页面元素写成这样:

 那么可以通过这样查找:

WebElementcheese=driver.findElement(By.linkText("cheese"));

 

 

 

3.2  如何对页面元素进行操作

找到页面元素后,怎样对页面进行操作呢?

我们可以根据不同的类型的元素来进行一一说明。

3.2.1输入框(textfieldortextarea)

  找到输入框元素:

WebElementelement=driver.findElement(By.id("passwd-id"));

在输入框中输入内容:

element.sendKeys(“test”);

将输入框清空:

element.clear();

获取输入框的文本内容:

element.getText();

 

3.2.2下拉选择框(Select)

找到下拉选择框的元素:

Selectselect=newSelect(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();

 

3.2.3单选项(RadioButton)

找到单选框元素:

WebElementbookMode=driver.findElement(By.id("BookMode"));

选择某个单选项:

bookMode.click();

清空某个单选项:

bookMode.clear();

判断某个单选项是否已经被选择:

bookMode.isSelected();

3.2.4多选项(checkbox)

多选项的操作和单选的差不多:

WebElementcheckbox=driver.findElement(By.id("myCheckbox."));

checkbox.click();

checkbox.clear();

checkbox.isSelected();

checkbox.isEnabled();

3.2.5按钮(button)

找到按钮元素:

WebElementsaveButton=driver.findElement(By.id("save"));

点击按钮:

saveButton.click();

判断按钮是否enable:

 

saveButton.isEnabled();

3.2.6左右选择框

也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。

例如:

Selectlang=newSelect(driver.findElement(By.id("languages")));

lang.selectByVisibleText(“English”);

WebElementaddLanguage=driver.findElement(By.id("addButton"));

addLanguage.click();

3.2.7弹出对话框(Popupdialogs)

Alertalert=driver.switchTo().alert();

alert.accept();

alert.dismiss();

alert.getText();

3.2.8表单(Form)

Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:

WebElementapprove=driver.findElement(By.id("approve"));

approve.click();

approve.submit();//只适合于表单的提交

3.2.9上传文件(UploadFile)

上传文件的元素操作:

WebElementadFileUpload=driver.findElement(By.id("WAP-upload"));

StringfilePath="C:

\test\\uploadfile\\media_ads\\test.jpg";

adFileUpload.sendKeys(filePath);

3.2.10 Windows和Frames之间的切换

一般来说,登录后建议是先:

driver.switchTo().defaultContent();

切换到某个frame:

driver.switchTo().frame("leftFrame");

从一个frame切换到另一个frame:

driver.switchTo().frame("mainFrame");

切换到某个window:

driver.switchTo().window("windowName");

 

3.2.11 拖拉(DragandDrop)

WebElementelement=driver.findElement(By.name("source"));

WebElementtarget=driver.findElement(By.name("target"));

 

(newActions(driver)).dragAndDrop(element,target).perform();

 

3.2.12 导航(NavigationandHistory)

打开一个新的页面:

 driver.navigate().to("");

 

通过历史导航返回原页面:

driver.navigate().forward();

driver.navigate().back();

3.3 高级使用

3.3.1改变useragent

UserAgent的设置是平时使用得比较多的操作:

FirefoxProfileprofile=newFirefoxProfile();

profile.addAdditionalPreference("general.useragent.override","someUAstring");

WebDriverdriver=newFirefoxDriver(profile);

3.3.2读取Cookies

我们经常要对的值进行读取和设置。

增加cookie:

//Nowsetthecookie.Thisone'svalidfortheentiredomain

Cookiecookie=newCookie("key","value");

driver.manage().addCookie(cookie);

获取cookie的值:

//AndnowoutputalltheavailablecookiesforthecurrentURL

SetallCookies=driver.manage().getCookies();

for(CookieloadedCookie:

allCookies){

   System.out.println(String.format("%s->%s",loadedCookie.getName(),loadedCookie.getValue()));

}

根据某个cookie的name获取cookie的值:

driver.manage().getCookieNamed("mmsid");

删除cookie:

 

//Youcandeletecookiesin3ways

//Byname

driver.manage().deleteCookieNamed("CookieName");

//ByCookie

driver.manage().deleteCookie(loadedCookie);

//Orallofthem

driver.manage().deleteAllCookies();

3.3.3调用JavaScript

Webdriver对JavaScript的调用是通过JavascriptExecutor来实现的,例如:

JavascriptExecutorjs=(JavascriptExecutor)driver;

js.executeScript("(function(){inventoryGridMgr.setTableFieldValue('"+inventoryId+"','"+fieldName+"','"

               +value+"');})()");

 

3.3.4Webdriver截图

如果用webdriver截图是:

driver=webdriver.Firefox()

driver.save_screenshot("C:

\error.jpg")

3.3.5页面等待

因为Load页面需要一段时间,如果页面还没加载完就查找元素,必然是查找不到的。

最好的方式,就是设置一个默认等待时间,在查找页面元素的时候如果找不到就等待一段时间再找,直到超时。

Webdriver提供两种方法,一种是显性等待,另一种是隐性等待。

显性等待:

WebDriverdriver=newFirefoxDriver();

driver.get("http:

//somedomain/url_that_delays_loading");

WebElementmyDynamicElement=(newWebDriverWait(driver,10))

 .until(newExpectedCondition(){

 @Override

 publicWebElementapply(WebDriverd){

   returnd.findElement(By.id("myDynamicElement"));

 }});

 

隐性等待:

WebDriverdriver=newFirefoxDriver();

driver.ma

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 幼儿教育 > 幼儿读物

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

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