Selenium自動化測試環境搭建匯總(一):Selenium+Eclipse+Junit+TestNG


第一步 安裝JDK

  JDk1.7.

下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

一路猛擊‘下一步’,OK。安裝完成后配置環境變量:

  JAVA_HOME = E:\Java\Java\jdk1.7.0_15

  PATH = %JAVA_HOME%\bin

  CLASSPATH = .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar

配置完環境變量后,CMD命令行輸入:java -version,返回如下結果,則表示安裝成功:

 

 

第二步 下載Eclipse

下載地址:http://www.eclipse.org/download/ 

最新的Eclipse Standard 4.3, 198 MB,下載的都是不用安裝的,解壓出來后直接用。

 

第三步 下載Selenium IDE、SeleniumRC、IEDriverServer

下載地址:http://www.seleniumhq.org/download/

  1、  Selenium IDE:selenium-ide-2.5.0.xpi 用來在Firefox上錄制腳本。 

  2、  Selenium RC:selenium-server-standalone-2.40.0.jar 模擬服務器端,selenium 1.0執行腳本時需要單獨啟動該jar包, selenium webdriver無需單獨啟動。

  3、  IEDriverServer:IEDriverServer_Win32_2.40.0.zip IE驅動

  

 

這里,我將下載得到的所有文件,全存放在E:\eclipse\selenium下面,方便管理:

 

第四步 下載Firefox

 

下載地址:http://www.firefox.com.cn/download/

 

下載得到文件:Firefox-latest.exe,最好是下載Firefox 25簡體中文版,后續版本有人說通過Selenium會啟動不了Firefox。

 

 

第五步 安裝IDE、Firebug、Xpath checker、Xpath finder

 

安裝完Firefox后,打開Firefox,把前面下載的selenium-ide-2.5.0xpi拖放到Firefox,彈出下圖后,安裝即可。

Firebug、Xpath checker、Xpath finder,打開firefox瀏覽器,選擇工具――附加組件,打開附加組件管理器頁面,搜索firebug、Xpath。

將查詢到的firebug、xpath checker、xpath finder都裝上,重啟瀏覽器后生效: 

SeleniumIDE、Firebug和xpath的用法,可以百度Selenium私房菜(新手入門教程).pdf,里面有很好的說明。

 

第六步 啟動SeleniumRC

注意:selenium 1.0需要啟動單獨rc,webdriver則不需要啟動。

啟動seleniumRC的方法:
cmd命令行進入selenium-server-standalone-2.40.0.jar存放目錄,輸入如下命令
java -jar selenium-server-standalone-2.40.0.jar

 

為了方便,可以將啟動命令寫一個bat來執行,Run_selenium.bat,內容如下:

@echo off
cd E:\eclipse\selenium
E:
java -jar selenium-server-standalone-2.40.0.jar

 

第七步 Eclipse執行Selenium的Java實例

-----7.1

打開Eclipse,新建一個工程File—new—Java Project

 

-----7.2

輸入工程名:Selenum,next

-----7.3

接下來,窗口進入Java Settings,選擇Libraries,點擊Addlibrary。

引用Junit4的Jar包(E:\eclipse\plugins\org.junit_4.11.0.v2XXXX)。

然后點擊Add External Jars..,

引用Selenium相關的包(E:\eclipse\selenium),最終Libraries如下:

 

 

完成后,Java視圖如下:

 

 

-----7.4

右擊src,new->package新建一個包Selenium_Test,

再右擊包Selenium_Test,new->class,新建一個Class類Case1.java,最終效果如下:

 

 

-----7.5

下面我們來用IE瀏覽器執行一個實例,修改Case1.java,這里我們用selenium webdriver來寫代碼,代碼如下

