First Test Case with Selenium WebDriver

In case if you are moving with Selenium version 3.0 or above, please skip this tutorial and move forward with next chapter. And if the Selenium Version is less than 3.0, this article would work for you.

In this chapter we will write a very basic automation script of Selenium and try to understand the meaning of import statements, comments in java, print statements and how to instantiate a browser using WebDriver object.

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 every thing is set up correctly.

Scenario to Automate

Launch the Firefox browser

Open website “www.Store.DemoQA.com”

Print a Message to display that the website is opened successfully

Wait for 5 Seconds

Close the Browser

So far we have not done any learning, I would suggest you to 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.store.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. Red colored Underlined word represent the errorin the Eclipse IDE. Eclipse is a powerful tool and it gives us the 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 it self 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 to refer to classes which are declared in other packages to be accessed without referring to the full package name. It means that rather declaring statement like:

org.openqa.selenium.WebDriver driver = new FirefoxDriver(); you can simple import package at the top of the class and write the statement likeimport org.openqa.selenium.WebDriver; public class TestCase {public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); } }

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 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.

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.

Run the Java Test case

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) After a few Seconds a Firefox Browser will open and you will see that with the help of your script, Selenium will Launchthe Online Store, display the Successful Message and Close the browser.

Once the execution is finished, you will see the message in Consolesection of the Eclipse IDE displaying:

Successfully opened the website www.Store.Demoqa.com

Explanation of the Code

Import Packages/Statements

In java we use import statements to use the classes present in another packages. In a way 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 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

This is the way to instantiate a driver object in Selenium. We will not get in to 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

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.Store.Demoqa.com”);

Shortcut for print statement is, type ‘sysout’ and then press ‘CTRL + SPACE’ button. This will bring the complete print statement automatically. Define message with in the parenthesis.

Sleep Statement

This is again more oftenly 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 much smarter option available in Selenium which is Smart Waits and that will be introduced later in this tutorial.

Comments

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

//Comments

What ever 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.

Here we are finished with the initial set up of Selenium and basic understanding of Eclipse. In the next series of WebDriver Commands we will cover the following commands: