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


一、下載必要的文件

    1、eclipse

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

[html]  view plain copy print?在CODE上查看代碼片派生到我的代碼片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>Selenium2Test</groupId>  
  5.     <artifactId>Selenium2Test</artifactId>  
  6.     <version>1.0</version>  
  7.     <dependencies>  
  8.         <dependency>  
  9.             <groupId>org.seleniumhq.selenium</groupId>  
  10.             <artifactId>selenium-java</artifactId>  
  11.             <version>2.25.0</version>  
  12.         </dependency>  
  13.         <dependency>  
  14.             <groupId>com.opera</groupId>  
  15.             <artifactId>operadriver</artifactId>  
  16.         </dependency>  
  17.     </dependencies>  
  18.     <dependencyManagement>  
  19.         <dependencies>  
  20.             <dependency>  
  21.                 <groupId>com.opera</groupId>  
  22.                 <artifactId>operadriver</artifactId>  
  23.                 <version>0.16</version>  
  24.                 <exclusions>  
  25.                     <exclusion>  
  26.                         <groupId>org.seleniumhq.selenium</groupId>  
  27.                         <artifactId>selenium-remote-driver</artifactId>  
  28.                     </exclusion>  
  29.                 </exclusions>  
  30.             </dependency>  
  31.         </dependencies>  
  32.     </dependencyManagement>  
  33. </project>  




    pom.xml修改保存后,Eclipse會自動把需要的jar包下載完成
    給項目工程配置所需要的庫
        工程右擊properties->java build path->add library
        
    2、測試Firefox(ExampleForFirefox.java)

[java]  view plain copy print?在CODE上查看代碼片派生到我的代碼片
  1. import org.openqa.selenium.By;  
  2. import org.openqa.selenium.WebDriver;  
  3. import org.openqa.selenium.WebElement;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5. import org.openqa.selenium.support.ui.ExpectedCondition;  
  6. import org.openqa.selenium.support.ui.WebDriverWait;  
  7.   
  8. public class ExampleForFireFox  {  
  9.     public static void main(String[] args) {  
  10.         // 如果你的 FireFox 沒有安裝在默認目錄,那么必須在程序中設置  
  11. //      System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
  12.         // 創建一個 FireFox 的瀏覽器實例  
  13.         WebDriver driver = new FirefoxDriver();  
  14.   
  15.         // 讓瀏覽器訪問 Baidu  
  16.         driver.get("http://www.baidu.com");  
  17.         // 用下面代碼也可以實現  
  18.         // driver.navigate().to("http://www.baidu.com");  
  19.   
  20.         // 獲取 網頁的 title  
  21.         System.out.println("1 Page title is: " + driver.getTitle());  
  22.   
  23.         // 通過 id 找到 input 的 DOM  
  24.         WebElement element = driver.findElement(By.id("kw"));  
  25.   
  26.         // 輸入關鍵字  
  27.         element.sendKeys("zTree");  
  28.   
  29.         // 提交 input 所在的  form  
  30.         element.submit();  
  31.            
  32.         // 通過判斷 title 內容等待搜索頁面加載完畢,間隔10秒  
  33.         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {  
  34.             public Boolean apply(WebDriver d) {  
  35.                 return d.getTitle().toLowerCase().endsWith("ztree");  
  36.             }  
  37.         });  
  38.   
  39.         // 顯示搜索結果頁面的 title  
  40.         System.out.println("2 Page title is: " + driver.getTitle());  
  41.            
  42.         //關閉瀏覽器  
  43.         driver.quit();  
  44.     }  



        運行正常的情況下就可以看到自動打開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代碼:
   

[html]  view plain copy print?在CODE上查看代碼片派生到我的代碼片
  1. <repositories>    
  2.         <repository>    
  3.           <snapshots>    
  4.             <enabled>false</enabled>    
  5.           </snapshots>    
  6.           <id>central</id>    
  7.           <name>Maven Repository Switchboard</name>    
  8.           <url>http://repo2.maven.org/maven2</url>    
  9.         </repository>    
  10.       </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



免責聲明!

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



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