First Selenium WebDriver Script | Selenium Tutorial

First Selenium WebDriver Script | Selenium Tutorial

Creating First Selenium WebDriver Script:

Here I don’t want to go in depth to show the first Selenium WebDriver script. I would like to keep it very simple.

Hope you have already installed Selenium WebDriver. If not go through the below mentioned link to download and install Selenium WebDriver.

How To Download And Install Selenium WebDriver

In the first Selenium WebDriver Script, let’s see the below mentioned sceanario using Selenium WebDriver.

Scenario:
To open appropriate URL and verify the title of the home page

Steps:
i. Open Firefox browser
ii. Go to the specified URL
iii. Verify the title and print the output of the title
iv. Close the Firefox browser

Test Data:
URL:
 https://www.softwaretestingmaterial.com
Expected Value: Software Testing Material – A site for Software Testers

To create our first Selenium WebDriver Script, we have to first create a Java Project, Package and Class in Eclipse.

i. Create a Java Project “SoftwareTestingMaterial”
ii. Create a package “seleniumTutorial”
iii. Create a Java Class “FirstSeleniumWebDriverScript”

Test script with an explanation:

(Note: We use // text when we want to comment a single line of code. and use /* text */ when we want to comment multiple lines of code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package seleniumTutorial;
 
//Importing packages
//We need to import relevant packages depends on our needs.
import org.openqa.selenium.WebDriver;
//It contains the WebDriver class to instantiate a new browser
import org.openqa.selenium.firefox.FirefoxDriver;
//It contains the FirefoxDriver class to instantiate a Firefox driver
 
public class FirstSeleniumWebDriverScript {
 
public static void main(String[] args) {
  System.setProperty(“webdriver.gecko.driver”,“D://Selenium Training//Selenium Environment Files//geckodriver.exe”);
//Instantiation of driver object. To launch Firefox browser
WebDriver driver = new FirefoxDriver();
//Declaration of variables
String url = “http://softwaretestingmaterial.com”;
String expectedTitle = “Software Testing Material – A site for Software Testers”;
String actualTitle = null;
//To oepn URL “http://softwaretestingmaterial.com”. This is what we have assigned to the variable named ‘url’.
driver.get(url);
//To get the actual value of the title. getTitle method used to get the page title
actualTitle = driver.getTitle();
//Using if-else condition to compare the Expected Title and Actual Title. As per the below lines of code (if-else condition).
if (actualTitle.contentEquals(expectedTitle)){
//’system.out.println’ prints the output
System.out.println(“Expected Value is “+expectedTitle);
System.out.println(“Actual Value is “+actualTitle);
System.out.println(“Test Passed”);
} else {
System.out.println(“Expected Value is “+expectedTitle);
System.out.println(“Actual Value is “+actualTitle);
System.out.println(“Test Failed”);
}
//’close’ method is used to close the browser window
driver.close();
//To run the script – Go to menu bar – click on Run – Run or use shortcut key Ctrl+F11
//You could see the output in the console as shown below:
//Expected Value is Software Testing Material – A site for Software Testers
//Actual Value is Software Testing Material – A site for Software Testers
//Test Passed
}
}

Check out the below link for the complete list of Selenium WebDriver tutorials. There is a propaganda in the industry as learning and working on Selenium is very difficult. To be frank, learning Selenium is very easy.

Selenium WebDriver Tutorial

If you are not regular reader of my blog then I highly recommend you to sign up for the free email newsletter using the below link.

Subscribe and get a free eBook and regular updates from SoftwareTestingMaterial.com