Getting started with Selenium2 (WebDriver)

As defined at the Selenium website,
Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.
Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks. 
In the other words, Selenium is a web automation engine. It provides APIs to interact with the web browser.
Coming back to the project, we are going to add dependency to the Selenium library as specified at http://docs.seleniumhq.org/download/maven.jsp
 <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.40.0</version>
    </dependency>  
Your pom.xml now is
<?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>SimpleTestWithJava</groupId>

    <artifactId>SimpleTestWithJava</artifactId>

    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>

            <groupId>org.easytesting</groupId>

            <artifactId>fest-assert-core</artifactId>

            <version>2.0M10</version>

        </dependency>

        <dependency>

            <groupId>org.testng</groupId>

            <artifactId>testng</artifactId>

            <version>6.1.1</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.seleniumhq.selenium</groupId>

            <artifactId>selenium-java</artifactId>

            <version>2.40.0</version>

        </dependency>

    </dependencies>

</project>

Wait for a while for maven updating the library. Adding a new sample class to work with Webdriver. In this example, we automate the search feature with Google.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * Created by nhien.truong on 3/3/14.
 */
public class FirstSampleWithWebDriver {
    public static void main(String[] args) {
        // Launch firefox browser
        WebDriver driver = new FirefoxDriver();

        // Open url
        driver.get("http://google.com");

        // write out the web page title
        System.out.println("The google page's title is: " + driver.getTitle());

        // close the browser
        driver.close();
    }
}
It's so simple, right? Next posts we are going to make more complicated one. Now, it's your turn to read document from the Selenium website and have some tries before I complete my posts. Good luck!

Không có nhận xét nào:

Đăng nhận xét