In our previous lessons, we familiarized you with the foundational structure and attributes of WebDriver and the necessary setup needed to commence Selenium WebDriver. It is presumed that you have properly configured your system with all required tools and packages, and our next step is to execute our first WebDriver testing script.
Moving on to the subsequent Selenium WebDriver guide, we will craft our WebDriver program. Additionally, we will touch upon the commonly used WebDriver commands that are basic. We will also delve into the strategies for identifying UI elements and integrating them into test scripts. The discussion will also involve an in-depth coverage of Get Commands.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Lessons Covered:
Scripting
For scripting, we will use the “Learning_Selenium” project from the prior guide and test it on the “gmail.com” website.
Scenario:
- Start the browser and open “Gmail.com”.
- Confirm the page title and print the confirmation result.
- Input the username and password.
- Select the Sign in button.
- Shut down the web browser.
Step 1: Generate a new Java class called “Gmail_Login” within the “Learning_Selenium” project.
Step 2: Replicate and input the following code into the “Gmail_Login.java” class:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Gmail_Login { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); String appUrl = "https://accounts.google.com"; driver.get(appUrl); driver.manage().window().maximize(); String expectedTitle = " Sign in - Google Accounts "; String actualTitle = driver.getTitle(); if (expectedTitle.equals(actualTitle)) { System.out.println("Verification Successful - The correct title is displayed on the web page."); } else { System.out.println("Verification Failed - An incorrect title is displayed on the web page."); } WebElement username = driver.findElement(By.id("Email")); username.clear(); username.sendKeys("TestSelenium"); WebElement password = driver.findElement(By.id("Passwd")); password.clear(); password.sendKeys("password123"); WebElement signInButton = driver.findElement(By.id("signIn")); signInButton.click(); driver.close(); System.out.println("Test script executed successfully."); System.exit(0); } }
The code above fulfills the aforementioned scenario.
Understanding Code
Import Statements:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By;
We need to import the following packages before we start scripting:
import org.openqa.selenium.WebDriver – This is the importation of WebDriver interface, it is essential for initiating a new web browser instance.
import org.openqa.selenium.firefox.FirefoxDriver – This involves importing the FirefoxDriver class, which facilitates the creation of a Firefox-specialized driver via WebDriver interface.
import org.openqa.selenium.WebElement – Importing the WebElement class is imperative for instantiating web elements.
import org.openqa.selenium.By – Involved in importing the By class, which is necessary for invoking different locator types.
For larger projects, importing extra packages for advanced functionalities might be necessary. This can involve Excel manipulation, logging, assertions, and database connectivity.
Creating an Instance
WebDriver driver = new FirefoxDriver();
In this case, “driver” is a reference variable created for the WebDriver interface and instantiated using the FirefoxDriver class. This begins the Firefox profile default without any extensions or plugins and operates in safe mode.
Launching the Web Browser
driver.get(appUrl);
We invoke the get() method on the WebDriver instance to kickstart a new web browser instance. The string parameter passed into the get() method navigates the web browser to the app URL mentioned.
Maximizing the Browser Window
driver.manage().window().maximize();
After launching the web page, the maximize() method gets employed to maximize the browser window.
Retrieving the Page Title
driver.getTitle();
The getTitle() method fetches the title of the current webpage and saves it in a string variable.
Comparing Expected and Actual Values:
if (expectedTitle.equals(actualTitle)) { System.out.println("Verification Successful - The correct title is displayed on the web page."); } else { System.out.println("Verification Failed - An incorrect title is displayed on the web page."); }
Here, a conditional statement is used to compare the expected and actual title values of the page. A message gets printed based on the result.
Instantiation of WebElement
WebElement username = driver.findElement(By.id(“Email”));
In this case, we are instantiating the WebElement reference with the support of driver.findElement(By.id(“Email”)). This allows us to reference the Email text box on the User Interface whenever an action is to be performed on it.
Clear Command
username.clear();
The clear() command removes the value, including any default placeholder text in the text box.
sendKeys Command
username.sendKeys(“TestSelenium”);
The sendKeys() command is used to input a certain value into the text box. Note that sendKeys() is invoked on the WebElement object instantiated with the property that corresponds to the UI element.
The above code feeds the string “TestSelenium” into the Email text box within the Gmail application.
sendKeys() is among the widely utilized commands in WebDriver scripts.
Click Command
signInButton.click();
click() command, similar to sendKeys(), is used for interacting with web elements and is utilized to click on a web element.
The code above clicks on the “Sign in” button of the Gmail application.
Notes:
- Unlike sendKeys(), click() methods do not accept parameters.
- Clicking on a web element sometimes loads a new page. Thus, the click() method is designed to wait until the page is fully loaded.
Closing the Web Browser
driver.close();
The close() method shuts down the existing browser window.
Mandatorily Ending the Java Program
System.exit(0);
The exit() method forcefully ends the Java program. Before terminating the program, always ensure all the browser instances are shut down.
Execution of Tests
The test script, or in simple terms the Java program, can be run in the following manner:
#1. Eclipse has an icon in the menu bar which executes the test script. Check the diagram below for reference:
Note that the class selected will be the one executed.
#2. Using the editor, right-click anywhere inside the class, press “Run As,” and select “Java Application”.
#3. You can also execute the test script using the Ctrl + F11 shortcut.
When execution ends, the console will show the message “Test script executed successfully.”
Identifying Web Elements
In WebDriver, one can locate and inspect web elements using similar methods as discussed in the prior Selenium IDE guides. Both Selenium IDE and Firebug can be used to inspect web elements on the GUI. It is recommended to employ Selenium IDE for locating web elements. Once a web element gets successfully identified, its target value can be copied and pasted into the WebDriver code. The strategies for locating and the types of locators remain mostly the same except for the syntax and usage specifics.
In WebDriver, dynamic finders (findElement(By.locatorType(“locator value”))) are used to locate web elements.
Sample Code:
driver.findElement(By.id(“Email”));
Types and Syntax of Locators
Locator Type | Syntax | Description |
---|---|---|
id | driver.findElement(By.id(“ID_of_Element”)) | Identifies an element by the value of its “id” attribute |
className | driver.findElement(By.className(“Class_of_Element”)) | Identifies an element by the value of its “class” attribute |
linkText | driver.findElement(By.linkText(“Text”)) | Identifies a link element by its text |
partialLinkText | driver.findElement(By.partialLinkText(“PartialText”)) | Identifies a link element by its partial text |
name | driver.findElement(By.name(“Name_of_Element”)) | Identifies an element by the value of its “name” attribute |
xpath | driver.findElement(By.xpath(“Xpath”)) | Identifies an element by its xpath |
cssSelector | driver.findElement(By.cssSelector(“CSS Selector”)) | Identifies an element by its CSS selector |
tagName | driver.findElement(By.tagName(“input”)) | Identifies an element by its tag name |
Summing Up
In this guide, we developed an automated script using WebDriver and Java. We also discussed various aspects of a WebDriver script.
Main takeaways from this Selenium WebDriver guide are:
- Before scripting, it is necessary to import multiple packages to create a WebDriver script.
- importopenqa.selenium.By;
- importopenqa.selenium.WebDriver;
- importopenqa.selenium.WebElement;
- importopenqa.selenium.firefox.FirefoxDriver;
- We launch a WebDriver interface using the FirefoxDriver class.
- The get() method launches a web browser instance and steers to a specified URL.
- The maximize() method amplifies the browser window.
- The clear() method erases the value in a text box.
- The sendKeys() command keys in a value into a text box.
- The click() method clicks on a web element.
- Web elements are located dynamically using the findElement(By.locatorType(“locator value”)) syntax.
- There are multiple types of locators such as id, className, name, xpath, cssSelector, linkText, partialLinkText, and tagName.
Our next guide will elaborate on TestNG, a framework facilitating automation testing. The guide will concentrate on the various test annotations offered by the framework.
Coming up in guide #11: Prior to a detailed study of frameworks, we will discuss JUnit, an open-source unit testing tool. JUnit is widely used by programmers due to its easy-to-use nature and minimal testing effort requirements. This guide will shed light on JUnit and its usage in Selenium scripts.
Note for Readers: While the next guide in the Selenium series is being drafted, we encourage you to create and run your WebDriver scripts. Stay tuned for more advanced scripts and concepts in the upcoming Selenium WebDriver guides.
If you come across any barriers while creating or running WebDriver scripts, do let us know in the comments.