Use GitHub Copilot and Selenium to write your first Javascript-test

GitHub Copilot, Javascript, Selenium
18/06/2024

One of the more boring things to do as a developer, is writing tests. With Selenium, you can automate your frontend-tests. I am going to show you a real simple example of writing a Javascript-test with Selenium with the help of GitHub Copilot.

This is an AI-example. So your results might differ. Just try to check if it's similar to your results!

Prerequisites

  • NodeJS installed
  • VS Code with GitHub Copilot as an extension
  • A basic knowledge of Javascript and CSS

Installation

  • Go to https://www.npmjs.com/ and search for 'selenium-webdriver'.
  • Click the topresult and install your prefered driver. I'm using Firefox in this example. You can use any other browser, but just make sure you have the same browser installed on your device too.
  • Save the .exe to disk and add that to your path via Environment Variables.
  • Test via CMD 'start geckodriver' if your Geckodriver is installed correctly. It should open a new window.
  • Create a new folder somewhere on your harddisk, name it something like 'SeleniumTest'
  • Enter that folder. Open up your command-window in that folder and start

You can leave anything as is. Just make sure if you change the entrypoint, to remember this (you can also check this later in your package.json).

If you don't want to answer any of the questions, use this command:

Open this folder in your VSCode. You can enter 'code .' in the command, or open the folder in your VSCode.

Create an empty index.js. We will use that later.

Then use NPM to install the needed library:

It will install the required library. You can check if it did so by opening package.json and check if it has 'selenium-webdriver' under 'dependencies'.

It also installed a 'node_modules'-folder. Don't delete this!

Writing your test

Open up index.js, press CTRL+i and type:

This is written for Windows. If you're using Mac, use Command + i.

You should now have something like this:

The function might have some comments. You can freely delete those.

Be sure the function is async

Then, in the test()-function, write something like this:

And press [enter].

It should now give you some suggestions.

If it's not the right suggestion, hover over it and use the 'next' and 'previous'-icons to make sure you get something like:

If you're not using the Firefox Gecko browser, it shouldn't say 'firefox' but something else related to your chosen browser

Now it's time to build the test!

We're going to go to sibren.dev, click on the 'posts' link and check the title

Type:

The suggestion should be something like this:

Type:

It should then suggest something like:

Make sure you save your .js-file.

You should now be able to test it by typing the following into the terminal (where index is your entrypoint given earlier, without.js):

Now it should open a new Firefox-window (the first time it might take some time) and you should be able to see the browser going to sibren.dev and then loading the posts.

Final tests

If the site opened is the posts-page, be sure to check the title.

You can do so by typing the following:

It should now give you something like:

Then, make sure the title is correct:

When pressed [enter], it should give you something like:

Then run the 'node index'-command again and see what happens.

Roundup

Select the previously generated if-statement and use CTRL+i.

Type in:

It should now change the if-statement to something like:

This is just a small example on using GitHub Copilot for creating tests. Because AI is constantly evolving, the outcomes might differ a bit.

The last thing we need to do is close the browser after we're done. You can probably think of your own prompt by now to close it, but if you don't, you can just use this code:

If you want to know more about using GitHub Copilot in depth in VS Code and/or Visual Studio, please contact me on LinkedIn or go to Cavero.nl (in Dutch) and ask for a training on GitHub Copilot.