Wednesday, October 9, 2013

Automating a basic web page using Selenium WebDriver + Java + Mavan +TestNG

It is very simple.Just follow these three steps.

1. update the pom file

 <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.21.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-control</artifactId>
            <version>2.0rc2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <groups></groups>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

2. Create the page class

Initialize the constructor on page object.Same time you can call the url.

    public YourFirstPage(WebDriver driver)  {

        this.driver = driver;
        driver.get("http://abc.lk");
     
    }

Then you can do what ever you want to do(filling forms,assertions,validations,....etc) in the page.

 public void setUserName(String userName) {

        WebElement uNameFld = driver.findElement(By.id("some id"));
        uNameFld.clear();
        uNameFld.sendKeys(userName);
    }

3. Create the test case class

This class is to write your test cases by calling methods in page class.

Before class

 @BeforeClass(alwaysRun = true)
    public void BeforeTest() throws IOException, InterruptedException {

         DesiredCapabilities capability = DesiredCapabilities.firefox();
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
        firstPage = new FirstPage(driver);
}

After Class

@AfterClass(alwaysRun = true)
    public void AfterMethod() {

        driver.close();
    }

That's it!
You can write test cases here now.

See my example code below to automate to open and validate links in www.abc.lk.





No comments:

Post a Comment

-----KusH----