First Program in Selenium WebDriver to test www.google.com

Once we have configured Selenium jar files in to Eclipse (refer post: Downloading Selenium WebDriver Jars and configuring in eclipse), We are now good to start with Basic programs using Selenium.
We write the java program for selenium test in a java class. So, let us create one java class.
Right click on Package -> New -> Class (If you don’t find Class over there, then go to Other, and search for Java class there)
Give some meaningful name to the class and click on Finish.
The first basic thing is, we need to create driver of the web browser which will drive the flow.
Here I am using Mozilla Firefox driver.
Below is the sample code for my first selenium test where I am navigating to www.google.com and finding out the Title of the page.
Once the code is written in a class file, right click on it, select Run AS – > Java Application
Sample code – 

package basicsOfSelenium;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstSeleniumTest {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

driver.navigate().to("https://www.google.co.in");

String TitleOfPage = driver.getTitle();

System.out.println("Title of Current page is : "+TitleOfPage);

driver.close();
}

}

Sample Output:

Title of Current page is : Google
Good luck for your beginning

Do post your comments for any questions / suggestions.

2 Comments

  1. samantha

    Reply

    Hi sir ,

    Iam trying to execute above prgm it is displaying ,following error;

    “Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;”

    • admin_prasee

      Reply

      Have you set geckodriver properties?

      System.setProperty(“webdriver.gecko.driver”,”path to geckodriver.exe on your local machine”);

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories