2013-02-19 51 views
5

我想用Java通過Selenium WebDriver項目自動化Firefox示例用於基於Java的Selenium WebDriver項目的Maven pom.xml適用於Firefox

正如我從來沒有使用過Selenium也不Maven我不熟悉如何設置一個合適的Maven pom.xml文件創建一個項目,然後可以導入到Eclipse之前。

http://docs.seleniumhq.org/docs/03_webdriver.jsp#introducing-webdriver我發現Opera瀏覽器下面的例子:

<?xml version="1.0" encoding="UTF-8"?> 
<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> 
    <dependencies> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-java</artifactId> 
      <version>2.28.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.opera</groupId> 
      <artifactId>operadriver</artifactId> 
     </dependency> 
    </dependencies> 
    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>com.opera</groupId> 
       <artifactId>operadriver</artifactId> 
       <version>1.1</version> 
       <exclusions> 
        <exclusion> 
         <groupId>org.seleniumhq.selenium</groupId> 
         <artifactId>selenium-remote-driver</artifactId> 
        </exclusion> 
       </exclusions> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 
</project> 

你會這麼好心,並告訴我如何去適應這個pom.xml申請Firefox瀏覽器,說Firefox 17

回答

4

通過使用selenium-java依賴關係,您可以獲得所有瀏覽器的所有java綁定。所以,你不需要改變你現有的任何東西。唯一會改變的是你的驅動程序初始化代碼,它是driver = new FirefoxDriver()而不是OperaDriver。

+0

謝謝!我錯誤地解釋了pom.xml文件中的opera依賴關係,並認爲需要相應的Firefox部分。 – Thomas853 2013-02-19 14:48:04

1

我知道,火狐17 2.28.0沒有得到支持,但瀏覽Selenium project homepage給了我新的見解 - 最新的版本是2.30.0所以更新此:

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

要這樣:

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

清潔,然後再試一次。希望它有幫助。這裏的一般建議是:始終使用最新版本的Selenium

+0

2.28不支持FF 17日以來在2.27加FF17本身支持按http://selenium.googlecode.com/git/java/CHANGELOG – 2013-02-19 14:46:36

+0

FF 17被2.28不適合我。只是說' – 2013-02-19 14:55:57

+0

好吧,我沒有嘗試過這樣的..只是要通過更改日誌:) – 2013-02-19 15:08:51

1

下面是從https://github.com/Ardesco/selenium-standalone-server-plugin採取了基本的POM:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

<groupId>com.lazerycode.selenium</groupId> 
<artifactId>maven-template</artifactId> 
<version>1.0-SNAPSHOT</version> 
<modelVersion>4.0.0</modelVersion> 

<name>Selenium Maven Template</name> 
<description>A Maven Template For Selenium</description> 
<url>http://www.lazerycode.com</url> 

<licenses> 
    <license> 
     <name>Apache 2</name> 
     <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> 
     <distribution>repo</distribution> 
     <comments>A business-friendly OSS license</comments> 
    </license> 
</licenses> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <selenium.version>2.45.0</selenium.version> 
    <overwrite.binaries>false</overwrite.binaries> 
    <browser>firefox</browser> 
    <threads>1</threads> 
    <remote>false</remote> 
    <seleniumGridURL/> 
    <platform/> 
    <browserVersion/> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.hamcrest</groupId> 
     <artifactId>hamcrest-all</artifactId> 
     <version>1.3</version> 
    </dependency> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-java</artifactId> 
     <version>${selenium.version}</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-remote-driver</artifactId> 
     <version>${selenium.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>com.codeborne</groupId> 
     <artifactId>phantomjsdriver</artifactId> 
     <version>1.2.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.testng</groupId> 
     <artifactId>testng</artifactId> 
     <version>6.8.8</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

<profiles> 
    <profile> 
     <id>selenium-tests</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <build> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <configuration> 
         <source>1.7</source> 
         <target>1.7</target> 
        </configuration> 
        <version>2.3.2</version> 
       </plugin> 
       <plugin> 
        <groupId>com.lazerycode.selenium</groupId> 
        <artifactId>driver-binary-downloader-maven-plugin</artifactId> 
        <version>1.0.7</version> 
        <configuration> 
         <rootStandaloneServerDirectory>${standalone.binary.root.folder}</rootStandaloneServerDirectory> 
         <downloadedZipFileDirectory>${project.basedir}/src/test/resources/selenium_standalone_zips</downloadedZipFileDirectory> 
         <customRepositoryMap>${project.basedir}/src/test/resources/RepositoryMap.xml</customRepositoryMap> 
         <overwriteFilesThatExist>${overwrite.binaries}</overwriteFilesThatExist> 
        </configuration> 
        <executions> 
         <execution> 
          <goals> 
           <goal>selenium</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-failsafe-plugin</artifactId> 
        <version>2.7.2</version> 
        <configuration> 
         <parallel>methods</parallel> 
         <threadCount>${threads}</threadCount> 
         <systemProperties> 
          <browser>${browser}</browser> 
          <screenshotDirectory>${project.build.directory}/screenshots</screenshotDirectory> 
          <remoteDriver>${remote}</remoteDriver> 
          <gridURL>${seleniumGridURL}</gridURL> 
          <desiredPlatform>${platform}</desiredPlatform> 
          <desiredBrowserVersion>${browserVersion}</desiredBrowserVersion> 

          <!--Set properties passed in by the driver binary downloader--> 
          <phantomjs.binary.path>${phantomjs.binary.path}</phantomjs.binary.path> 
          <webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver> 
          <webdriver.ie.driver>${webdriver.ie.driver}</webdriver.ie.driver> 
          <webdriver.opera.driver>${webdriver.opera.driver}</webdriver.opera.driver> 
         </systemProperties> 
         <includes> 
          <include>**/*WebDriver.java</include> 
         </includes> 
        </configuration> 
        <executions> 
         <execution> 
          <goals> 
           <goal>integration-test</goal> 
           <goal>verify</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles> 

這將:

  • 下載所有必需的硒庫
  • 下載獨立的驅動程序二進制文件(如chromedriver,phantomJS等)
  • Down加載PhantomJSdriver庫
  • 添加TestNG的一個測試框架
  • 添加在所有的Hamcrest匹配器進行明確的測試斷言(見https://code.google.com/p/hamcrest/wiki/Tutorial

原路段將帶你到一個基本的項目設置,你可以只需退房並運行,如果你不僅僅需要一個POM。

+0

好又簡單的pom。 – cbaldan 2015-05-18 17:07:09

+0

IDEA報告錯誤_無法解析符號'webdriver.chrome.driver'_。如何解決這個問題? – al0 2015-12-21 19:07:08

+0

通過IDEA運行Maven生命週期(例如驗證)。如果您未運行Maven進程,Maven將無法指定驅動程序位置。如果您想通過IDEA運行測試而不調用maven,則需要將「webdriver.chrome.driver」的值設置爲測試配置的一部分。 – Ardesco 2015-12-23 11:16:43

1

要僅使用FirefoxDriver實現,您不必使用selenium-java工件。相反,您可以添加Firefox工件的依賴關係。添加以下依賴於你的pom.xml:

<dependency> 
    <groupId>org.seleniumhq.selenium</groupId> 
    <artifactId>selenium-firefox-driver</artifactId> 
    <version>3.0.1</version> 
</dependency> 
相關問題