1. 程式人生 > >自動化測試【Maven+Eclipse+Selenium+Java環境搭建和測試】

自動化測試【Maven+Eclipse+Selenium+Java環境搭建和測試】

一、下載必要的檔案

    1、eclipse

    2、jdk

jdk官網

    3、selenium IDE、Selenium Server、Selenium Client Drivers(java)等等

Selenium下載地址  備註:需要代理伺服器才能下載 我使用的是太太貓

    4、maven安裝、配置等

二、安裝
    1、Eclipse解壓縮就可以用了
    2、jdk安裝、配置變數等
    3、Selenium相關的安裝
    4、maven

        最新版本的Eclipse已經自帶maven

三、執行
    1、Eclipse建個maven工程
,建成後,直接修改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>Selenium2Test</groupId>
        <artifactId>Selenium2Test</artifactId>
        <version>1.0</version>
        <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>2.25.0</version>
            </dependency>
            <dependency>
                <groupId>com.opera</groupId>
                <artifactId>operadriver</artifactId>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.opera</groupId>
                    <artifactId>operadriver</artifactId>
                    <version>0.16</version>
                    <exclusions>
                        <exclusion>
                            <groupId>org.seleniumhq.selenium</groupId>
                            <artifactId>selenium-remote-driver</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>



    pom.xml修改儲存後,Eclipse會自動把需要的jar包下載完成
    給專案工程配置所需要的庫
        工程右擊properties->java build path->add library
        
    2、測試Firefox(ExampleForFirefox.java)
        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.firefox.FirefoxDriver;
        import org.openqa.selenium.support.ui.ExpectedCondition;
        import org.openqa.selenium.support.ui.WebDriverWait;
        
        public class ExampleForFireFox  {
            public static void main(String[] args) {
                // 如果你的 FireFox 沒有安裝在預設目錄,那麼必須在程式中設定
        //      System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
                // 建立一個 FireFox 的瀏覽器例項
                WebDriver driver = new FirefoxDriver();
        
                // 讓瀏覽器訪問 Baidu
                driver.get("http://www.baidu.com");
                // 用下面程式碼也可以實現
                // driver.navigate().to("http://www.baidu.com");
        
                // 獲取 網頁的 title
                System.out.println("1 Page title is: " + driver.getTitle());
        
                // 通過 id 找到 input 的 DOM
                WebElement element = driver.findElement(By.id("kw"));
        
                // 輸入關鍵字
                element.sendKeys("zTree");
        
                // 提交 input 所在的  form
                element.submit();
                 
                // 通過判斷 title 內容等待搜尋頁面載入完畢,間隔10秒
                (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver d) {
                        return d.getTitle().toLowerCase().endsWith("ztree");
                    }
                });
        
                // 顯示搜尋結果頁面的 title
                System.out.println("2 Page title is: " + driver.getTitle());
                 
                //關閉瀏覽器
                driver.quit();
            }


        執行正常的情況下就可以看到自動開啟Firefox視窗,訪問baidu.com,然後輸入關鍵字並查詢

在搭建、測試中出現了幾個問題:

問題1:
    Description    Resource    Path    Location    Type ArtifactTransferException: Failure to transfer com.google.code.findbugs:jsr305:jar:1.3.9 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact com.google.code.findbugs:jsr305:jar:1.3.9 from/to central (http://repo.maven.apache.org/maven2): repo.maven.apache.org    pom.xml    /lesson    line 2    Maven Dependency Problem
解決方案:

    可能是連不上http://repo1.maven.org/maven2這個倉庫,在pom.xml檔案加一下下面的配置試試看。  
xml程式碼:
   
<repositories>  
        <repository>  
          <snapshots>  
            <enabled>false</enabled>  
          </snapshots>  
          <id>central</id>  
          <name>Maven Repository Switchboard</name>  
          <url>http://repo2.maven.org/maven2</url>  
        </repository>  
      </repositories>

問題2:
    ava檔案(.java)右鍵run as沒有java application
    
解決方法:
    public static void main(String[] args)

    這幾個必不可少
    public
    static
    void
    main
    String[] arg 或者 String arg[]
    可能少掉其中一個元素

問題3:
    Caused by: com.thoughtworks.selenium.SeleniumException: Failed to start new browser session: java.lang.RuntimeException: Browser not supported: http://www.baidu.com (Did you forget to add a *?)
    分析:出現這個錯誤,是說明你的 FireFox 檔案並沒有安裝在預設目錄下,這時候需要在最開始執行:System.setProperty 設定環境變數  "webdriver.firefox.bin" 將自己機器上 FireFox 的正確路徑設定完畢後即可。
解決方法:
    如果你的 FireFox 沒有安裝在預設目錄,那麼必須在程式中設定
     System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");

    3、測試IE
    Selenium 主要也就是針對 FireFox 和 IE 來製作的,所以把 FireFox 的程式碼修改為 IE 的,那是相當的容易,只需要簡單地兩步:
    1)把 ExampleForFireFox.java 另存為 ExampleForIE.java
    2)把 WebDriver driver = new FirefoxDriver(); 修改為 WebDriver driver = new InternetExplorerDriver();

    3)一般大家的 IE都是預設路徑,所以也就不用設定 property 了

問題1:
    執行出現
    WebDriverException: Message: u'Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones.'
解決方法:

進入ie8工具選項->進入“安全”tab->保證"internet" "本地intranet" "可信站點" "受限站點"四個安全設定下的"啟用保護模式(要求重新啟動Internet Explorer)"的選擇是一致的:要麼都選,要麼都不選。由於ie8在預設情況下,只"受限站點"安全設定內此選項是啟用的,所以才會出現上述問題中的exception