Selenium WebDriver Basics – First Webdriver Program

Selenium Webdriver basics

Selenium is not that harder as you think. Did you ever looked at it and realized it is tough or Did someone told you ? In Most cases, the answer would be the second one correct ? yes. So in this post, we will look at the basic webdriver commands and First webdriver program.

 

Browser Commands in WebDriver:

Just concentrate on the syntax and the functional operation of the command. At the end, we will implement them in our first webdriver program.

To Open URL

Syntax: driver.get (URL);
The  command will open the URL

Maximise

Syntax: driver.manage ().window ().maximize ();
The command will maximize the browser.

Navigation:

driver.navigate ().back ();
It will move back to browser history.

driver.navigate ().forward();
It will move forward to browser history.

driver.navigate().refresh();
It will refresh the current page.

driver.navigate().to(url);
It will load the new url in current page.

Close:

driver.close();

It will close the browser.

Quit:

driver.quit();

It will close the browser and kill the driver instance.

 

Web Driver Interaction Commands:

Before Interacting with browser we need to create a web element object.

Syntax:

WebElement element =driver.findelement (By.locatorname(“locator”);

From above element we can inherit commands like below.

element.clear();
It will clear the value in edit box.

element.click();
It will click on clickable elements like Buttons ,Images , Check boxes , links and radio buttons etc.

element.sendKeys(“Value to be Enter”);
It will enter the value in to edit box.

Select:

Select select = new Select(driver.findElement(By.loacatorName(locator)));
To select value from drop downs.

select.selectByIndex(index);
It will select options based on Index like 0,1,2

select.selectByValue(value);
It will select the option based on value. That is, when given “foo” this would select an option like:
<option value=”foo”>Bar</option>

JUST 3 steps – Browser commands, Interaction commands and Webdriver program

select.selectByVisibleText(text);
It will select the option based on text.
That is, when given “Bar” this would select an option like?<option value=”foo”>Bar</option>

Deselect:

select.deselectByIndex(index)
It will deselect options based on Index like 0,1,2 …

select.deselectByValue(value);
It will deselect the option based on value.

select.deselectByVisibleText(text);
It will deselect the option based on text.

select.deSelectAll();
It will deselect all options selected currently.

 

First web driver program:

package com.training;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleSearch {

public static WebDriver driver;

      public static void main(String[] args) {

      driver = new FirefoxDriver();

      driver.get("https://www.google.co.in/");

      driver.manage().window().maximize();

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

      element.sendKeys("selenium testing");

      driver.findElement(By.name("btnG")).click();

      driver.quit();

      }

}

 

QUESTION FOR YOU:

SO, what did you understand from the webdriver program. Please put in your answers in the comment section below about what does the program do upon execution.