Write and Execute the Selenium test script

Last updated on Nov 24 2021
Swaminathan M

Table of Contents

Write and Execute the Selenium test script

Write the Selenium test Script

For our testing purpose, we’ll perform a Login test on the Gmail application.
In this test, we’ll automate the below test scenarios:

Steps Actions Input Expected Result
1. Import web driver from Selenium. Web driver should be imported.
2. Open the Google Chrome browser. The Google Chrome browser should be opened.
3. Maximize the browser and delete all the cookies The browser should be maximized, and cookies should be deleted.
4. Navigate to the home page Gmail application. https://www.gmail.com The Gmail home page must be displayed.
5. Identify the username text box and pass the worth xyz11@gmail.com The username text box should be identified and value should be entered in the username text box.
6. Click on the Next button. Subsequent button should be clicked.
7. Identify the password text box and pass the value. ####### The password text box should be identified and value should be entered in the Password text box.
8. Click on the Next button. The next button should be clicked.
9. Close the Browser. The Browser should be closed.

Follow the below steps:
Step1
In the initiative , we’ll import the online driver with the assistance of the subsequent statement:
1. from selenium import webdriver
Step2
After that, we’ll open the Google Chrome browser.
• To launch the Google Chrome or the other browser (Firefox, ie, etc.), we’d like to download the executable files of the Chrome driver from the given link: https://sites.google.com/a/chromium.org/chromedriver/downloads
• Then, paste the downloading exe enter the Python package (demo) to beat the exceptions as we will see within the below screenshot:

Page 2 Image 3
Executable files

The sample code is as below:

1. #open Google Chrome browser
2. driver = webdriver.Chrome()
Step3
In the next step, we maximize and delete all the cookies of the browser window.
Here the sample code:
1. #maximize the window size
2. driver.maximize_window()
3. #delete the cookies
4. driver.delete_all_cookies()
Step4
In this step, we'll navigate to the Gmail application URL.
The sample code is as below:
1. #navigate to the url
2. driver.get("https://www.gmail.com")

Step5
Once we navigate to the URL of the Gmail application, we’ll identify the username text box and spending the worth of it.
To identify the username text box, follow the below process:
• Right-click on the username text box.
• And select the Inspect option within the given pop-up menu as we will see within the below screenshot:

Page 3 Image 5
Inspect

• The developer tool window will open with all the precise codes utilized in the event of the usernametext box.
• Then, copy the worth of its id attribute that is: identifierId as we will see within the below image:

Page 4 Image 7
copy id attribute
• And, here the sample code:
1. #identify the user name text box and enter the worth
2. driver.find_element_by_id("identifierId").send_keys("xyz11@gmail.com")
3. time.sleep(2)

Step6
In this step, we’ll identify subsequent button and click on thereon .
To identify subsequent button, follow the below process:
• Right-click on subsequent button, and click on on the Inspect option within the given pop-up menu as we will see within the below image:

Page 5 Image 9
Identify button

• The developer tool window will open with all the precise codes utilized in the event of subsequent
• And, copy the worth of its absolute XPath that is: //span[@class=’RveJvd snByac’] from the chropath section as we will see within the below image:

Page 6 Image 11
Copy absolute XPath
• Here the sample code:
1. #click on subsequent button
2. driver.find_element_by_xpath("//span[@class='RveJvd snByac'][1]").click()
3. time.sleep(3)

Step7
In this step, we’ll identify the password text box and pass the worth of it.
To identify the password textbox, follow the below process:
• Right-click on the password text box, and click on on the Inspect Option from the given pop-up menu as we will see within the below screenshot:

Page 7 Image 13
Inspect

• The developer tool window will open with all the precise codes utilized in the event of the password text box.
• And, copy the worth of name attribute, i.e., password as we will see within the below image:

Page 8 Image 15
copy name attribute
Here the sample code:
1. #identify the password text box and enter the worth
2. driver.find_element_by_name("password").send_keys("########")
3. time.sleep(3)

Step8
In this step, we’ll identify subsequent button and click on thereon .
To identify subsequent button, follow the below process:
• Right-click on subsequent button, and click on on the Inspect option within the given pop-up menu as we will see within the below image:

Page 9 Image 17
Inspect option

• The developer tool window will open with all the precise codes utilized in the event of subsequent
• Copy the worth of its absolute XPath that is: //span[contains(text(),’Next’)] from the chropath section as we will see within the below image:

Page 10 Image 19
Copy absolute XPath
Here the sample code:
1. #click on subsequent button
2. driver.find_element_by_xpath("//span[contains(text(),'Next')][1]").click()
3. time.sleep(3)
Step9
In the last step of the test script, we'll close the browser.
Here the sample code:
1. #close the browser
2. driver.close()
And, our final test script will appear as if this after writing all the above steps successfully.
1. from Selenium import webdriver
2. import time
3. from Selenium.webdriver.common.keys import Keys
4. print("test case started")
5. #open Google Chrome browser
6. driver = webdriver.Chrome()
7. #maximize the window size
8. driver.maximize_window()
9. #delete the cookies
10. driver.delete_all_cookies()
11. #navigate to the url
12. driver.get("https://www.gmail.com")
13. #identify the user name text box and enter the worth
14. driver.find_element_by_id("identifierId").send_keys("xyz11@gmail.com")
15. time.sleep(2)
16. #click on subsequent button
17. driver.find_element_by_xpath("//span[@class='RveJvd snByac'][1]").click()
18. time.sleep(3)
19. #identify the password text box and enter the worth
20. driver.find_element_by_name("password").send_keys("#########")
21. time.sleep(3)
22. #click on subsequent button
23. driver.find_element_by_xpath("//span[contains(text(),'Next')][1]").click()
24. time.sleep(3)
25. #close the browser
26. driver.close()
27. print("Gmail login has been successfully completed")
Note: within the above code, use your Gmail id at place: xyz11@gmail.com and password: #########

Run the test script

To run the above test script, we’ll right-click on the code then select Run As → Python Run as we see within the below screenshot:

Page 12 Image 22
Run the test script

The above test script will launch the Google Chrome browser and automate all the test scenarios.

Page 13 Image 24
Google Chrome browser

And, as we will see within the below screenshot that our code is running successfully as we get the print message (output) on the console screen.

Page 13 Image 25

So, this brings us to the end of blog. This Tecklearn ‘Write and Execute the Selenium 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:

https://www.tecklearn.com/course/selenium-training-certification/

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 "Write and Execute the Selenium test script"

Leave a Message

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