7

我想通过一个项目自动化Firefox使用。JavaSelenium WebDriver

由于我从未使用过,以前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

4 回答 4

4

使用 selenium-java 依赖项,您可以获得所有浏览器的所有 java 绑定。因此,您无需更改 pom 中已有的任何内容。唯一会改变的是您的驱动程序初始化代码,即 driver = new FirefoxDriver() 而不是 OperaDriver。

于 2013-02-19T14:44:34.900 回答
3

这是取自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>

这会:

  • 下载所有必需的 Selenium 库
  • 下载独立的驱动程序二进制文件(例如 chromedriver、phantomJS 等)
  • 下载 PhantomJSdriver 库
  • 添加 TestNG 作为测试框架
  • 添加所有 Hamcrest 匹配器以获得清晰的测试断言(请参阅https://code.google.com/p/hamcrest/wiki/Tutorial

原始链接将带您进入基本项目设置,如果您想要的不仅仅是 POM,您可以签出并运行它。

于 2015-04-11T09:49:30.993 回答
1

我知道,Firefox 17 在 2.28.0 中不受支持,但浏览Selenium 项目主页给了我新的见解 - 最新版本是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

于 2013-02-19T12:47:57.353 回答
1

要仅使用 FirefoxDriver 实现,您不必使用 selenium-java 工件。相反,您可以只添加对 Firefox 工件的依赖项。将以下依赖项添加到您的 pom.xml:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-firefox-driver</artifactId>
    <version>3.0.1</version>
</dependency>  
于 2016-11-08T10:15:45.050 回答