WRITE AUTOMATION COMANDS IN SELENIUM WEBDRIVER USING GENERIC SELENIUM COMMANDS

In this blog, you will understand about one of the approach to write automation commands in Selenium WebDriver.

A common approach used by most automation engineers is to use the generic selenium commands like
click()
sendKeys()
to perform their automation task. This is shown in the example below:
WebElement textbox = driver.findElement(By.name("q"));
textbox.sendKeys("selenium commands");
WebElement button = driver.findElement(By.name("btnG"));
button.click();

In the code snippet shown above generic selenium commands are used to first fill the textbox with a text value and then clicked the button. You can expand the snippet shown above to perform a Google search as shown below in a complete Java example:




Java Source code:

package com.helloselenium.selenium.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class UsingGenericSeleniumCommands {

    public static void main(String args[]){

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://google.com");

        WebElement textbox = driver.findElement(By.name("q"));
        textbox.sendKeys("selenium commands");
        WebElement button = driver.findElement(By.name("btnG"));
        button.click();

        driver.quit();
    }
}

Below are the line by line code explanation for above Java program:




package com.helloselenium.selenium.test;
Above code is package structure for current program file.
Following code is for required packages.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
below code is to maximize the driver instance.
driver.manage().window().maximize();



Following code is to open the link in browser.
driver.get("http://google.com");
You can also use the following code is to open the link in browser.
driver.navigate().to("http://google.com");
Following code is to find the file input type webelement on the page.
WebElement textbox = driver.findElement(By.name("q"));
Following code is to type into webelement.
textbox.sendKeys("selenium commands");
Following code is to find the file input type webelement on the page.
WebElement button = driver.findElement(By.name("btnG"));
Following code is to click on the webelement.
button.click();
Following code is to quit the webdriver instance.
driver.quit();
You can also use the following code is to close the webdriver instance.
driver.close();
Output of the above program should be:


Post a Comment

0 Comments