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:
- Browser Methods perform actions on a browser.
- WebElement Methods perform actions on WebElements.
- Navigation Methods load a web page, refresh a web page, or move backwards and forwards in our browser’s history.
- Wait Methods pause between execution statements.
- 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
- You’re here → Selenium Browser Methods (Chapter 1)
- WebElement Methods (Chapter 2)
- findElement
- click
- sendKeys
- getText
- getAttribute
- clear
- isDisplayed
- isEnabled
- Navigation Methods (Chapter 3)
- to
- refresh
- back
- forward
- Wait Methods (Chapter 4)
- pageLoadTimeOut
- ImplicitWait
- ExplicitWait
- FluentWait
- 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:
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:
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:
@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:
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:
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:
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:
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:
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:
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:
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 🎓💥