White Box Testing
Test your code
   Home      selenium2WebDriver-ConfigAndSample
 
Configure and Setup Selenium webdriver project on Eclipse
 
Download Selenium Client Drivers and library for java from selenium website http://seleniumhq.org/download/
 
In the downloaded folder you will have two jar files for selenium and a lib folder
 
Create a new project in Eclipse and all downloaded jars through project properties
 
Configure and setup Selenium webdriver project through Maven
 
 
 
 
Selenium 2 webdriver example
 
1)Import required selenium packages
 
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
 
2)Create a new instance of the Firefox driver
                       
                        WebDriver driver = new FirefoxDriver();
 
                       
3)Now use the firefox instance driver to open a page URL
 
                        driver.get("http://www.whiteboxtest.com/selenium-test1.php");
 
 Alternative to open a URL driver.navigate().to("http://www.google.com");
 
                       
4)Find the text link by its name
 
                        WebElement someElement = driver.findElement(By.name("seleniumtest2"));
 
                       
5)Click the link text
 
                        someElement.click();
 
                       
6)Check the title of the page
 
                        System.out.println("Page title is: " + driver.getTitle());
 
                       
7)Close and clean firefox instance driver
 
                        driver.quit();
 
 
Download example code HelloWorld
 
 
 
 
Selenium 2 webdriver example with TestNG
 
To learn more about TestNG Java Unit Testing with TestNG
 
1)Import required selenium and TestNG packages
 
import org.testng.annotations.*;
import org.testng.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
 
 
2)Create webdriver interface reference as fields of test class
 
            WebDriver driver;
 
 
3)Call test method setup, and instanciate webdriver interface reference with FirefoxDriver
 
            @BeforeMethod
            public void setUp() {
                        driver = new FirefoxDriver();
            }
 
 
4)Call test method tearDown, and close firefox driver instance
 
            @AfterMethod
            public void tearDown() {
                        driver.quit();
            }
 
 
5)Call the test method
 
            @Test
            public final void testClick() {
                        driver.get("http://www.whiteboxtest.com/selenium-test1.php");
                        WebElement someElement = driver.findElement(By.name("seleniumtest2"));
                        someElement.click();
 
                        String title = driver.getTitle();
                        System.out.println("Page title is: " + title);
                        Assert.assertEquals(title,"White Box Testing");
}
 
 
Download example code HelloWorldWithTestNG