Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Selenium WebDriver Commands, Methods List

In this tutorial, we will learn about the most frequently used Selenium Webdriver commands for performing different operations in selenium tests.

Since we are using Selenium WebDriver with Java, commands are also called methods that are written in Java language.

To access any Selenium WebDriver methods, first, we need to create a driver object using a WebDriver reference variable and then all the public methods will appear for that object. Thus, we can call Selenium WebDriver methods using an object reference variable.

Let’s see a glance at the list of all possible methods provided by WebDriver in the below screenshot.

List of Selenium WebDriver Commands

To view all the above methods shown in the figure,  just open the Eclipse IDE loaded with Selenium Webdriver jar files, create a FirefoxDriver object for WebDriver, and press a dot key. It will show all of the possible methods provided by WebDriver. The following source code is given below:

package webDriverCommands; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
public class WebDriverMethods 
{ 
 public static void main(String[] args) 
 { 
// Create a driver object using WebDriver reference variable. 
   WebDriver driver = new FirefoxDriver(); 
    driver. 
  } 
}

Classification of Selenium WebDriver Commands


Selenium WebDriver commands can be classified as Get commands, Navigation commands, WebElement commands, Action commands, and Result commands.

Let’s see first Get command in detail.

Selenium WebDriver: Get Commands

Get commands are the most commonly used commands to perform the basic operations of WebDriver such as opening a browser, performing a few tasks, and then closing the browser. There are the following get methods provided by Selenium WebDriver that are as follows:

1. Get Command

Method:

  get(String arg) : void

➲ The get() method takes a String URL as a parameter and returns nothing.

➲ This method opens the specified URL in the current browser window. URL must be in the form of https://www.google.com. If the https is not included then it will throw a message “Cannot navigate to invalid URL”.

The general syntax to use get command is as follows:

