javamavenselenium-webdriverpom.xmlparameterization

Using parameters from pom.xml to switch environments for selenium tests + using mvn command line arguments


My goal: using parameters to switch environments in my tests for instance:

mvn test google -> Tests goes to google site

mvn test bing -> bing site

"I need to feed my tests which environment is the target and it should comes from the pom.xml and using them as arguments."

It would be very useful for teamcity/jenkins integration as well. Apart from that, I need to use url as variables in my tests. How can I do that?

Profiles can be a solution in pom.xml?

<profiles>
    <profile>
        <id>google</id>
        <properties>
            <base.url>http://www.google.com</base.url>
        </properties>
    </profile>
    <profile>
        <id>bing</id>
        <properties>
            <base.url>http://www.bing.com</base.url>
        </properties>
    </profile>
</profiles>

From build section:

<configuration>
   <systemProperties>
       <base.url>${base.url}</base.url>
   </systemProperties>
</configuration>

But How can I use system properties and overall the approach is good? Thanks!


Solution

  • You can configure the maven-surefire-plugin to include only specific test classes and the run mvn test. By default, mvn will run all these:

    but you could specify the tests you want to include like this:

    <project>
      [...]
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
            <configuration>
              <includes>
                <include>Sample.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>
    

    or exclude:

    <project>
      [...]
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
            <configuration>
              <excludes>
                <exclude>**/TestCircle.java</exclude>
                <exclude>**/TestSquare.java</exclude>
              </excludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>
    

    Having said that, this is probably not the best design and generally, you should be using some testing framework which you then can configure according to your needs. Few examples are (or combination of): jUnit, TestNG, Cucumber, Spring.

    In Cucumber, for example, you can have tags which then you can configure as a part of your test execution. If you use Jenkins, you might have something like this in your build fild:

    clean install -Dcucumber.options="--tags @Google
    

    or

    clean install -Dcucumber.options="--tags @Bing
    

    In Spring, you can have profiles that you can run like this as Jenkins job:

    mvn clean test -Dspring.profiles.active="google"
    

    EDIT

    Alternatively, you can define a custom property in your pom like this:

    <properties>
       <myProperty>command line argument</myProperty>
    </properties>
    

    And then pass it from command line like this:

    mvn install "-DmyProperty=google"
    

    EDIT2

    Providing a -D prefixed value in command line is a way of setting a system property. You can perform this action from Java code itself like this:

    Properties props = System.getProperties();
    props.setProperty("myPropety", "google");
    

    or simply:

    System.setProperty("myPropety", "google");