Friday, July 13, 2018

First test case using Selenium WebDriver

After setup Gradle project and web driver configuration, it's ready to write the first test case using Selenium WebDriver.

In this article, we are going to test a percentage calculator on the site Percentage Calculator. Here is the page looks like.


There are three text boxes on the page, if you type 10 and 100 in two blue text boxes, you will get 10 in the third text box, which means 10% of 100 equals 10.

Now we are creating the test case using JUnit in Eclipse.

1) Create a package
     Right click on "src/test/java" source folder, and choose New > Package.

    Type a package name in "Name" field and click the "Finish" button.

2) Create a Test Case
    Right click on "me.simplejavautomation" package, and choose New > JUnit Test Case.

    Type a class name in "Name" field and click the "Finish" button. And make sure to check the "setUp()" and "tearDown()" check-boxes.

3) Write Test Case
     Type the code below and will let you familiar with the selenium classes and interfaces.

4) Run the Test Case
    Right-click the Test Class or test method and choose Run As > JUnit Test. A new browser window will be opened and you will see,
    a) It navigates to the website http://www.percentagecalculator.co/
    b) The cursor moves on first text-box and types 10
    c) The cursor moves on second text-box and types 100
    d) The third text-box value changes to 10
    e) The browser is closed
    Then, you will get the green bar in the JUnit tab which shows you the test case passed.

5) How to identify the element on the page?
    Before we can find an element on the page using Web Driver, we need to identify the id, name, CSS or XPath on the element. Follow the steps below to find the right element id.
    a) Open the website using Google Chrome
    b) Press F12 or Ctrl+Shift+I to open Developer Tools
    c) Click the arrow button on the top left of Developer Tools window or Ctrl+Shif+C
    d) Point your mouse to the element you want on the page
    You may find the id, name, CSS on the web element and Properties tab on the right side. In this example, the first textbox's id is A. Then you can find ids for other two textboxes.

6) Run Test Case on other browsers
    To run the test case on different browsers, just instantiate the related Web Driver object in "setUp()" function.

1 comment:

  1. Such nice blog . Thanks for sharing such great information for me. I hope you will share some more information about Please keep sharing!
    selenium classes in pune hadapsar

    ReplyDelete