关键词不能为空

当前您在: 主页 > 英语 >

Selenium使用教程

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-28 05:41
tags:

-

2021年2月28日发(作者:连衣裙英语怎么写)


Selenium2.0


帮助文档




1




Webdirver


基础


< p>
.


................................ .................................................. ................................


2



1.1

下载


selenium2.0



l ib



........................ .................................................. .....................


2



1.2



webdriver< /p>


打开一个浏览器



.

................................................ ........................................


2



1.3


打开测试页面



.

................................................ .................................................. ................


2



1.4 GettingStarted .................. .................................................. ...............................................


2




2




Webdirver


对浏览器的支持



....................................... .................................................. ......


4



2.1 HtmlUnit Driver


.


........... .................................................. ..................................................


4



2.2 FireFox Driver


.


.................... .................................................. ............................................


4



2.3 InternetExplorer Driver


.


... .................................................. ...............................................


5




3




使用操作



.


.................................................. .................................................. ........................


5



3.1


如何找到页面元素



.


.............................. .................................................. ...........................


5



3.1.1 By ID


.


.............................. .................................................. .......................................


6



3.1.2 By Name . .................................................. .................................................. .............


6



3.1.3 By XPATH ........................ .................................................. .....................................


6



3.1.4 By Class Name


.


...................... .................................................. ................................


7



3.1.5 By Link Text ............................................. .................................................. .............


7



3.2


如何对页面元素进行操作



.


........................... .................................................. ..................


8



3.2.1


