Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to start test automation using Selenium and JavaScript

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to start test automation using Selenium and JavaScript". In daily operation, I believe many people have doubts about how to start test automation using Selenium and JavaScript. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to start test automation using Selenium and JavaScript". Next, please follow the editor to study!

Testing is an important phase of the software development life cycle (SDLC), in which the main agenda is to identify the largest bug and mine the missing requirements needed to successfully release the product. In order to increase product test coverage and deliver error-free products in the fastest way, it is important to introduce functional automation testing into the picture to ensure that the product works as expected and provides a perfect experience for end users.

Today, we have many automated testing tools and frameworks to automate Web operations. However, Selenium is one of the most popular and feasible Web automation frameworks.

Selenium is an open source testing framework used to automate the end-to-end testing process through the front end of the application. It is further classified as:

Selenium IDE: the Selenium integrated development environment (IDE) is primarily a recording and playback tool that can be used to automate Web operations. It is easy to use and can be configured simply by adding an extension to the browser. It is a GUI-based application that records interactions with websites and creates test suites in IDE.

Selenium WebDriver: is a remote control interface that provides control over user agents. Selenium WebDriver is a browser automation framework that accepts commands from language bindings and forwards them to browsers. It controls the browser by communicating directly with the browser and is implemented through browser-specific drivers.

Selenium Grid: a trending tool for running multiple automated tests in parallel against different environments created by different browsers, browser versions, and different operating systems. Selenium Grid is basically a combination of hubs and nodes, where hubs are Selenium independent hubs and nodes are different browsers connected to a single hub.

Selenium supports a variety of scripting languages, such as Java, Ruby, C #, Python, JavaScript, etc. JavaScript is an advanced, multi-paradigm programming language for both client and server, allowing you to convert static web pages to interactive web pages. We will examine how to use Selenium Webdriver with Javascript for automated testing of Web applications.

Why choose JavaScript to automate UI based on Selenium?

JavaScript is primarily used to develop Web applications, because many Web applications are based on either the MEAN stack (MongoDB, Express.js, AngularJS, and Node.js) or the MERN stack (MongoDB, ExpressJS, ReactJS, Node.js). According to Stack Overflow, JavaScript is the most widely used programming language in the world.

Because it is popular with many Web development audiences, integrating it with Selenium to enhance test automation may be a major idea. The following highlights why you chose JavaScript and Selenium:

JavaScript is a well-developed and structured schema language that makes automated scripts more compact and easy to understand.

Selenium-based UI automation and Javascript can be a good combination for many organizations because they can stick to one technology for Web development and test automation.

These technologies are open source and free to use, and are gradually being enhanced for better development.

It is wrapped in advanced security and is relatively safe to use.

JavaScript is a scripting language that provides a huge advantage because it has direct control over server or client script.

Top four JavaScript testing frameworks 1. JEST

JEST is an automated testing framework developed by Facebook to test the JavaScript code of applications developed using React JS. It is considered to be a well-documented and fast-performing JavaScript testing framework. The main advantage of using JEST is that you can write test cases using a behavior-driven development approach, and the framework can also be used to perform visual regression tests by taking screenshots.

2. Nightwatch

Nightwatch is an automated testing framework driven by node.js. It facilitates end-to-end testing of Web applications by leveraging W3C Selenium WebDriver API. It can be used for unit testing and integration testing. It has a built-in command line test runner to execute tests through retries and implicit waits. The syntax of the Nightwatch framework is very clean and easy to understand, helping testers to write more effective test cases.

3.Protractor

As we studied above, JEST is mainly used for React JS-based applications, similarly, for Angular JS-based Web applications, we have Protractor. It is usually used for end-to-end testing of Angular applications and supports other locators similar to Selenium locators, such as repeater, model, binding, and so on. Setting up parallel execution of test cases for different environments is very simple. In addition, using Protractor makes it easier to take and compare screenshots automatically.

4. Mocha

Mocha JS has been a popular automated testing framework since 2011. It is an open source testing framework that is mainly used by JavaScript developers for unit testing. It runs on Node.js and provides compatibility for front-end and back-end testing. It is widely used in third-party assertions, ridicule and espionage. It supports the node.js debugger to make error identification easier. The Mocha JS framework is also known for its accurate reporting and easy mapping of exceptions to related test cases.

Prerequisites for setting Selenium using JavaScript

To continue to use Selenium and JavaScript for test automation, there are few prerequisites to check. Let's take a look:

Step 1: install Node.js npm

Node.js is bundled with npm, the Node package Manager. You can download the same content from the official Node.js site. Run the following command to verify the installation of npm and node.js:

Npm-v

