logo logo

Breakdown 5 Selenium Method Categories – Browser Methods (Chapter 1)

Selenium Browser Methods

In this tutorial of five articles, my goal is to share knowledge regarding each Selenium Method Category. There’s a total of 5 Selenium Method Categories. The categories are Browser Methods, WebElement Methods, Navigation Methods, Wait Methods, and Switch Methods. Each category has a group of methods that perform actions via Selenium:

  1. Browser Methods perform actions on a browser.
  2. WebElement Methods perform actions on WebElements.
  3. Navigation Methods load a web page, refresh a web page, or move backwards and forwards in our browser’s history.
  4. Wait Methods pause between execution statements.
  5. Switch Methods switch to alerts, windows, and frames. An alert is also known as a pop-up.

Selenium is an open source implementation used for automating web applications. The Selenium Suite consists of Selenium IDE, Selenium WebDriver, and Selenium Grid.

  • Selenium IDE is an add-on for Chrome and Firefox that’s used to record-and-playback the browser interactions.
  • Selenium WebDriver is a collection of language bindings (Java, JavaScript, Python, C#, and Ruby) that are used to automate a web browser.
  • Selenium Grid is used to run tests across multiple browsers, machines, and Operating Systems in parallel.

By the end of this tutorial series, you will have a solid foundation of Selenium automation. The code from this article is located on GitHub: https://github.com/RexJonesII/TestProject-TestNG

Tutorial Chapters

  1. You’re here → Selenium Browser Methods (Chapter 1)
  2. WebElement Methods (Chapter 2)
    • findElement
    • click
    • sendKeys
    • getText
    • getAttribute
    • clear
    • isDisplayed
    • isEnabled
  3. Navigation Methods (Chapter 3)
    • to
    • refresh
    • back
    • forward
  4. Wait Methods (Chapter 4)
    • pageLoadTimeOut
    • ImplicitWait
    • ExplicitWait
    • FluentWait
  5. Switch Methods (Chapter 5)
    • Switch To Frame
    • Switch To Alert
    • Switch To Window

Browser Methods are a group of methods that perform actions on a browser. In alphabetical order, the following image shows each Browser Method and its description:

Selenium Browser Methods

The following code utilizes all of the Browser Methods excluding getPageSource for TestProject’s Blog web page:

package testproject.blog4browser;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class BrowserMethods {
  WebDriver driver;
  
  @BeforeClass 
  public void setUp () {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Rex Allen Jones II\\Downloads\\Drivers\\chromedriver.exe");
    driver = new ChromeDriver ();
    driver.manage().window().maximize();		
  }
  
  @Test
  public void demoBrowserMethods () {
    driver.get("https://blog.testproject.io/");	
    
    String urlTestProject = driver.getCurrentUrl();
    System.out.println("What Is Test Project's Current URL? " + urlTestProject);
    
    String titleTestProject = driver.getTitle();
    System.out.println("What Is Test Project's Page Title? " + titleTestProject);
  }

  @AfterClass
  public void tearDown () {
    //driver.quit();
    driver.close();
  }
}

get Browser Method

The get method is a method that loads a new web page. In this example, the web page is TestProject’s Blog site. Here’s the syntax, method description, and parameter description for the get method:

get Browser Method

Let’s breakdown the syntax void org.openqa.selenium.WebDriver.get(String url):

  • void is the return type which returns no value.
  • org.openqa.selenium is the Selenium package.
  • WebDriver is the main interface used for testing. It represents an idealized web browser such as Chrome.
  • get(String url) is the method for loading a web page.

Selenium’s get method is the only Browser Method that receives a parameter. The parameter is String url. String is the Data Type while url is the value to load. Here’s an image of the code for the get method:

Code for the get Browser Method

@Test is a TestNG annotation that sets demoBrowserMethods as a Test Method. Annotations were covered in The Power of TestNG (Test Next Generation) article series. The method demoBrowserMethods contains a public access modifier with a void return type. Here’s a breakdown of the get method:

  • driver is the object reference for WebDriver interface.
  • get is the method receiving TestProject’s URL https://blog.testproject.io as the parameter.

The following image displays TestProject’s Blog web page after executing the get method:

TestProject Blog Web Page

getCurrentUrl Browser Method

The getCurrentUrl is a method that gets a string defining the current URL. Therefore, the same URL https://blog.testproject.io that loaded the Blog web page gets returned via getCurrentUrl method. Here’s the syntax, method description, and return description for getCurrentUrl:

getCurrentUrl

Notice the return Data Type is String. This means, the getCurrentUrl returns a String value. The following are images of Selenium’s code and value printed in the Console:

getCurrentUrl Selenium Code TestProject Blog URL

The code shows driver.getCurrentUrl assigned to String urlTestProject. On the next line, the value is returned via urlTestProject. As expected, the Console prints What Is Test Project’s Current URL? https://blog.testproject.io

getTitle Browser Method

The getTitle method is a method that loads the current page title. TestProject’s Blog title is Test Automation Blog | TestProject. In the following image, we see the syntax, method description, and return description for getTitle:

getTitle

The return Data Type is String. Therefore, this method returns a String value just like the getCurrentUrl method. The following images show the Automation Code, TestProject’s Page Title, and output via Console:

getTitle Automation Code

TestProject Blog Page Title

TestProject Blog Page Title

Statement line driver.getTitle gets the CurrentUrl then assigns the value to titleTestProject. After execution, the 3rd image shows What Is Test Project’s Page Title? Test Automation Blog | Test Project.

close vs. quit Browser Methods

Browser Methods close and quit operate similar to each other because they close the browser’s window. However, the quit method closes the window and quits the driver, while close only closes the window. Here’s the automation code and description of both methods:

close vs. quit Browser Methods

close vs. quit Browser Methods

close vs. quit Browser Methods

Personally, I believe most Selenium Automation Engineers use the quit method since it quits the driver. Here’s an example via Task Manager using the close method when the chromedriver.exe did not quit:

close method

The chromedriver.exe will not be available via the Task Manager when implementing the quit method.

In this tutorial, we covered the Selenium Browser Methods which perform actions on a browser. The next article in this series of Selenium Method Categories is of the WebElement Methods 🎓💥

 

About the author

Rex Jones II

Rex Jones II has a passion for sharing knowledge about testing software. His background is development but enjoys testing applications.

Rex is an author, trainer, consultant, and former Board of Director for User Group: Dallas / Fort Worth Mercury User Group (DFWMUG) and member of User Group: Dallas / Fort Worth Quality Assurance Association (DFWQAA). In addition, he is a Certified Software Tester Engineer (CSTE) and has a Test Management Approach (TMap) certification.

Recently, Rex created a social network that demonstrate automation videos. In addition to the social network, he has written 6 Programming / Automation books covering VBScript the programming language for QTP/UFT, Java, Selenium WebDriver, and TestNG.

✔️ YouTube https://www.youtube.com/c/RexJonesII/videos
✔️ Facebook http://facebook.com/JonesRexII
✔️ Twitter https://twitter.com/RexJonesII
✔️ GitHub https://github.com/RexJonesII/Free-Videos
✔️ LinkedIn https://www.linkedin.com/in/rexjones34/

Leave a Reply

FacebookLinkedInTwitterEmail