Very first thing to know before we move to this chapter is that this test won’t work, but this is just to give you a basic understanding of How a Selenium Test is written in Eclipse and the meaning of each and every line.

In this chapter, we will write a very basic Selenium Test for Selenium Automation Testing and try to understand the following:

Import statements

Comments in Java

Print Statements

How to use Instantiate a browser using WebDriver?

Prerequisites

There are few prerequisites, which needs to be done to run a Selenium Test in Eclipse IDE:

Scenario to Automate

How to write First Selenium Test Automation Script?

Now you are all set to write your First Selenium Automation Test Script. I have set up a Dummy WebSite to practice automation. Let’s just automate a very simple scenario and see if everything is set up correctly.

So far we have not done any learning, I would suggest you copy the below code in the class which was created in the previous chapter and run the test.

First Test Case

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

packageautomationFramework;

publicclassFirstTestCase{

publicstaticvoidmain(String[]args){

// Create a new instance of the Firefox driver

WebDriver driver=newFirefoxDriver();

//Launch the Online Store Website

driver.get("http://www.shop.demoqa.com");

// Print a Log In message to the screen

System.out.println("Successfully opened the website www.Store.Demoqa.com");

//Wait for 5 Sec

Thread.sleep(5000);

// Close the driver

driver.quit();

}

}

The Eclipse code window will look like this:

How to resolve the Java Compilation Error?

Notice that there are two errors in the code at the statement ‘WebDriver driver = new FirefoxDriver();‘, as it is underlined with the red color line. The red colored Underlined word represents the errorin the Eclipse IDE.

An eclipse is a powerful tool and it gives us very wonderful features and one of the best feature is the ‘Solution Suggestion’. To use this feature, just mouse hover on the error keyword ‘WebDriver’ and Eclipse itself will try to give you the possible solutions to fix the error.

Note: This suggestion will work only when the WebDriver jars are added/imported to the project if not then Eclipse would not be able to suggest the correct solution.

Select the very first solution provided by the Eclipse IDE. You will notice that after selecting the first solution, it will add one import statement at the top saying ‘import org.openqa.selenium.WebDriver;’

What is import Statement?

The import statement in Javaallows referring to classes which are declared in other packages to be accessed without referring to the full package name. It means that rather declaring a statement like:

org.openqa.selenium.WebDriver driver = new FirefoxDriver();

you can simple import package at the top of the class and write the statement like

The same way try resolving the second error of the same statement which is at ‘FirefoxDriver()’ keyword. Just do the same thing, bring the cursor over the error keyword and again Eclipse will bring up a few possible solutions and select the very first solution.

This will add the statement ‘import org.openqa.selenium.firefox.FirefoxDriver;‘ and the error will be gone.

Once you resolve these error one more error will pop up at the statement ‘Thread.sleep(5);‘, now we know what to do right? Just mouse-hover the error and choose the best solution. This time it will ask to select the ‘Add throws declaration‘ and after selecting that it will add ‘throws InterruptedException‘ at the method declaration.

Note: For now, just do not focus on what it is and what does it do, just ignore this part and we will take this later in the tutorial.

How to Run the Selenium Test?

By now all the errors are resolved and we are good to go with the execution of our first automated test cases.

1) Now, to start the test just select Run > Run As > Java Application Or Right Click on Eclipse code and ClickRun As > Java Application.

2) In case if you are using Selenium v2.***, this test will work fine and After a few Seconds, a Firefox Browser will open. You will see that with the help of your script, Selenium will Launchthe Online Store, display the Successful Message and Close the browser.

Explanation of the Code

Import Packages/Statements – Selenium Test

In Java, we use import statements to use the classes present in other packages. In a way, the import keyword is used to import built-in and user-defined packages into your java source file. So that your class can refer to a class that is in another package by directly using its name. To get started, you need to import the following two packages:

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

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.

If your test needs more complicated actions such as accessing another class, taking browser screenshots, or manipulating external files, definitely you will need to import more packages and that we are going to learn in following chapters.

Instantiating an Objects– Selenium Test

This is the way to instantiate a driver object in Selenium. We will not get into the complexity of the code in the first chapter. We have a detailed chapter on this in the later tutorial.

WebDriver driver = new FirefoxDriver();

So for the sake of simplicity just remember that this code will instantiate a new Firefox driver in safe mode which will have no plugins and extensions loaded. This instant of Firefox browser will be different from the Firefox which is installed on your desktop.

Print Statement– Selenium Test

Make a note of this statement, as you are going to use this statement in the selenium code again and again. This will display the message in the Console Window.

System.out.println(“Successfully, opened the website www.Shop.Demoqa.com”);

Sleep Statement– Selenium Test

This is again more often used statement. As selenium is very fast and sometimes it is difficult to see the operations done or the action performed by selenium. One can use Thread.sleep() statement to delay the execution. Here 5 is the number of seconds.

Thread.sleep(5);

Although this method is not suggested and we have a much smarter option available in Selenium which is Smart Waits and that will be introduced later in this tutorial.

Comments– Selenium Test

In java, the comments are marked by double slashes ‘//’.

//Comments

Whatever statement comes just after the double slashed, Java execution engine will ignore that and move to the following statement mentioned just below the commented statement.

Now you are good to start with the next tutorial, put your seat belts on because now you will actually see something running on your system, which is nothing but the first selenium automated test.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT

Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.