• Selenium Video Tutorials

Selenium WebDriver - First Test Script



Before starting with our first test script on Selenium, we should ensure that Java, and Maven have been installed in our system.

Prerequisites Steps To Create the First Test Script

Step 1 − Confirm Java installation by running the command: java, from the command prompt.

Step 2 − Confirm the version of the Java installed by running the command:

java version

Step 3 − Confirm the version of the Maven installed by running the command −

mvn version

Step 4 − Download and install any code editor, say the Eclipse from the link Download Eclipse Technology.

To get more information about the Eclipse and its installation process, please visit the link Eclipse Tutorial.

Steps To Create the First Test Script

Step 1 − Initiate a driver session and perform some activity on the browser, say launching an web page on the Chrome browser with the help of the below code −

Webdriver driver = new ChromeDriver();

driver.get("https://www.tutorialspoint.com/selenium/practice/text-box.php");

Step 2 − Retrieve some information about the current URL that is launched on the Chrome browser with the help of the getCurrentUrl() method.

driver.getCurrentUrl();

Step 3 − Add some synchronization techniques so that the interactions between the Selenium tool and browser actions would be in sync. There are several synchronization methods available in Selenium, like the implicit, explicit, and fluent waits.

While implicit wait is a kind of global wait (applicable to all the steps in a text case), explicit and fluent waits are applied to a specific element on the browser. For our first test script here, we would only apply an implicit wait.

Syntax to add an implicit wait of 15 seconds −

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

To get more information about the different waits in Selenium, please visit the link Selenium Webdriver Wait Support.

Step 4 − Perform any task on a webelement in the same webdriver session, for that purpose, identify the element uniquely with the help of any of the Selenium Locators like id, name, class, tagname, link text, partial link text, xpath, and css selector along with findElement() method. If there are multiple elements with the same value of locator, only the first matching element should be identified.

Syntax to identify an element with xpath locator −

WebElement e = driver.findElement(By.xpath("<value of xpath>"));

Step 5 − Perform a task on the element identified on the previous step using the sendKeys() method. Enter the text - Selenium and text to be entered is passed as an argument to the method.

Syntax

e.sendKeys("Selenium");

Step 6 − Request the information on the value entered on input box using the getAttribute() method and value is passed as an argument to the method.

Syntax

String text = driver.findElement(By.xpath("<value of xpath>")).getAttribute("value");

Step 7 − Wipe out the text entered with the clear() method.

Syntax

e.clear();

Step 8 − Again request the information on the value on the input box using the getAttribute() method(to prove there is no value within the input box after using the clear() method) and value is passed as an argument to the method.

Syntax

String text = driver.findElement(By.xpath("<value of xpath>")).getAttribute("value");

Step 9 − Terminate the driver session using the quit() method which would also close the opened browser window.

Syntax

driver.quit();

Example

Right click on the web page below, and then click on the Inspect button in the Chrome browser. For identifying the first text box on the page, click on the left upward arrow at the top of the visible HTML code as highlighted below.

Selenium First Test Script 1

Once we had clicked and pointed the arrow to the input box(highlighted in the below image), its HTML code appeared, reflecting the input tagname and the type attribute having the value as text.

Selenium First Test Script 2

Let us take an example of the above page, where we would first input text in the text box with the help of the sendKeys() method. Later, we would clear out the text with the help of the clear() method.

Code Implementation

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class FrstJavaTestScript {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify edit box to enter        driver.get("https://www.tutorialspoint.com/selenium/practice/text-box.php");

      // getting current URL
      System.out.println("Getting the Current URL: " + driver.getCurrentUrl());

      // Identify the input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));

      // enter text in input box
      e.sendKeys("First Test Script");

      // Get the value entered
      String text = e.getAttribute("value");
      System.out.println("Entered text is: " + text);

      // clear the text entered
      e.clear();

      // Get no text after clearing text
      String text1 = e.getAttribute("value");
      System.out.println("Get text after clearing: " + text1);

      // Closing browser
      driver.quit();
   }
}

Output

Getting the Current URL: 
https://www.tutorialspoint.com/selenium/practice/text-box.php
Entered text is: First Test Script
Get text after clearing: 

Process finished with exit code 0

In the above example, we had first obtained the current URL with the message in the console: Getting the Current URL: Selenium Automation Practice Form . Then we had entered the text First Test Script in the input box, We had retrieved the value entered in the console with the message - Entered text is: First Test Script. Next, we had cleared the value entered, and hence, obtained no value in the same text box.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Conclusion

This concludes our comprehensive take on the tutorial on Selenium Webdriver First Test Script. Weve started with describing prerequisite steps to create the First Test Script, steps to create the First Test Script, and an example to illustrate how to create the first test script in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver - First Test Script. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements