First Script in Selenium WebDriver




Scenario:
  • Launch the browser and open “Gmail.com”.
  • Verify the title of the page and print the verification result.
  • Enter the username and Password.
  • Click on the Sign in button.
  • Close the web browser.
Create a new java project named as “Selenium” and java class named as “FirstTrail” under this “Selenium” project.
Step 2: Copy and paste the below code in the “FirstTrail .java” class.
1
    import org.openqa.selenium.By;
2
    import org.openqa.selenium.WebDriver;

3
    import org.openqa.selenium.WebElement;
4
    import org.openqa.selenium.firefox.FirefoxDriver;

5

6
   public class FirstTrail {

10
       public static void main(String[] args) {

11
           
12
  // objects and variables instantiation

13
              WebDriver driver = new FirefoxDriver();
14

15
              
16
// launch the firefox browser and open the application url

17
              driver.get("https://accounts.google.com");
18
              

19
// maximize the browser window
20
              driver.manage().window().maximize();

21
              
22
// declare and initialize the variable to store the expected title of the webpage.

23
              String expectedTitle = " Sign in - Google Accounts ";
24
              

25
// fetch the title of the web page and save it into a string variable
26
              String actualTitle = driver.getTitle();

27
              
28
// compare the expected title of the page with the actual title of the page and print the result

29
              if (expectedTitle.equals(actualTitle))
30
              {

31
     System.out.println("Verification Successful - The correct title is displayed.");
32
              }

33
             else
34
              {

35
     System.out.println("Verification Failed - An incorrect title is displayed.");
36
              }

37

38
// enter a valid username in the email textbox

39
              WebElement username = driver.findElement(By.id("Email"));
40
              username.clear();

41
              username.sendKeys("seleniumsender@gmail.com");
42
              

43
// enter a valid password in the password textbox
44
              WebElement password = driver.findElement(By.id("Passwd"));

45
              password.clear();
46
              password.sendKeys("password123");

47
              
48
// click on the Sign in button

49
              WebElement SignInButton = driver.findElement(By.id("signIn"));
50
              SignInButton.click();

51
              
52
// close the web browser

53
              driver.close();
54
              System.out.println("Test script executed successfully.");

55
              
56
// terminate the program

57
              System.exit(0);
58
       }

59
}
The above code is equivalent to the textual scenario presented earlier.
Code Walkthrough
Import Statements:
1
import org.openqa.selenium.WebDriver;
2
import org.openqa.selenium.firefox.FirefoxDriver;

3
import org.openqa.selenium.WebElement;
4
import org.openqa.selenium.By;

Prior to the actual scripting, we need to import the above packages:

import org.openqa.selenium.WebDriver – References the WebDriver interface which is required to instantiate a new web browser.

import org.openqa.selenium.firefox.FirefoxDriver – References the FirefoxDriver class that is required instantiate a Firefox specific driver on the browser instance instantiated using WebDriver interface.

import org.openqa.selenium.WebElement – References to the WebElement class which is required to instantiate a new web element.

import org.openqa.selenium.By – References to the By class on which a locator type is called.
As and when our project would grow, it is evident and logical that we might have to introduce several other packages for more complex and distinct functionalities like excel manipulations, database connectivity, logging, assertions etc.
Object Instantiation
WebDriver driver = new FirefoxDriver();
We create a reference variable for WebDriver interface and instantiate it using FirefoxDriver class. A default Firefox profile will be launched which means that no extensions and plugins would be loaded with the Firefox instance and that it runs in the safe mode.
Launching the Web browser
driver.get(appUrl);
get() method is called on the WebDriver instance to launch a fresh web browser instance. The string character sequence passed as a parameter into the get() method redirects the launched web browser instance to the application URL.

Maximize Browser Window
driver.manage().window().maximize();
The maximize() method is used to maximize the browser window soon after it is re-directed to the application URL.

Fetch the page Title
driver.getTitle();
The getTitle() method is used to fetch the title of the current web page. Thus, the fetched title can be loaded to a string variable.

Comparison between Expected and Actual Values:
1
if (expectedTitle.equals(actualTitle))
2
              {

3
              System.out.println("Verification Successful - The correct title is displayed.");
4
              }

5
              else
6
              {

7
            System.out.println("Verification Failed - An incorrect title is displayed.");
8
              }
The above code uses the conditional statement java constructs to compare the actual value and the expected value. Based on the result obtained, the print statement would be executed.
WebElement Instantiation
WebElement username = driver.findElement(By.id(“Email”));
In the above statement, we instantiate the WebElement reference with the help of “driver.findElement(By.id(“Email”))”. Thus, username can be used to reference the Email textbox on the user interface every time we want to perform some action on it.

Clear Command
username.clear();
The clear() method/command is used to clear the value present in the textbox if any. It also clears the default placeholder value.
sendKeys Command
username.sendKeys(“TestSelenium “);
The sendKeys() method/command is used to enter/type the specified value (within the parentheses ) in the textbox. Notice that thesendKeys() method is called on the WebElement object which was instantiated with the help of element property corresponding to the UI element.
The above block of code enters the string “TestSelenium” inside the Email textbox on the Gmail application.
sendKeys is one of the most popularly used commands across the WebDriver scripts.

Click Command
SignInButton.click();
Like sendKeys(), click() is another excessively used command to interact with the web elements. Click() command/method is used to click on the web element present on the web page.
The above block of code clicks on the “Sign in” button present on the Gmail application.
Close the Web Browser
driver.close();
The close() is used to close the current browser window.
Terminate the Java Program
System.exit(0);
The Exit() method terminates the Java program forcefully. Thus, remember to close all the browser instances prior terminating the Java Program.

Comments