woensdag 20 juni 2012

maven and local selenium tests

INTRODUCTION:

Running selenium tests are a must have these days. There are a couple of ways to deal with that The two I know are:

1. The selenium plugin in Firefox or any other browser
2. Howto make them run in Eclipse with maven.

the second one is the one I am going to describe. 

DESCRIPTION


1. First of all you have to download eclipse at:
    http://www.eclipse.org/downloads/

2. Install it where you like and install the two maven plugins you need:
    A. Goto help, Eclipse MarketPlace and type Maven in the searchbox on top.
    B. Install the "Maven Integration for Eclipse WTP".
    C Install the "Maven Integration for Eclipse"
    D Go back to the Eclipse MarketPlace and search for the testNG plugin. Install this to. 

3. Now comes the exciting part Setting up an environment that helps you to run the selenium tests
    automatic during the build and how to run a local selenium server where you can start any
    selenium test you like.  

 Open your pom file and find the depdencies section. Put the following part in there:

<dependency>
   <groupId>org.seleniumhq.selenium.client-drivers</groupId>
   <artifactId>selenium-java-client-driver</artifactId>
   <version>1.0-beta-2</version>
</dependency>
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.1.1</version>
  <scope>test</scope>
</dependency>

After you done that find the <build><plugins> section Put the following part in there:

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>selenium-maven-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start-server</goal>
                        </goals>
                        <configuration>
                            <background>true</background>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
           
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!-- Skip the normal tests, we'll run them in the integration-test phase -->
                    <skip>true</skip>
                </configuration>
               
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Now the plumbing part is done. You have a skeleton to work with.

All you have to do now is to create a new testNG class. Make sure it extends from SeleneseTestCase.
 All the annotations used are imported from testNG.
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

 You can put the following lines in there to see if it works:
 
      @BeforeMethod
      public void beforeMethod() {
          try {
            setUp("https://www.google.com/", "*firefox");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }

      @Test
      public void testLabel() {
            selenium.open("https://www.google.com/");
            selenium.type("q", "Selenium with JUnit 4");
      }

HOW TO RUN IT:
1. Now your all setup and ready to go for your first selenium test with maven.
    You can do a clean install and it runs while it builds
2. You can also type the command:
     selenium:start-server and run the test as testNG test.
     In the teardown of the test you can do something like:
        @AfterMethod
        public void tearDown() throws Exception {
            selenium.stop();
        }

This worked for me and I hope it works for you. Enjoy.

Geen opmerkingen:

Een reactie posten