Using GitHub Copilot for UI Test Automation with Selenium+Java

August 28, 2024

No matter how long you've worked with Selenium, Copilot can help streamline the automation process. Read this blog by Senior QA Automation Engineer Konstantin Loginov to find more tips and insights for making GitHub Copilot work for you!

GitHub Copilot is revolutionizing the way developers approach Selenium test automation by offering AI-powered code suggestions directly within the IDE. 

Whether you're a seasoned tester or new to Selenium, Copilot's capabilities can help you automate tests more efficiently and effectively.

1. Generating setup methods 

It is enough to write only the annotation @BeforeEach and you will be provided with this setUp() method:

You just need to press Tab  to add the suggested method to your code:

The same is possible to generate the tearDown() method:

You do not need to spend more time on these trivial activities.

2. Helper methods

Also, we often need to create some helper methods, which are not provided by Selenium by default.

For this, it is enough to simply write a comment like “Method which check that all elements of collection is displayed on the page” and Copilot will generate whole method for it:

You just need to press the Enter button to grab it.

3. Creating autotests 

You can also use Copilot for generating autotests. Based on practice, the better suggestions for autotesting could be to write one test and ask Copilot to create others. In this case, your test will be used like a template:

Human-created test:

Copilot generated test based on existing autotest:

4. AI Hallucinations

Suggestions are not always correct. You need to be very careful with them. For example, suggestions are firstly based on your repositories. So the following suggestion initially looks good, but with another test framework (Selenide), because it is from another project:

A suggestion for a method which should check that WEB element has the exact text with using explicit wait of 10 seconds looks like:

Copilot has generated almost the correct method, but used timeout quantity in integer value, which is not acceptable for WebDriverWait class instance initialization. For these purposes we need use Duration.ofSeconds() method:

5. Copilot Chat

GitHub Copilot also provides the chat, where you can specify your request. For example it can be useful, when there are issues in the project. It aggregates information about each line of code inside and provides you with issue resolving. It is much better than copying all your classes to ChatGPT.

Also with the help of Copilot chat you can generate tests based on already written ones.

Conclusion

GitHub Copilot is the perfect tool which allows testers to increase their productivity, avoid routine tasks and be focused on more intellectual questions. Despite all advantages it is still necessary to carefully verify AI suggestions to bypass the AI hallucinations.

About the author

Konstantin Loginov is a Senior QA Automation Engineer at First Line Software, and has been with the team since 2019. Konstantin actively shares his expertise in QA automation with his colleagues with workshops, meetups, and mentorship as an FLS Tech Titan Ambassador.

Let’s talk!

Have any questions? Fill out the form and our team will be in touch!