Effortless Test Automation: Leveraging GitHub Copilot for Cypress Testing

Posted 6 months ago by Jakub Janoska

AI and AI-powered development have become incredibly hot topics in the technology industry. The potential of AI to revolutionize various fields is widely acknowledged, and while impressive, is still being developed. Researchers, developers, and organizations are actively exploring new ways to use AI. In this dynamic landscape, we at profiq are constantly seeking out and experimenting with new libraries, plugins, and tools that leverage AI. These resources aim to enhance productivity, automate repetitive tasks, and unlock new possibilities in software development.

One such resource is GitHub Copilot (Copilot). Copilot is in a “ready to be used state”, and used by developers at profiq for simple unit tests. In this article, we took Copilot for a more complicated test drive to learn how productive it can be in generating web automation tests.

GitHub Copilot

Copilot is an AI-powered code completion tool introduced by GitHub in June 2021. It utilizes OpenAI’s ChatGPT model to help developers write code more efficiently. By analyzing the context and patterns of the code being written, Copilot suggests relevant code snippets and completions in real time. It supports multiple programming languages and integrates seamlessly into popular code editors like Visual Studio Code (VSC), IntelliJ IDEA, and others. We recommend trying it in VSC, as Copilot has the most features and newest advances. 

To install Copilot, go to the official GitHub Copilot website and follow the installation instructions specific to their preferred code editors: Getting started with GitHub Copilot.

Unveiling our journey

We used VSC’s editor to write Cypress automated tests in TypeScript.

Because Copilot isn’t familiar with your website, you need to write a page object pattern for it which describes the elements on your website in your code. After adding imports for page objects and data, as well as initial comments, Copilot astoundingly provided suggestions for the following remaining comments…

…and even generated a complete test suite. What’s more, the generated suite was immediately runnable without any manual modifications.

It’s impressive that Copilot generated a test suite with six test cases (as we specified in the comments), and only one required updating while the rest passed. Copilot was not able to handle the complexity of non-standard login form where the password field is displayed after entering an email, but after providing additional comments to guide Copilot, it accurately wrote the necessary code to handle this unique scenario. This demonstrates Copilot’s capacity to understand your intentions and generate code accordingly, saving you time and effort in manual coding.

Copilot’s inline code suggestions are invaluable assets in elevating our productivity. With the ability to accept suggestions using the Tab key or dismiss them with Esc, seamlessly incorporating the suggested code has become effortless. Furthermore, Copilot enhances our coding experience with convenient shortcuts for effortless navigation through suggestions. In addition, Copilot provides the flexibility of displaying suggestions in a separate panel, maximizing our coding efficiency. Here is a table summarizing the useful shortcuts for VSC when utilizing Copilot:

Action Shortcut – Windows / Linux Shortcut – macOS
Accept inline code suggestion Tab Tab
Dismiss inline code suggestion Esc Esc
Show next suggestion Alt + ] Option (⌥) + ]
Show previous suggestion Alt + [  Option (⌥) + [
Trigger suggestion  Alt + \ Option (⌥) + \
Open GitHub Copilot (additional suggestions in separate panel) Ctrl + Enter Ctrl + Enter

Contrasting GitHub Copilot and ChatGPT: Unleashing the power of AI in coding and conversations

GitHub Copilot and ChatGPT are both powerful AI models developed by OpenAI, but they serve different purposes and have different capabilities. Here’s a comparison between the two:

Copilot ChatGPT
Purpose It is designed to help developers write code. It integrates with code editors and provides code suggestions, completions, and even entire code snippets based on the context and code patterns. ChatGPT, on the other hand, is a conversational AI model. It’s trained to generate human-like responses in a conversational format and can understand and respond to a wide range of topics and queries.
Training Data It is trained on a massive dataset of publicly available code from various sources, including GitHub repositories. It learns from millions of lines of code and uses that knowledge to provide code suggestions and completions. It is trained on a diverse range of internet text, including articles, books, and websites. It doesn’t have direct access to code repositories like Copilot does.
Context and Output It works within the context of code editors and assists developers in writing code. It provides suggestions based on the code being written and can generate whole code snippets. It is designed for general conversation. It takes a series of messages as input and generates a response based on the conversation history. It doesn’t have direct knowledge of code-related contexts.
Usage It is primarily used by developers during software development. It helps speed up coding tasks by providing suggestions and reducing the need to write repetitive code. It can be used for a wide range of conversational applications, such as customer support, chatbots, and interactive dialogue systems.
Interface and Integration It integrates as a plugin with popular code editors like VSC, and provides suggestions directly within the editor interface. It can be integrated into various applications and platforms through APIs, and used by developers to build custom conversational interfaces.

Conclusion

In summary, GitHub Copilot is a specialized tool for helping developers write software with code suggestions and completions. ChatGPT, on the other hand,  is a general conversational AI model that can be used for a wide range of conversational applications. Both have their own specific use cases and areas of expertise.

Copilot can also provide suggestions based on the content of files that are currently opened in your IDE. When you have a file open in your IDE, Copilot’s suggestions are influenced by the code you’re actively working on. It can analyze the code within the opened files and provide contextually relevant suggestions based on the patterns and examples it learned from publicly available code repositories. This feature helps Copilot provide more accurate and specific suggestions tailored to your project’s codebase.

This experience brought immense relief and streamlined our work significantly. However, it’s important to note that while Copilot is incredibly helpful, it still requires the knowledge and expertise of a software engineer to properly specify their requirements, choose the right Copilot design, and address any issues that may arise.

It’s important to consider the cost associated with utilizing this powerful tool. Copilot is a paid service with individual plans priced at $10 per month and business plans at $19 per month. If you factor in the cost of developer time, we think the investment is quickly paid in saved time. You can try it yourself with the free trial which is offered there.

If you are a developer and you are considering buying either ChatGPT or Copilot to help your coding, we recommend Copilot. From our experience, the paid model (ChatGPT 4) is not that big of an improvement for generating code, and you can do just fine with the free version of ChatGPT or Bard.

 

Jakub Janoska

One Response to “Effortless Test Automation: Leveraging GitHub Copilot for Cypress Testing”

  1. What level of experience or familiarity with GitHub Copilot is recommended for individuals interested in implementing it for Cypress testing automation?

Leave a Reply

Related articles