package Selenium_Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Case1 {
public static void main(String[] args) {
   System.setProperty("webdriver.ie.driver",
     "E:\\eclipse\\selenium\\IEDriverServer.exe");//注意這里IEDriverServer.exe的文件存放路徑
   DesiredCapabilities ieCapabilities = DesiredCapabilities
     .internetExplorer();
   ieCapabilities
     .setCapability(
       InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
       true);
//new一個webdriver對象
   WebDriver driver = new InternetExplorerDriver(ieCapabilities);
//上面這一段是用來解決IE安全設置提示的
//通過webdriver的get方法調用瀏覽器,打開網頁:http://www.google.com.hk
   driver.get("http://www.google.com.hk");
    //通過頁面元素的name=q定位到查詢輸入框

   WebElement element = driver.findElement(By.name("q"));
  //在輸入框輸入‘hello Selenium!’
   element.sendKeys("hello Selenium!");
  //提交查詢
   element.submit();
//等待,超時則拋出錯誤
   try {
    Thread.sleep(3000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
//輸出當前頁面的title
   System.out.println("Page title is: " + driver.getTitle());
//關閉所有webdriver進程,退出
   driver.quit();
  }
 }

 

 

-----7.6

右擊Case1.Java,Run As—>Java Application,執行成功結果如下:

 -----7.7

接着,我們換成用selenium 1.0來寫代碼,Case1_1.java代碼如下:

package Selenium_Test;

import com.thoughtworks.selenium.*;

 

public class Case1_1 {

 

public static void main(String[] args)

{

        DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.baidu.com/");

       selenium.start();

       selenium.open("/");

       selenium.type("id=kw1", "selenium");

       selenium.click("id=su1");

       System.out.println("Page title is: " + selenium.getTitle());

       selenium.stop();

 }

}

 

 -----7.8

右擊Case1_1.Java,Run As—>Java Application,執行成功結果如下:

 

-----7.9

上面提示不能連接服務器,下面我們先執行前面的Run_selenium.bat,啟動selenium rc

 

-----7.10

再次右擊Case1_1.Java,Run As—>Java Application,執行成功結果如下:

 

以上例子,展示了使用selenium webdriverselenium 1.0寫代碼的執行區別。

兩者打開瀏覽器用的方法不同,當然,還有其他的方法也不同。且1.0還得另外啟動selenium rc

下面我們通過Junit來運行腳本,腳本需要修改一下,因為Junit的Java文件有它自己的格式。

第八步 Eclipse通過Junit執行Selenium的Java實例

-----8.1

右擊Selenium_Test,new->Junit test case 新建一個Case2.java

完成后如下:

 

-----8.2

修改Case2.java代碼如下:

 1 package Selenium_Test;
 2 
 3 import org.junit.*;
 4 import org.openqa.selenium.*;
 5 import org.openqa.selenium.firefox.FirefoxDriver;
 6 
 7 public class Case2 {
 8  WebDriver driver;
 9 
10  @Before
11  public void setUp() throws Exception {
12   driver = new FirefoxDriver();
13  }
14 
15  @Test
16  public void test_case2() throws Exception {
17   driver.get("http://www.google.com.hk");
18   WebElement element = driver.findElement(By.name("q"));
19   element.sendKeys("hello Selenium!");
20   element.submit();
21  }
22 
23  @After
24  public void tearDown() throws Exception {
25   System.out.println("Page title is: " + driver.getTitle());
26   driver.quit();
27  }
28 }

 

-----8.3 

右擊Case2.java,Run As—>Junit Test,執行成功結果如下:

 

第九步 Eclipse通過TestNG執行Selenium的Java實例

-----9.1

安裝 TestNG

 

  在 Eclipse 中,點擊 Help ->  Install new software ,在 add 欄中輸入http://beust.com/eclipse,在下面就會看到 TestNG.選中點擊安裝,按下一步直到安裝完,在線安裝會有點很慢。

安裝完重啟Eclipse后,在 window->Show View->other 里面選中Java->TestNG,就會出現TestNG選項了。

 

-----9.2

右擊包Selenium_Test,new->other->TestNG新建一個 TestNG 的測試類Case3.java。

完成后如下:

修改Case3.java腳本內容如下:

 1 package Selenium_Test;
 2 
 3 import org.testng.annotations.Test;
 4 import org.openqa.selenium.By;
 5 import org.openqa.selenium.WebDriver;
 6 import org.openqa.selenium.WebElement;
 7 import org.testng.annotations.BeforeMethod;
 8 import org.testng.annotations.AfterMethod;
 9 import org.openqa.selenium.firefox.FirefoxDriver;
10 
11 public class Case3 {
12  WebDriver driver;
13 
14  @BeforeMethod
15  public void beforeMethod() {
16 
17  }
18 
19  @AfterMethod
20  public void afterMethod() {
21   System.out.println("Page title is: " + driver.getTitle());
22   driver.quit();
23  }
24 
25  @Test
26  public void test_case3() {
27   driver = new FirefoxDriver();
28   driver.get("http://www.google.com.hk");
29   WebElement element = driver.findElement(By.name("q"));
30   element.sendKeys("hello Selenium!");
31   element.submit();
32  }
33 }

 

-----9.3

右擊Case3.java,Run as->TestNG Test,執行成功結果如下:

 

 執行完,會生成一個test-output文件夾,文件夾下面的index.html就是測試報告,如下:

 以上是在Eclipse下如何搭建Selenium的測試環境,包括直接執行.java,通過Junit執行.java,通過TestNG執行.java。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM