Eclipse里使用maven配置selenium-webdriver

上一篇 / 下一篇  2012-12-06 00:30:40 / 个人分类:selenium

Eclipse里使用maven配置selenium-webdriver51Testing软件测试网;|!c$_9@!uy2F

 

mx8I&w,[*eF0

1.      下载maven,如apache-maven-3.0.4-bin,将该文件夹bin目录添加到环境变量path

x$cSc o0

2.      建立文件夹webdriverSample,在文件夹建立pom.xml,内容及格式如下:

/C'xy#e'v1F-VI0

<?xml version="1.0" encoding="UTF-8"?>51Testing软件测试网0adp7M"a
<projectxmlns="http://maven.apache.org/POM/4.0.0"51Testing软件测试网O;ZT'Q0tL4\+{
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8?#I"vqHi.z0                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3\?.R r5_K:LL)} OY0       <modelVersion>4.0.0</modelVersion>
1{7At vX,Ia3g0       <groupId>MySel20Proj</groupId>
QFcs4da0       <artifactId>MySel20Proj</artifactId>
3L!VZ)m;u0       <version>1.0</version>51Testing软件测试网`0z"j&i)X(\8t4I
       <dependencies>51Testing软件测试网/vHE&{n*N(k [
           <dependency>
E!t[CabLs&W0               <groupId>org.seleniumhq.selenium</groupId>
,C;@-W+qy0               <artifactId>selenium-java</artifactId>
#C#cQ:V;W Gp2iH+s0               <version>2.25.0</version>
B@(p#Nt0           </dependency>
A,eB1AE2TrOd+R0           <dependency>
Mcox,`8U+i.kbjV5E0               <groupId>com.opera</groupId>
6} K!RH"L!y0               <artifactId>operadriver</artifactId>51Testing软件测试网[#Z*o/yS8xj
           </dependency>51Testing软件测试网,|uD|~;j
       </dependencies>51Testing软件测试网"vb&P-f^*S7?7LOL
       <dependencyManagement>51Testing软件测试网Ko6l)m8zE|
           <dependencies>51Testing软件测试网Z0Ki7r8~2_Aj
               <dependency>51Testing软件测试网 yj4\@-[
                   <groupId>com.opera</groupId>51Testing软件测试网?!d ^O6n;V*o
                   <artifactId>operadriver</artifactId>51Testing软件测试网,J;S]/l.L!o`(F8Y0D5Mw
                   <version>0.16</version>
@b5[L+E A+L x0                   <exclusions>
E~6z O XW t+M9RP0                       <exclusion>51Testing软件测试网/c$q:}2e,S.R
                           <groupId>org.seleniumhq.selenium</groupId>
n/W{0eSR]0                           <artifactId>selenium-remote-driver</artifactId>
a u\ hoYcX0                       </exclusion>51Testing软件测试网%hy4Z)z0T4n
                   </exclusions>51Testing软件测试网)h&j7tT2R(y
               </dependency>
)]-L cu;~&w0           </dependencies>51Testing软件测试网1C J%p,JWX{$G P
       </dependencyManagement>
O~%x$\l/D&V0</project>
M7LRR{h(?v?Ou0

2|R9A5y1gS#kM(x0

3.      cmd进入webdriverSample目录下,键入mvn clean install将会下载selenium和所有的依赖(默认路径为~/.m2/repository,即document and setting/user/.m2/repository,见maven目录conf文件夹/settings.xml),并将它们加入工程cmd里显示build success为成功51Testing软件测试网M'u^T,J { j*P

4.      再键入mvneclipse:eclipsebuild success为成功51Testing软件测试网1T*wP3vKB

5.      打开eclipse,导入该工程,选择windows菜单/preferences/java/build path/classpath variables里里new,选择foldername输入M2_REPO,路径为repository路径如“C:/Documents and Settings/***/.m2/repository”,一般都会有的,但我的eclipse里名称不是M2_ERPO,就识别不出来

Uj\ ^L?n0

6.      新建source folder/ srcpackage/pac,和class/cls,复制以下下代码到java文件中,选择工程,右键,选择run as /run configuration,右键java application选择newmain标签下,工程名输入该工程名称,main calss输入pac.Clss,点击run运行该程序,启动firefox并执行操作

pL&Lv)}Sq9Vx0

packageorg.openqa.selenium.example;
*oP'oU ^/@-Z051Testing软件测试网H U:b b$kv
importorg.openqa.selenium.By;51Testing软件测试网IGM/u#V8{?2X
importorg.openqa.selenium.WebDriver;51Testing软件测试网{T$fUEy#|
importorg.openqa.selenium.WebElement;51Testing软件测试网0PF kl;R${0l]W
importorg.openqa.selenium.firefox.FirefoxDriver;51Testing软件测试网k8s/S2j+o`#S.p0qs
importorg.openqa.selenium.support.ui.ExpectedCondition;51Testing软件测试网!j EJ`9z pV)})D
importorg.openqa.selenium.support.ui.WebDriverWait;51Testing软件测试网(X1p-[y!a)n

E} y5qn;s `V|;r x0publicclassSelenium2Example {51Testing软件测试网 R-zw%mS[ L_
   publicstaticvoidmain(String[]args){51Testing软件测试网&w+b B(B qI#`PH8In']
       // Create a new instance of the Firefox driver51Testing软件测试网+F!eU G}(R-D(N:@
       // Notice that the remainder of the code relies on the interface,
#Q|:d2l4KsuHT)D7e0       // not the implementation.
Ima }t A-a%U0       WebDriverdriver=newFirefoxDriver();51Testing软件测试网 `t[^!IH&j
51Testing软件测试网:|G [B4\\(R{
       // And now use this to visit Google
HMd%|C0       driver.get("http://www.google.com");51Testing软件测试网+|`2iO4f#v
       // Alternatively the same thing can be done like this51Testing软件测试网K#Z3l$JT q tg?I
       // driver.navigate().to("http://www.google.com");51Testing软件测试网n0[Pi,vW"rD(x

P7UMs6p g;b*^ ~'t[c1g D0       // Find the text input element by its name
wf~0K?;d8|0       WebElementelement=driver.findElement(By.name("q"));51Testing软件测试网S#U"`tr,p
51Testing软件测试网/} rnL-JwY#c3_/K
       // Enter something to search for51Testing软件测试网 v t.\JW S
       element.sendKeys("Cheese!");51Testing软件测试网%t/W)QP\wCs

fb |0O'My0NhD4[0       // Now submit the form. WebDriver will find the form. for us from the element51Testing软件测试网8\8[ c1p}
       element.submit();
_ Fm]-Hx_%gw`0
rIKGN"`#X:Q \0       // Check the title of the page51Testing软件测试网wiu {5GkIM:c"V%P
       System.out.println("Page title is: "+driver.getTitle());51Testing软件测试网] m4a/e`2W
       51Testing软件测试网 vS7{\(q2[G\
       // Google's search is rendered dynamically with JavaScript.51Testing软件测试网7Tv p puwL;I-C
       // Wait for the page to load, timeout after 10 seconds51Testing软件测试网I+r#~2a^$k
       (newWebDriverWait(driver,10)).until(newExpectedCondition<Boolean>(){
(~*y WpI3CX0           publicBooleanapply(WebDriverd){51Testing软件测试网Zny D&W:D1xO1Z[
               returnd.getTitle().toLowerCase().startsWith("cheese!");51Testing软件测试网M3g3b c^/T.V
           }51Testing软件测试网#\(f9qP f4h8t;j
       });
9y G%`/RV8ua l0
:Z Zpn N7P9S ^|0       // Should see: "cheese! - Google Search"51Testing软件测试网uj ^ K g;p
       System.out.println("Page title is: "+driver.getTitle());
_ZND+O#HBC0       51Testing软件测试网 Z#?fz }D4u1U
       //Close the browser51Testing软件测试网i$b/q;N~k v2^Ci
       driver.quit();
jbB ZuY0   }51Testing软件测试网,Q~6Z5q(y2TL}t
}51Testing软件测试网nmX0Czq/aj3Q;M

Dy*pN:r'b5Q5i X|J0

TAG:

 

评分:0

我来说两句

Open Toolbar