Syntax:

  driver.get(https://www.google.com);

WebDriver Get command

For example:

  driver.get("https://www.google.com"); 
// Or It can be also written as 
  String URL = "https://www.scientecheasy.com"; 
   driver.get(URL);

2. Get Current URL Command

Method:

 getCurrentUrl() : String

➲ The getCurrentURL() method takes nothing as a parameter and returns URL of the web page currently loaded in the browser.

➲ This method is used to get string URL of the current web page loaded in the opened browser.
Syntax:
driver.getCurrentUrl();

For example:

  driver.getCurrentUrl(); 
// The above code can be also written as 
   String currentUrl = driver.getCurrentUrl(); // Since the return type of getCurrentUrl() method is String. So, we will store it using variable currentUrl with String data type.

3. Get Title Command

Method:

  getTitle() : String

➲ The getTitle() method takes nothing as a parameter and returns the page title of the currently loaded web page. If the web page has no title, it will return a null String.

➲ This method is used to get the title of the current web page.
Syntax:
driver.getTitle();

For example:

 driver.getTitle(); 
// The above code can also be written as 
   String title = driver.getTitle(); // Return type of getTitle is String.

4. Get Page Source Command

Method:

  getPageSource() : String

➲ The getPageSource() method accepts nothing as a parameter and returns the source code of the current web page.
➲ This method is used to get the page source code of the currently loaded web page.
Syntax:
driver.getPageSource();

For example:

  driver.getPageSource(); 
// Or 
  String pageSource = driver.getPageSource(); // Since return type of the this method is String. Therefore, the output must be stored in a variable with datatype String.

5. Get Text Command

Method:

  getText() : String

➲ The getText() method accepts nothing as a parameter and returns a string value.
➲ This method is used to retrieve the inner text of the specified element.
Syntax:
driver.findElement(By.locatorName(“Specified Element”)).getText();

For example:

String text = driver.findElement(By.id("India")).getText(); // id is name of locator. India is a specified element. 
System.out.println(text); 

// The above code can also be written as 
   WebElement element=driver.findElement(By.id("India")); 
   String text=element.getText(); // Return type of getText method is String. 
   System.out.println("Inner Text: " +text);
6. Get Tag Name Command
Method:
  getTagName() : String

➲ The getTagName() method takes nothing as a parameter and returns a String value.
➲ This method is used to get the tagName of the WebElement returned by the findElement(By) method.
Syntax:
element.getTagName(); // Here. element is a variable whose return type is WebElement.

For example:

   String tagName = driver.findElement(By.id("DownloadButton")).getTagName(); 
// or 
   WebElement element = driver.findElement(By.id("DownloadButton")); 
   String tagName = element.getTagName(); 
   System.out.println(tagName);
7. Get CSS Value Command

Method:

  getCssValue() : String

➲ The getCssValue() method accepts nothing as a parameter and returns a String value.

➲ This method is used to fetch the value of the CSS property of the given web element when it is invoked.
Syntax:
element.getCssValue();

8. Get Attribute Command

Method:

  getAttribute(String Name) : String

➲ The getAttribute() method takes the String as a parameter and returns a String value.

➲ It is used to fetch or get the value of the attribute of the WebElement.
Syntax:
element.getAttribute();

For example:

WebElement element = driver.findElement(By.id("DownloadButton")); 
String attributeValue = element.getAttribute("id"); //This will return "DownloadButton". 
System.out.println(attributeValue);

9. Get Size Command

Method:

  getSize() : int

➲ The getSize() method does not accept any parameter but returns the size of the element on the page.

➲ This method is used to fetch the width and height of the rendered element.
Syntax:
element.getSize();

For example:

WebElement element = driver.findElement(By.id("DownloadButton")); 
Dimension elementSize = element.getSize(); 
System.out.println(“Height :” + elementSize.height + ”Width : "+ elementSize.width);

10. Close Command

Method:

  close() : void

➲ The close method takes nothing as a parameter and returns nothing.
➲ This method is used to close only the browser window that web driver is currently controlling.
Syntax:
driver.close();

11. Quit Command

Method:

   quit() : void

➲ The quit() method accepts nothing as a parameter and returns nothing.
➲ This method is used to close all windows opened by WebDriver.
Syntax:
driver.quit();

Selenium WebDriver Methods Example Programs


Let us take an example program based on get(), getTitle(), and close() methods.

Scenario to Automate:
1. Launch the Firefox browser.
2. Visit the home page of Google.
3. Wait to be loaded web page fully.
4. Get the title of home page and print the title on the console.
5. Close browser.

Follow all the steps in the following program source code to understand the program clearly. So, let’s start coding.

Program source code 1:

package seleniumProject; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class GetCommandExample 
{ 
// To execute any program we need main main() method. So we will continue from the main method. 
 public static void main(String[] args) 
 { 
// Create a driver object using WebDriver reference. 
   WebDriver driver = new FirefoxDriver(); // FirefoxDriver class with no parameters. So, Default Firefox will be launche by Java program. Default Firefox profile is similar to launching the firefox in save mode. 

// Declaration of variable URL with data type String and store the URL of the webpage. 
   String URL = "https://www.google.co.in"; 
// Now call the get() method to open the URL of the web page in the existing browser. 
   driver.get(URL); // It takes String URL as a parameter and returns nothing. 

// Wait For Page To Load.
   driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 
// Print a message to display on the console that the website is opened successfully. 
   System.out.println("Website is opened successfully"); 

// Call getTitle() method using reference variable to get the title of the web page. 
   String getTitle = driver.getTitle(); 
// Now print title of web page on the console. 
   System.out.println("Title of home page: " +getTitle); 

// Now call the close() method using reference variable 'driver' to close the browser. 
   driver.close(); 
 } 
}
Output: 
       Website is opened successfully 
       Title of home page: Google

Explanation:
1. When the statement WebDriver driver=new FirefoxDriver(); is executed, it will launch the Firefox web browser.
2. When the statement driver.get(URL); will be executed, it will open the web page with a specified URL.
3. After visiting the home page, the driver will wait for 60 seconds on the current web page.

4. When the statement driver.getTitle() method will be executed, the driver will fetch the title of Google home page and then it will store title in a variable getTitle. After it, it will print the title on the console.

5. driver.close() method will close the existing browser.

How to verify title in Selenium WebDriver using Java?


In this section, we will verify the title of the web page in Selenium WebDriver using the String concept of Java.

Scenario to Automate:
1. Launch the web browser.
2. Open the URL https://www.facebook.com in the current browser.
3. Fetching home page title.
4. Verify actual tile of the home page with the expected title.
5. Print home page title on the console.
6. Close the browser.

Let’s understand the following source code related to the above scenario.

Program source code 2:

package seleniumProject; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
public class VerifyingTitle 
{ 
 public static void main(String[] args) 
 { 
// Create WebDriver reference. 
   WebDriver driver; // Till now the driver object is not created. 

// Create an driver object using WebDriver reference variable. 
   driver = new FirefoxDriver(); // Now driver object is created. 
   String URL = "https://www.facebook.com"; 
   driver.get(URL); // It will navigate the Facebook home page in the existing web browser. 

// Call getTitle() method to get the actual title of home page. 
   String actualTitle = driver.getTitle(); 
   String expectedTitle = "Facebook – log in or sign up"; 

// Use equalsIgnoreCase() method to verify actual title with expected title. 
   if(actualTitle.equalsIgnoreCase(expectedTitle))  // Verifying the home page title.
   { 
     System.out.println("Verification is success" ); 
     System.out.println("Home page title is: " + actualTitle ); 
   } 
  else 
  { 
    System.out.println("Verification is failed, Matched not found"); 
  } 
  driver.close(); 
 } 
}
Output: 
       Verification is success 
       Home page title is: Facebook – log in or sign up

Explanation:
1. In the preceding example program, we have created a driver object in two steps. First, we have declared WebDriver reference variable and then created a driver object of Firefox class using reference variable of WebDriver.

2. equalsIgnorecase() method performs the compersion between two strings by ignoring the case difference. i.e, it only checks the content characters.

If you any doubt in understanding it, you can go to below link to learn more in detail.How to create Object in Java with Example

Let’s one more practice a program based on getCurrentUrl() method and getPageSource() method.

Scenario to Automate:
1. First, launch the browser.
2. Open the Google home page.
3. Wait for the web page to be load.
4. Fetch the current URL of current web page and print it on the console.
5. Fetch page source of current web page.
6. Close the browser at last.

Let’s see the following source code to automate the above scenario.
Program source code 3:

package seleniumProject; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class GetCommandExample2 
{ 
  public static void main(String[] args) 
  { 
// Create a WebDriver reference and assign a driver object with it. 
   WebDriver driver; 
   driver = new FirefoxDriver(); 
// Declaration of variable URL with data type String and store the URL of the webpage. 
   String URL = "https://www.google.co.in"; 

// Call the get() method to open the URL of the web page in the existing browser. 
   driver.get(URL); 
// Wait For Page To Load. 
   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

// Call getCurrentUrl() method using reference variable to get the current url of the web page. 
   String getCurrentUrl = driver.getCurrentUrl(); 
// Print current URL of web page on the console. 
   System.out.println("Current URL of Home page: " +getCurrentUrl); 

// Call getPageSource() method to get page source of web page. 
   String getPageSource = driver.getPageSource(); 

// Now call the close() method using reference variable 'driver' to close the browser. 
    driver.close(); 
   } 
}
Output: 
       Current URL of Home page: https://www.google.co.in/

Hope that this tutorial has covered almost all important points related to Selenium WebDriver commands list with example programs. I hope that you will have understood the most frequently used methods of WebDriver class. In the next tutorial, we will familiarize with navigation and WebElement commands.

If you like this tutorial, please, share it on social networking sites for your friends.
Thanks for reading!!!Next ⇒ Navigation commands in Selenium⇐ PrevNext ⇒

The post Selenium WebDriver Commands, Methods List appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Selenium WebDriver Commands, Methods List

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×