菜鳥學自動化測試(八)----selenium 2.0環境搭建(基於maven)


 

之前我就講過一種方試來搭建selenium rc 的環境搭建,那個文章是基於selenium RC 1.0的的方式,目前官網已經不提供1.0的下載了,我所提供的下載鏈接是CSDN上的。

其實,如果大家熟悉maven的話,selenium RC將變成易常簡單,當然,我的操作還是基於java語言陣營的。

前提:你要有maven環境。關於maven環境的搭建,請參考本博客的關於maven的文章。左側導航欄有maven環境搭建的相關文章。

 

第一步:

Cmd打開命令提示符號,創建一個maven項目,命令如下:

mvn archetype:generate -DgroupId=MySel20Proj -DartifactId=MySel20Proj  -Dversion=1.0

 

打開創建的項目,找到項目目錄下面的pom.xml文件,打開,在<dependencies>...</dependencies>之前加上如下內容:

 

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.16.1</version>
</dependency>

 

我們將junit的版本改為4.2 。修改之后完整的pom.xml代碼如下:

 

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>MySel20Proj</groupId>
<artifactId>MySel20Proj</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>MySel20Proj</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.16.1</version>
</dependency>
</dependencies>
</project>

 

Ok.!現在在命令提示符號下切換到項目目錄下:

 

Djava\MySel20Proj> mvn eclipse:clean 

 

Djava\MySel20Proj> mvn eclipse:eclipse    執行這個需要下載相應的包,時間會久一些。

 

現在把我們構建好的項目導入eclipse中,如果有問題請參考我的maven文章。

 

http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.html

 

我們依然用之前的代碼進行測試,(這段代碼還是基於selenium 1.0selenium RC

 

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

public class test extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iexplore",
"http://www.baidu.com/");
selenium.start();
}

@Test
public void testTest() throws Exception {
selenium.open("/");
selenium.type("id=kw", "aaaa");
selenium.click("id=su");
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

 

當然,你可以用可用selenium IDE錄制腳本,將腳本導出成java所類型的語言進行測試,方式參考:
http://www.cnblogs.com/fnng/archive/2011/10/19/2217506.html  

--上面的代碼與方式,已經不是推薦使用的方式了。因為selenium 2.0來了,selenium RC的舞台該退場了。

 

 

詳解maven方式添加插件

 

為了得到一個不同的selenium Maven工件的概述,請看下圖,它顯示selenium Maven工件以及最重要的類以及這些類接口之間的依賴關系:

 

如果你想使用不同的WebDriver來運行代碼,如:FirefoxDriver 你不必依靠selenium Java 構建(其中有很多的傳遞依賴),你只需要需要在pom.xml文件中添加相應的插件就可以了。

 

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.18.0</version>
</dependency>

然后,在cmd(命令提示符)下,將項目重新構建一下就行了(再次執行mvn eclipse:eclipse 命令)。Maven自動下載我們所需要的FirefoxDriver 相關jar包。

 

 

如果您正在使用DefaultSelenium(或RemoteWebDriver的 實現),你仍然需要啟動Selenium Server。最好的辦法是下載selenium Server standalone.jar ,只是用它。此外,您還可以嵌入到自己的項目Selenium Server,下面的依賴關系,如果你添加到你的pom.xml:

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.18.0</version>
</dependency>

注意:這個selenium Server 產品依賴於servlet-API-2.5 產品。如果你的項目運行在一個web應用程序容器的依賴,你應該排除。

 

 

第三方的瀏覽器不被支持,請用seleniumhq 開發的插件;

<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>0.10</version>
</dependency>

 

以上參考內容:

 

http://seleniumhq.org/docs/03_webdriver.html

http://seleniumhq.org/download/maven.html



 

什么是Selenium 2.0 

 

從較高的層次看,Selenium由三種工具組成。第一個工具Selenium IDE,是Firefox的擴展插件,支持用戶錄制和回訪測試。錄制/回訪模式?存在局限性,對許多用戶來說並不適合,因此第二個工具——Selenium WebDriver提供了各種語言環境的API來支持更多控制權和編寫符合標准軟件開發實踐的應用程序。最后一個工具——Selenium Grid幫助工程師使用Selenium API控制分布在一系列機器上的瀏覽器實例,支持並發運行更多測試。在項目內部,它們分別被稱為“IDE”、“WebDriver”和“Grid”。 

 

而現在我們大家能看到的:selenium2.0 = selenium1.0+ webdriver 

 

 

什么是WebDriver?

 

WebDriver是一個自動化測試Web應用程序的工具,尤其以驗證它們是否如預期運作。它旨在提供一個友好的API,它很容易探討和理解,更容易比的Selenium-RC(1.0)API,這將有助於使你的測試更容易閱讀和維護使用。它不依賴任何特定的測試框架,因此它可以用來在一個單元測試,或從一個普通的舊的“main”的方法同樣。 

下一節來學習如何selenium 2.0的新東東 webdriver ! 




免責聲明!

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



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