Node-v

Step 2: install Selenium WebDriver

Npm is the built-in package manager that will be further used to install Selenium for JS.

The following is the command to install Selenium WebDriver (- save is used to create a new package that will be saved to the project package.json file):

Npm install-save selenium-webdriver

You also need to download drivers for Chrome, Firefox, or any other specific browser on which you want to perform automated tests, and you can download the same driver from the npm js site. It is strongly recommended that you add the path of the browser driver to the system PATH so that Selenium can use these executables to launch the browser directly.

Step 3: install IDE

To achieve this, you can install any IDE of your choice for JS from the following:

Visual Studio Code

Eclipse

Write our first Web automated test using Selenium and JavaScript

After completing all the above steps and successfully completing the configuration settings, we can begin to write our first Selenium automated test using JavaScript.

Our test scenario is as follows:

Navigate to the official pCloudy website

Get the title of the home page

Verify the title of the home page verify login to the pcloudy website

Print log based on test case pass or failure

First_test.js

Var webdriver = require ('selenium-webdriver'); const driver = new webdriver.Builder () .forBrowser (' chrome') .build () Driver.get ('http:/www.pCloudy.com') .then (function () {driver.getTitle (). Then (title= > {if (title.localeCompare ("Mobile App Testing, Continuous Testing Cloud, Mobile Testing Tools | pCloudy")) {console.log ("Test Passed!")} else {console.log ("Test Failed!")})); driver.quit (); code demonstration:

Initially, we imported the selenium-webdriver library, and then we created an instance of chromedriver using the Builder method in the library we imported.

Next, we use driver.get () to navigate to the Web application URL. Then we use driver.getTitle () to get the title of the application's home page and verify that the actual title matches the expected title. In addition, the log is printed on the console to show the status of the test case. At the end of the script, we quit the running browser session.

To run the test, execute the following command:

Node first_test.js

Run Selenium automated tests using JavaScript on pCloudy Browser Cloud

PCloudy is a continuous cloud testing platform that provides a large combination of browsers supported by real Windows and Mac machines. The browser platform is called the browser cloud, where all remote machines are hosted on the cloud so that we can leverage and run our automated tests on different environments (that is, different browsers, multiple browser versions, and different operating systems).

Browser Cloud also provides manual testing of Web applications on these cloud-managed machines. If your automated test fails in a particular environment, you can simply select the same environment from pCloudy UI and run the test manually in the same environment on pCloudy.

Now, let's take a quick look at the following JavaScript-based Selenium script, which will be used to perform cross-browser tests on the pCloudy browser cloud:

Const webdriver = require ('selenium-webdriver'); var id =' ramit.dhamija@gmail.com';var accessKey = '5TfF4UcNRbN3JhucQ' Function verifyHomePageTitle () {const capabilities = {pCloudy_Username: id, apiKey: accessKey, clientName: id, email: id, os: 'Mac', osVersion:' Catalina', browserName: 'safari', browserVersions:' 14, pCloudy_EnableVideo: true, pCloudy_EnablePerformanceData: true PCloudy_EnableDeviceLogs: true} const gridUrl = 'https://prod-browsercloud-in.pcloudy.com/seleniumcloud/wd/hub'; Const driver = new webdriver.Builder () .usingServer (gridUrl) .withcapabilities (capabilities) .build () Driver.get ('http:/www.pCloudy.com') .then (function () {driver.getTitle (). Then (title= > {if (title.localeCompare ("Mobile App Testing, Continuous Testing Cloud, Mobile Testing Tools | pCloudy")) {console.log ("Test Passed!")} else {console.log ("Test Failed!")});} verifyHomePageTitle () Code demonstration:

Since we are now running our automated test, pCloudy Browser Cloud, on Selenium Grid, it is important to add the necessary functionality to set up the test environment. As part of the required functionality, we passed our pCloudy credentials (username and api key) and the functionality of the real test machine, such as "Mac" as the operating system, "Catalina" as the operating system version, "safari" as the browser, and "14" as the safari browser version. In addition, we use some of the built-in features of pCloudy to capture test video records, performance data, and logs.

To run our tests on the pCloudy Selenium grid, we defined the selenium grid URL. In addition, we have written a script for the same test scenario as the above script.

PCloudy output:

To see the current running status of the test, you need to go to the pCloudy device page and click report-> my active session:

To view all tests performed so far, go directly to the pCloudy device page and click reports-> all reports:

To view logs and snapshots of a specific test, go directly to the pCloudy device page and click reports-> all reports and perform actions on specific session names as needed, with logs and screenshots:

At this point, the study on "how to start test automation using Selenium and JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report