Selenium-WebDriver API by Example

Selenium-WebDriver API by Example

Selenium-Webdriver API
Selenium-Webdriver API

In this section, we will understand the Selenium-WebDriver API by Example. Therefore, let’s begin.

WebDriver is a tool for automating web application testing and to verify that they work as expected. Moreover, it aims to provide a friendly API that’s easy to explore and understand, which will help to make tests easier to read and maintain.
Once the project is set up, you can see that WebDriver acts just like any normal library. In other words, you are entirely self-contained and you usually don’t need to remember to start any additional processes before using it.
Additional steps are required to use Chrome Driver, Opera Driver, Android Driver, and iOS Driver
You’re now ready to write some code. An example in Java language is:

package org.openqa.selenium.example;
import org.openqa.selenium.By;
Secondly, import org.openqa.selenium.WebDriver;
After that, import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
Lastly, import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example {
public static void main(String[] args) {
Firstly, // Create a new instance of the Firefox driver
Secondly, // Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();

// And now use this to visit Google
driver.get(“http://www.google.com”);
Firstly, // Alternatively the same thing can be done like this
After that, // driver.navigate().to(“http://www.google.com”);

// Find the text input element by its name
WebElement element = driver.findElement(By.name(“q”));

// Enter something to search for
element.sendKeys(“Cheese!”);

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

// Check the title of the page
System.out.println(“Page title is: ” + driver.getTitle());

// Google’s search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith(“cheese!”);
}
});

// Should see: “cheese! – Google Search”
System.out.println(“Page title is: ” + driver.getTitle());

Selenium-WebDriver API Commands and Operations

In order to check what all we have in WebDriver, create a driver object from WebDriver and press the dot key. Further, this will list down all the methods of WebDriver. Methods followed by Object keyword are the generic methods get from Object Class in Java. Above all, you will find this method for every object of java language.

Method

A Java method is a collection of statements that comes together to perform an operation.

  • Method Name: Firstly, to access any method of any class, we need to create an object of the class. After that, all the public methods will appear for the object.
  • Parameter: Secondly, it is an argument that is passed to a method as a parameter to perform some operation. Moreover, every argument must pass with the same data type.
  • Return Type: Lastly, the method can return a value or return nothing. That is to say, if the void is stated after the method, it means the method is returning no value. And if it is returning any value, then it must depict the type of the value.

Fetching a Page

The first thing you would want to do with WebDriver is to navigate to a page. The normal way to do this is by calling “get”:

driver.get(“http://www.google.com”);

Dependent on several factors, including the OS/Browser combination, WebDriver may or may not wait for the page to load. In some circumstances, WebDriver may return control before the page has finished, or even started, loading. To ensure robustness, you need to wait for the element(s) to exist in the page using Explicit and Implicit Waits.

 

Get certified and unlock more opportunities. Practice and Validate your skills to become a Certified Agile Testing Professional Now!

Understanding Selenium
Understanding JMeter

Get industry recognized certification – Contact us

keyboard_arrow_up