First Selenium WebDriver Script | Selenium Tutorial
In this Selenium tutorial, we are going to show you how to create a basic Selenium script.
Earlier we have written about architecture of Selenium. I assume you got a clear idea of Selenium Architecture.
I also assume that you have already installed Selenium WebDriver. If not check our detailed post on How To Download And Install Selenium WebDriver.
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.
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:
- Open Firefox browser
- Go to the specified URL
- Verify the title and print the output of the title
- 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.
- Create a Java Project “SoftwareTestingMaterial”
- Create a package “seleniumTutorial”
- 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)
Code Explanation:
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 open URL "https://softwaretestingmaterial.com". This is what we have assigned to the variable named 'url'. driver.get(url); // maximize the browser window driver.manage().window().maximize(); //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 } }
Import Packages:
Before writing the actual steps, we should import the below packages.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver – WebDriver class to instantiate a new browser
import org.openqa.selenium.firefox.FirefoxDriver – FirefoxDriver class to instantiate a Firefox driver
Object Instantiation
WebDriver driver = new FirefoxDriver();
Instantiation of driver object. To launch Firefox browser
Launching the Web browser
driver.get(url);
A get() method is called on the WebDriver instance to launch a web browser. The string passed as a parameter (i.e., String url = “https://softwaretestingmaterial.com”) into the method get() redirects the launched web browser instance to the application URL.
Maximize Browser Window
driver.manage().window().maximize();
To maximize the browser window, we use the maximize() method..
Fetch the page Title
driver.getTitle();
To fetch the title of the current webpage, we use the getTitle() method.
Close the Web Browser
driver.close();
To close the current browser window, we use close() method.
Conclusion
In this tutorial, we learnt how to write our first Selenium script along with detailed explanation.
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.
Also don’t miss to know the difference between Cypress vs Selenium and also the alternatives to Selenium
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
Hi Rajkumar,
eclipse not showing import option for WebDriver for WebDriver driver = new FirefoxDriver();
could you please help me with this?
Hi Shruthi, it might be because of jars.. Check out whether you have added required Selenium Jars in your project or not. Check this post.
Hi Raj,
somehow eclipse not showing any suggestion. Yesterday it was working fine and all of sudden eclipse not showing any suggestion. I tried to restoring the default options in ‘Windows > Preferences > Java > Editor > Content Assist > Advanced’. and language(clr+space) but it didn’t work for me. I tried all other option which suggested on Google. still it is not working for me. can you please help me out.
Hi Shruthi, you said you have tried everything. So i suggest you to try Project – clean.. i think its under window in eclipsw.. also try restart your system.. if you find solution, just let us know how u resolved it
Hi Raj,
I degrade the version of eclipse from oxygen to neon. Now it is working fine.