Automation Using Selenium Webdriver

Monday, 17 October 2016

TestNG Selenium Simple Search Example

 TestNG Selenium Simple Search Example::
Selenium provides WebDriver as its API for automating web application testing. WebDriver drives the browser directly using each browser’s built-in support for automation.
In this example, we will use the WebDriver to open google and search for TestNG.
WebDriver is an interface so we still need to an implementation to run the test on an actual browser.
We will select the implementation based on the browser used. For example, if we are using firefox,
we will use FirefoxDriver. If it is chrome, we will use ChromeDriver. Since ChromeDriver works with Chrome through the chromedriver.exe, we need to make sure that the binary be placed somewhere on yoursystem’s path.
Points to be noted regarding the test class:
In @BeforeSuite method, initDriver(), we create the driver.
searchTestNGInGoogle() is our test method.
We call driver.navigate().to(url) to open the url.
Once the site is open, we need to get a handle on the search field so that we can type in the text to
 be searched.
When we call driver.findElement(By.name("q")), the WebDriver locates the search field using matching name attribute.
Next, we call element.sendKeys("TestNG") to type in the text “TestNG” in the search field.
The search is submitted on calling element.submit().
Finally, we wait for the results to be returned.
In @AfterSuite method, quitDriver(), we call driver.quit() to close the browser session.

TestNGSeleniumSimpleSearchExample:


package com.javacodegeeks.testng.selenium;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.ExpectedCondition;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.Test;

public class TestNGSeleniumSimpleSearchExample {

    private WebDriver driver;



    @BeforeSuite

    public void initDriver() throws Exception {

        System.out.println("You are testing in firefox");

        driver = new FirefoxDriver();

    }



    @Test

    public void searchTestNGInGoogle() {

        final String searchKey = "TestNG";

        System.out.println("Search " + searchKey + " in google");

        driver.navigate().to("http://www.google.com");

        WebElement element = driver.findElement(By.name("q"));

        System.out.println("Enter " + searchKey);

        element.sendKeys(searchKey);

        System.out.println("submit");

        element.submit();

        (new WebDriverWait(driver, 10)).until(new ExpectedCondition() {

            public Boolean apply(WebDriver d) {

                return d.getTitle().toLowerCase()

                        .startsWith(searchKey.toLowerCase());

            }

        });
        System.out.println("Got " + searchKey + " results");

    }



    @AfterSuite

    public void quitDriver() throws Exception {

        driver.quit();

    }

[TestNG] Running:

  OUTPUT:



You are testing in firefox
Search TestNG in google
Enter TestNG
submit
Got TestNG results



==============================================

TestNgSeleniumSuite

Total tests run: 1, Failures: 0, Skips: 0

===============================================

No comments:

Post a Comment