输入框(


text field or textarea



.


.................................... ........................................


8



3.2.2


下拉选择框


(Select) .............. .................................................. ................................


9



3.2.3


单选项


(Radio Button)


.


..................................... .................................................. .....


9



3.2.4


多选项


(checkbox)


.


......................................... .................................................. .....


1


0


3.2.5


按钮


(button)


.

< p>
............................................ .................................................. ...........


1


0


3.2.6


左右选择框


< p>
.


................................ .................................................. ........................


1


0


3.2.7


弹出对话框


(Popup dialogs) ......................................... ........................................


1


0


3.2.8


表单


(Form) ................... .................................................. ......................................


1


0


3.2.9


上传文件



(Upload File) ............................................ ...........................................


11


3.2.10 Windows




Frames


之间的切换


< p>
............................................ ..........................


11


3.2.11


拖拉


(Drag andDrop) ......................................... .................................................. .


11


3.2.12


导航



(Navigationand History)


.


.................. .................................................. ........


11


3.3


高级使用



.


.................................................. .................................................. ......................


1


2


3.3.1


改变


user agent ............................................ .................................................. .......


1


2


3.3.2


读取


Cookies


.


............................................. .................................................. .........


1


2


3.3.3


调用


Java Script ........................................... .................................................. .......


1


3


3.3.4 Webdriver


截图



..... .................................................. ...............................................


1


3


3.3.5


页面等待



.


.................................................. .................................................. ..........


1


3



4




RemoteWebDriver


.


.................................................. .................................................. ........


1


4


4.1


使用


RemoteWebDriver


.


..................................... .................................................. ............


1


4


4.2 SeleniumServer .................... .................................................. ...........................................


1


5


4.3 How to setFirefox profile using RemoteWebDriver ......... ................................................


1


5



5




封装与重用



.


................................................. .................................................. ...................


1


5



6





selenium2.0


中使用


selenium1.0



API

< p>
.


................................ ....................................


1


5



1



Webdirver


基础



1.1




下载


se lenium2.0



lib




/p/selenium/downloads/list


< /p>


官方


UserGuide



/docs/



1.2





webdriver


打开一个浏览器



我们常用的浏览器有


firefox



IE


两种 ,


firefox



selenium


支持得比较成熟的浏览器。但是


做页面的测试,速度通常很慢, 严重影响持续集成的速度,这个时候建议使用


HtmlUnit



不过


HtmlUnitDirver


运行时是看不到界面的,


对调试就不方便了。


使用哪种浏览器,


可以做


成配置项,根据需要灵活配置。




打开


firefox


浏览器:




//Create a newinstance of the Firefox driver



WebDriver driver = new FirefoxDriver();



打开


IE


浏览器




//Create a newinstance of the Internet Explorer driver



perty(


driver = new InternetExplorerDriver();



打开


HtmlUnit


浏览器




//Createa new instance of the Internet Explorer driver




WebDriverdriver = new HtmlUnitDriver();



打开


Chrome

< br>浏览器



perty(


driver = new ChromeDriver();



1.3




打开测试页面



对页面对测试,首先要 打开被测试页面的地址(如:




,we b driver



供的


get


方法可以打开一个页面:




// And now use thedriver to visit Google



(


1.4




GettingStarted


package e;


import


import ver;


import ment;


import xDriver;


import edCondition;


import verWait;



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


(



// Alternatively thesame thing can be done like this



// te().to(




// Find the text inputelement by its name



WebElement element =ement((




// Enter something tosearch for


ys(




// Now submit the ver will find the form for us from the element



();




// Check the title ofthe page


n(





// Google's search isrendered dynamically with JavaScript.



// Wait for the pageto load, timeout after 10 seconds



(newWebDriverWait(driver, 10)).until(new ExpectedCondition() {



public Booleanapply(WebDriver d) {



le().toLowerCase().startsWith(


}



});




// Should see:


n(





//Close the browser



();



}


}



2



Webdirver


对浏览器的支持



2.1




HtmlUnit Driver


优点:


HtmlUnit Driver


不会实际打开浏览器,运行速度很快。对于用


FireFox


等浏览器来做


测试的自动化测试用例,


运行速度通常很慢,


HtmlUnit Driver


无疑是可以很好地解决这个问


题。



缺点:它对


JavaScript


的支持不够好,当页面上有复杂


Java Script


时,经常会捕获不到页面


元素。

< br>


使用:



WebDriver driver = new HtmlUnitDriver();



2.2




FireFox Driver


优点:


FireFox


Dirver


对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对


JavaScript


的支持也非常完善,基本上页面上做的所有操作


FireFox Driver


都可以模拟。



缺点:启动很慢,运行也比较慢,不过,启动之后


Webdriver


的操作速度虽然不快但还是可


以接受的,建议不要频繁启停


FireFox Driver




使用:



WebDriver driver = new FirefoxDriver();





ff


浏览 器路径找不到,可显示指定路径



方法一



//


打开指定路径的


firefox,


方法


1


perty(


zilla



//


打开默认路径的


firefox


(路径指的是


firefox


的安装路径)



WebDriver diver = new FirefoxDriver();


方法二:



//


打开指定路径的


firefox,


方法


2


File pathToFirefoxBinary = new File(



FirefoxBinary


firefoxbin


=


new


FirefoxBinary(pathToFirefoxBinary);


WebDriver driver1 = new FirefoxDriver(firefoxbin,null);




2.3


InternetExplorer Driver


优点:直 观地模拟用户的实际操作,对


JavaScript


提供完善的 支持。



缺点:


是所有浏览器中运行速 度最慢的,


并且只能在


Windows


下运行,



CSS


以及


XPA


TH


的支持也不够好。



使用:



WebDriver driver = new InternetExplorerDriver();



2.4


操作浏览器




访问链接、获取


Title


、获取


url



(url); //


访问链接



le(); //


获取当前的


Title


rentUrl(); //


获取当前的


url




关闭浏览器



q


uit();


关闭所有页面




close();


关闭本次执行打开 的页面




(


driver


.navigate().to(



);








Navigation nav = te();



(



);




可以合并为一句




te().to(



);



3




使用操作



如果是链接,采用



Link Text




其他控件采用



id,name,classname


文本信息采用


css selector


3.1



如何找到页面元素



Webdrive r



findElement


方法可以 用来找到页面的某个元素,最常用的方法是用


id


< p>
name


查找。下面介绍几种比较常用的方法。



3.1.1 By ID


假设页面写成这样:





那么可以这样找到页面的元素:



通过


id


查找:



WebElement element = ement((


3.1.2 By Name


或通过


name


查找:



WebElement element = ement((


3.1.3 By XPATH


或通过


xpath


查找:



WebElement element =ement((



使用


XPATH


有如下几种方法定位元 素(相比


CSS


选择器,方法稍微多一点):

< br>


a


、通过绝对路径定位元素(不推荐!




ement((


b




通过相对路径定位元素



ement( (


返回查找到的第一个符合条件的元素



c




使


用索引定位元素


,


初始为

< br>1



ement((


返回查找 到的第二个符合条件的元素



d




使用< /p>


XPATH


及属性值定位元素



ement((




ement((



ement((


starts-with()



ement((


(@id



'user')]


ends-with





ement((



'name')]


c ontains()





ement((





e


、使用任意值来匹配属性及元素



WebElement ele


=



driver.


findElement


(


By.


xpath


(



))


;


//



1


配所有

input


元素中含有属性的值为


fuck


的元素





3.1.4 By Class Name


假设页面写成这样:





class=


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



Listcheeses = ements(ame(



3.1.5 By Link Text


假设页面元素写成这样:





href=



那么可以通过这样查找:



WebElement cheese =ement(xt(



根据链接的部分文字





cheese
>


WebElement cheese = ement(lLinkText(



3.1.6 By cssSelector


a



css


之类选择器




class=




class=


replyid=


href=


登录




href=


注册





第三方登录


:



例:


(ector(


b



css



id


选择器



ector(



e ment(ector(


在标签与


id


之间使用


#


连接




另外该方法也可简写为


ement( ector(


有点儿类似于


id


选择器




例:


ec tor(


c



css

< br>之属性选择器



使用元素的任何属性来定位元素





ement(ector(


标签名< /p>


[


属性名


='


属 性值


']



ement(ector (


标签名


[


属性名

^='xxx']


匹配属性值以


xxx

开头的元素



ement(ector(

< br>标签名


[


属性名


$$='xxx' ]


匹配属性值以


xxx


结尾的元素



ement(ector(


标签名


[


属性名


~='xxx']

< br>匹配属性值包含


xxx


的元素



例:


ector(




3.2



如何对页面元素进行操作



找到页面元 素后,怎样对页面进行操作呢?我们可以根据不同的类型的元素来进行一一说


明。



3.2.1


输入框(


text field or textarea





找到输入框元素:



WebElement element = ement((


在输入框中输入内容:



ys(“test”);



将输入框清空:



();


获取输入框的文本内容:



t();



ement((



3.2.2


下拉选择框


(Select)


找到下拉选择框的元素:



Select select = new Select(ement((




选择对应的选择项:



ByVisib leText(“mediaAgencyA”);





ByValue(“MA_ID_001”);




索引值从


0


开始



ByIndex(2);


不选择对应的选择项:



ctAll();


ctByValue(“MA_ID_001”);



ctByVisibleText(“mediaAgencyA”);



或者获取选择项的值:



SelectedOptions();


stSelectedOption();



3.2.3


单选项


(Radio Button)


找到单选框元素:



WebElement bookMode =ement((


选择某个单选项:



();


清空某个单选项:



();


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



cted();


3.2.4


多选项


(checkbox)


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



WebElement checkbox =ement((


();



();


cted();


led();


3.2.5


按钮


(button)


找到按钮元素:



WebElement saveButton = ement((


点击按钮:



();


或者


ement((



判断按钮是否


enable:



led ();


3.2.6


左右选择框



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



Select lang = new Select(ement((


ByVisibleText( “English”);



WebElement addLanguage =ement((


();


3.2.7


弹出对话框


(Popup dialogs)


Alert alert = To().alert();


();


s();


t();



ement(By.


id

< p>
(



)).click();

System.


out


.println(To().al ert().getText());


To().alert().accept();



3.2.8


表单


(Form)

-


-


-


-


-


-


-


-



本文更新与2021-02-28 05:41,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/679016.html

Selenium使用教程的相关文章