How to create your First Selenium Automation Test Script

Last updated on Nov 29 2021
Swaminathan M

Table of Contents

How to create your First Selenium Automation Test Script

In this blog , you’ll find out how to make your First Selenium Automation Test Script.
Under this test, we’ll automate the subsequent scenarios:

• Invoke Google Chrome browser.
• Open URL: www.google.com
• Click on the Google Search text box.
• Type the worth “tecklearn tutorials”
• Click on the Search button.

We will create our test suit step by step to offer you an entire understanding of every component intimately .

Step1. Launch Eclipse IDE and open project “Demo_Test” which we’ve created within the previous section (Configure Selenium WebDriver) of this Tutorial. we’ll write our first Selenium test script within the “First.class” file under the “Demo_Test” test suite.

Automation 1
First.class

Note: To invoke a browser in Selenium, we’ve to download an executable file specific thereto browser. for instance , Chrome browser implements the WebDriver protocol using an executable called ChromeDriver.exe. These executable files start a server on your system which successively is liable for running your test scripts in Selenium.

Step2. Open URL: https://sites.google.com/a/chromium.org/chromedriver/downloads in your browser.
Step3. Click on the “ChromeDriver 2.41” link. it’ll redirect you to the directory of ChromeDriver executables files. Download as per the OS you’re currently on.

Automation 2
ChromeDriver

For windows, click on the “chromedriver_win32.zip” download.

Automation 3
For windows

The downloaded file would be in zipped format. Unpack the contents during a convenient directory.

Automation 4
Unpack contents

Note: Selenium developers have defined properties for every browser that needs the situation of the respective executable files to be parsed so as to invoke a browser. for instance , the property defined for Chrome browser – webdriver.chrome.driver, needs the trail of its executable file – D:\ChromeDriver\chromedriver.exe so as to launch chrome browser.

Automation 5
executable file

Step4. we might need a singular identification for the online elements like Google Search text box and Search button so as to automate them through our test script. These unique identifications are configured along side some Commands/Syntax to make Locators. Locators help us to locate and identify a specific web element in context of an internet application.
The method for locating a singular identification element involves inspection of HTML codes.

• Open URL: https://www.google.com in your Chrome browser.
• Right click on the Google search text box and choose Inspect Element.

Automation 6
Inspect Element

• It will launch a window containing all the precise codes involved within the development of the test box.

Automation 7
launch

• Pick the worth of id element i.e. “lst-ib”.

Automation 8
lst-ib

• Given below is that the Java syntax for locating elements through “id” in Selenium WebDriver.
1. driver.findElement(By.id ())
• Here is that the complete code for locating Google Search text box up our test script.
1. driver.findElement(By.id (“lst-ib”))
• Now, right click on the Google Search button and choose Inspect Element.

Automation 9
Inspect

• It will launch a window containing all the precise codes involved within the development of the Google Search button.

Automation 10
launch window

• Pick the worth of name element i.e. “btnK”.

Automation 11
btnK
• Given below is that the Java syntax for locating elements through "name" in Selenium WebDriver.
1. driver.findElement(By.name ())
• Here is that the complete code for locating Google Search button in our test script.
1. driver.findElement(By.name ("btnK"))

Step5. Now it’s time to code. we’ve embedded comments for every block of code to elucidate the steps clearly.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class First {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "D:\\ChromeDriver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("http://www.google.com/");
// Click on the search text box and send value
driver.findElement(By.id("lst-ib")).sendKeys("tecklearn tutorials");
// Click on the search button
driver.findElement(By.name("btnK")).click();
}
}

Step6. Right click on the Eclipse code and choose Run As > Java Application.

Automation 13
Run

Step7. The output of above test script would be displayed in Google Chrome browser.

Explanation of the Code

Import Packages/Statements

In java, import statements are wont to import the classes present in another packages. In simple words, import keyword is employed to import built-in and user-defined packages into your java source file.

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

2. org.openqa.selenium.chrome.ChromeDriver – References the ChromeDriver class that’s required to instantiate a Chrome-specific driver onto the browser instantiated by the WebDriver class.

Instantiating objects and variables

A driver object is instantiated through:
1. WebDriver driver=new ChromeDriver();
Launch Website
To launch a replacement website, we use navigate().to() method in WebDriver.
1. driver.navigate().to(“http://www.google.com/”);
Click on a component
In WebDriver, user interactions are performed through the utilization of Locators which we might discuss in later sessions of this tutorial. For now, following instance of code is employed to locate and parse values during a specific web element.
1. driver.findElement(By.id(“lst-ib”)).sendKeys(“tecklearn tutorials”);

So, this brings us to the end of blog. This Tecklearn ‘How to create your First Selenium Automation Test Script’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Selenium Certification Training

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium

• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse

Setting the environment in Eclipse for Selenium

• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On

Advantages of Selenium automation testing

• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On

Selenium Web driver Automation

• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On

Deploying Web Drivers for scripting

• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On

Deep dive into Selenium Web Driver

• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On

Switching Operations in WebDriver using Window

• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On

Introduction to TestNG Framework

• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report

JUnit Operations and Test Framework

• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project

Object Repository

• Understanding of Object Repository
• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory

JavaScript Functions

• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "How to create your First Selenium Automation Test Script"

Leave a Message

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