Selenium WebDriver with TestNG Sample Script

Selenium WebDriver with TestNG in Java - in this example, we will test the Google Calculator feature using Selenium for UI automation and TestNG as testing framework. You can download this java file here (right click on '' and click on 'save link as...' to save the sample selenium test script).

public class calculatorTest {
	//Tests google calculator
	public void googleCalculator(){
		//Create firfox driver's instance
		WebDriver driver = new FirefoxDriver();
		//Set implicit wait of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		//Launch google
		//Write 2+2 in google textbox
		WebElement googleTextBox = driver.findElement("gbqfq"));
		//Click on searchButton
		WebElement searchButton = driver.findElement("gbqfb"));;
		//Get result from calculator
		WebElement calculatorTextBox = driver.findElement("cwos"));
		String result = calculatorTextBox.getText();
		//Verify that result of 2+2 is 4
		Assert.assertEquals(result, "4");