Monday, April 11, 2011

Maven and Selenium

Sample pom for testng based selenium script

<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>Sample</groupId>
  <artifactId>Sample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 <build>
  <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.6</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
  </plugins>
 </build>
  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>5.8</version>
      <classifier>jdk15</classifier>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium</artifactId>
      <version>2.0b3</version>
      <type>pom</type>
    </dependency>
  </dependencies>
</project>



Sample  selenium code


package foo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


public class MavenBackedSelenium
{

    WebDriver webdriver;
    WebDriverBackedSelenium selenium;


    @BeforeClass
    public void firstTest()
    {
        webdriver = new FirefoxDriver();
        selenium = new WebDriverBackedSelenium(webdriver, "");

    }

    @Test
    public void openGoogle(){
        String page = "http://www.google.com";
        selenium.open(page);
        selenium.type("q", "water");
        selenium.click("search");

    }

    @AfterClass
    public void closeSession(){
        selenium.close();
    }

}

1 comment: