top of page

Selenium Webdriver Automation: Create your first program with step-by-step instructions

Updated: May 4, 2023


Selenium is a free, open-source automated testing framework for web applications. It is used by many companies to automate their web application testing. Selenium can be used to automate both functional and non-functional testing.


Selenium webdriver Automation

Every great guide should start with an introductory paragraph. This is your opportunity to persuade your reader and let them know why this post will answer all their important questions. You can share why you are the ideal person to write this guide; write a personal anecdote about how you became an expert in the field; show your empathy with your reader regarding how hard it is to find accurate information; and/or stress the frequent mistakes people make that can be avoided.


Before writing your “how-to” or “best-of” post below, add one last sentence that sums up your paragraph and offers a polished transition to your guide.


What is Selenium?


Selenium is a suite of tools that can be used to automate web application testing. It consists of a number of different components, including:

  • Selenium Webdriver: A library that allows you to interact with web applications using a variety of programming languages.

  • Selenium IDE: A graphical tool that allows you to record and playback web application tests.

  • Selenium Grid: A tool that allows you to run Selenium tests on multiple machines in parallel.

  • Selenium RC: A remote control tool that allows you to control Selenium WebDriver from a variety of programming languages. (Note: Selenium RC was officially deprecated in Selenium version 3.8 and completely discontinued in Selenium version 4.0, which was released on November 2020.)

 

Why use Selenium?

There are many reasons to use Selenium for web application testing. Some of the benefits of using Selenium include:

  • It is free and open-source.

  • It is cross-platform.

  • It supports a variety of programming languages.

  • It is easy to learn and use.

  • It is a powerful tool that can be used to automate a wide range of web application tests.

 

How to get started with Selenium Webdriver?


Step 1: Download and install the Java Runtime Environment (JRE)


The Java Runtime Environment (JRE) is a software environment that provides the libraries and other resources that Java programs need to run. You can download the JRE from the Oracle website.


Step 2: Download and install the Eclipse IDE for Java Developers


The Eclipse IDE for Java Developers is a free and open-source integrated development environment (IDE) that is used for developing Java applications. You can download the Eclipse IDE from the Eclipse website.


Step 3: Download the Selenium WebDriver for Java


The Selenium WebDriver for Java is a library that allows you to interact with web applications using the Java programming language. You can download the Selenium WebDriver for Java from the Selenium website.


Selenium API
Selenium Binding

Step 4 : Steps to create a new Java project in Eclipse IDE

  1. Open Eclipse IDE.

  2. Click on "File" from the menu bar.

  3. Hover over "New" and then click on "Java Project".

  4. In the "New Java Project" window, enter a name for your project in the "Project name" field.

  5. Choose the desired Java runtime from the "Use an execution environment JRE" drop-down list.

  6. Choose the default option "create separate folder for source and Classfile" project layout from the "Project layout" options.

  7. Click on "Finish" to create the project.

  8. The new project will appear in the "Package Explorer" view on the left-hand side of the Eclipse window.

Step 5: Add the Selenium WebDriver JAR files to your Eclipse project

  1. Right-click on your Java project in the "Package Explorer" view.

  2. Click on "Build Path" and select "Configure Build Path".

  3. Click on the "Libraries" tab and then click on "Add Library".

  4. Select "User Library".

  5. Click on "New" and name your library, for example, "Selenium WebDriver".

  6. Select the newly created library and click on "Add External JARs".

  7. Navigate to the location where you have stored your extracted Selenium WebDriver JAR files, including the one in the Libs folder, and select all the JAR files.

  8. Add the selected JAR files to the new user library.

  9. Once all the files are added to the user library, click "OK" to close the dialog box.

  10. Select the newly created selenium library from the user library window and click Finish.

  11. Ensure it is Selenium Library list in the libraries tab in Java build setting.

  12. click Apply and Close.

Step 6: Create a new Java class and write your first Selenium test

  1. To do this, right-click on your project in the Eclipse Package Explorer and select "New" > "Class".

  2. In the "Name" field, type in the name of your class.

  3. In the "Package" field, type in the package name for your class.

  4. Click on the "Finish" button to create your class.


Use the following code and check if is working.


Note :


The following code will open the Google homepage in a Chrome browser:


public class MyFirstSeleniumTest {


public static void main(String[] args) {


/* Create a new Chromedriver object directly since from selenium version *4.6.0 includes Selenium manager which will take care of the browser *driver

*/


WebDriver driver = new ChromeDriver();


// Navigate to the Google homepage.

driver.get("https://www.google.com");


// Verify that the title of the page is "Google".

String title = driver.getTitle();


System.out.println("The title of the page is: " + title);


// Close the browser.

driver.quit();

}

}

You can use the Selenium WebDriver classes to perform a variety of actions on web pages, such as clicking on links, entering text into fields, and submitting forms.


For more information on how to use the Selenium WebDriver classes, you can refer to the Selenium documentation.


Once you have written your Selenium test, you can run it by right-clicking on the class in the Eclipse Package Explorer and selecting "Run As" > "Java Application".


If your test is successful, it will print a message to the console. If your test fails, it will print an error message to the console.


I hope this helps! Let me know if you have any other questions.


We'd love to hear your thoughts on this topic. Leave a comment below and let us know what you think!


108 views1 comment

1 Comment

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Guest
May 05, 2023
Rated 5 out of 5 stars.
Like
bottom of page