Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example Maven pom.xml for Java based Selenium WebDriver project for Firefox

I would like to automate Firefox using Java through a Selenium WebDriver project.

As I never used Selenium nor Maven before I'm not familiar with how to set up a suitable Maven pom.xml file to create a project, which then can be imported into Eclipse.

On http://docs.seleniumhq.org/docs/03_webdriver.jsp#introducing-webdriver I found the following example for the opera browser:

<?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>

Would you be so kind and show me how to adapt this pom.xml to apply for the Firefox browser, say Firefox 17?

like image 680
Thomas853 Avatar asked Feb 19 '13 12:02

Thomas853


2 Answers

With selenium-java dependency you get all java bindings for all browsers. So, you do not need to change anything in your pom from what you already have. The only thing that would change is your driver initialization code, which is driver = new FirefoxDriver() instead of OperaDriver.

like image 118
niharika_neo Avatar answered Oct 01 '22 13:10

niharika_neo


Here's a basic POM taken from https://github.com/Ardesco/selenium-standalone-server-plugin:

<?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>

This will:

  • Download all required Selenium libraries
  • Download the standalone driver binaries (e.g. chromedriver, phantomJS, etc)
  • Download the PhantomJSdriver libraries
  • Add TestNG as a test framework
  • Add in all the Hamcrest matchers for clear test assertions (see https://code.google.com/p/hamcrest/wiki/Tutorial)

The original link will take you to a basic project setup that you can just check out and run if you want more than just a POM.

like image 21
Ardesco Avatar answered Oct 01 '22 12:10

Ardesco