How to test broken links with WebdriverIO

Vinay Sharma
2 min readAug 7, 2021

--

While we are working with any automation script or writing test scripts, we often come across a scenario where we have to test links if they are working fine or broken. Also, Sometimes the list of the links are huge. Hence it becomes difficult to test by clicking each of them and verifying if the particular link is working fine or not.

So, In this blog let's dive into how we can test the links and find out if they are broken or working fine with the help of WebdriverIO and TypeScript/JavaScript. It is very simple and easy.

Pre-requisite :

  1. Node installation

Initial Setup :

  1. Create an npm project. Run below command
> npm init
  1. Need to install Typescript if you are using TS. Run below command
> npm i -g typescript 
> npm i — save-dev typescript

We are using the ‘node-fetch’ package from ‘npmjs to test the URLs.

Let's start by installing the node-fetch dependency in your package.json file.

switch to your terminal and go to the directory where you have the package.json file and write the below command

> npm i node-fetch

This will install the required files to the node modules.

Once you have installed the ‘node-fetch’ package. let's get into action by writing a simple reusable function that will take the URL as a parameter and return with the boolean value.

With this, You have your common and reusable function ready and now you just have to call this function wherever you need to test the URL.

Now, Let's create a test case with webdriverIO and call the above function.

In the above test case, we are sending the URL as a parameter to our function and it returns the boolean value true if the link is broken and false if it is working fine.

That's It, You are all set.

Now, Just run your test case based on your configuration in package.json and you will get the desired output.

I hope this blog has helped you and made testing a bit easier.

Thank You! Feel free to reach out to me for any queries.

--

--