GitHub Copilot practical guide to writing test scripts: QA uses AI to write Playwright and Selenium tests
Teach QA engineers step-by-step how to use GitHub Copilot to write Playwright and Selenium automated test scripts, including Prompt techniques, practical cases, common pitfalls, and best practices.
Last Updated:2026-05-24
Table of Contents
1. Why should QA use Copilot to write tests?
QA engineers spend a lot of time writing highly repetitive test scripts every day: login process, form validation, CRUD operations. These tests have similar logic, but writing each one manually is time-consuming. GitHub Copilot can automatically complete test code based on your annotations, function names, and context. Not to replace your judgment, but to speed up your hands.
-
Speed Improvement: The Writing Time Of Highly Repetitive Test Scripts Is Reduced By 50-70% On Average
-
Reduce Grammatical Errors: The Code Generated By Copilot Has A High Grammatical Accuracy, Which Reduces The Time Of Debugging Grammatical Problems.
-
Learn A New Framework: Even If You'Re New To Playwright, Copilot Can Produce The Correct Api Calls For Your Intent
-
Stay Focused: Let Ai Handle "How To Write" And You Focus On "What To Measure"
2. Environment preparation: 5 minutes to set up
Before you start, you need to prepare the following tools.
-
Vs Code: Install The Latest Version Of Visual Studio Code
-
Github Copilot Extension: Search And Install In The Vs Code Extension Store. The Free Plan Has A Monthly Usage Limit, And The Paid Version Is Us$10/Month.
-
Node.Js: Install Node.Js 18+ (Required For Playwright)
-
Playwright: Execute Npm Init Playwright@Latest Initialization Project
-
Python + Selenium (Optional): If Your Team Uses Python, Pip Install Selenium Pytest
Tip
- It is recommended to enable Copilot Chat (sidebar). You can directly ask AI to generate the entire test through dialogue.
3. Prompt tips: Make Copilot produce high-quality tests
The quality of Copilot's output depends on the context you give it. The following are the most effective prompt techniques that have been tested.
-
Tip 1 — Use Comments To Describe Test Intent: Write Clear Comments In The Code, For Example // Test: User Login With Valid Credentials Should Redirect To Dashboard
-
Tip 2 - Write The Test Name First: Test('Should Display Error When Password Is Less Than 8 Characters') And Then Let Copilot Complete The Content
-
Tip 3 - Provide Page Object Context: Write The Page Object First, And Copilot Will Automatically Combine The Test Steps According To The Available Methods
-
Tip 4 - Batch Generation Using Copilot Chat: Enter "Help Me Generate Positive, Negative And Boundary Value Tests For The Login Function Based On The Following Page Elements" In Chat
-
Tip 5 - Give Examples To Let Ai Learn Your Style: First Write A Complete Test By Hand As An Example, And Copilot Will Imitate Your Style And Patterns In Subsequent Tests.
4. Practical case: Playwright login test
The following is the complete process for generating a Playwright login test using Copilot. You only need to write comments and Copilot will generate most of the code.
-
Step 1: Create The Test File Login.Spec.Ts And Write The Describe And Test Titles
-
Step 2: Use Comments To Describe Each Step, Such As // Navigate To Login Page, Fill Email And Password, Click Submit
-
Step 3: Copilot Will Generate Page.Goto, Page.Fill, Page.Click And Other Calls Based On Your Description.
-
Step 4: Review And Add Assertions (Copilot Often Misses Key Assertions)
-
Step 5: Execute Npx Playwright Test To Verify Whether The Test Passes
Tip
- The locator produced by Copilot may not be stable enough. It is preferable to use stable selectors such as data-testid or getByRole instead.
- Be sure to add "negative testing" - Copilot usually only generates happy paths
5. Practical case: Selenium + Python API testing
The combination of Selenium + pytest is commonly used by Python QA teams. Copilot also performs well in the Python environment.
-
Use Docstring To Describe The Purpose Of The Test: Def Test_Add_To_Cart(): Add """Verify That Clicking Add To Cart Button Increases Cart Count By 1"""
-
Copilot Will Automatically Generate Driver.Find_Element, Driver.Click, Assert And Other Statements
-
With Pytest Fixtures: First Write The Setup/Teardown Of @Pytest.Fixture, Copilot Will Correctly Reference It In The Test
-
Paired With Conftest.Py: Copilot Can Read The Shared Settings And Fixtures In Conftest.Py And Use Them Correctly In New Tests
6. Common pitfalls of Copilot output testing
The tests produced by AI are not directly usable finished products. The following are the key points that must be checked manually.
-
Insufficient Assertions: Copilot Often Produces Action Steps But Misses Assertions, Or Only Adds A Weak Assertion. Each Test Must Have At Least One Meaningful Assertion
-
Waiting Strategy Error: Ai May Use Page.Waitfortimeout(3000), A Hard-Coded Wait, Which Should Be Changed To Page.Waitforselector Or Expect(Locator).Tobevisible()
-
Locator Instability: Ai Tends To Use Css Classes Or Xpath As Locators, Which Are Easily Broken When The Ui Changes. Use Data-Testid Or Playwright'S Getbyrole Instead
-
Lack Of Cleanup Steps: Failure To Clean Up Test Data After The Test Is Completed Will Affect Other Tests. Make Sure There Is Aftereach Or Teardown
-
Over-Reliance On Happy Path: Ai-Generated Tests Usually Only Cover Normal Processes, And You Need To Manually Supplement Error Scenarios, Boundary Values, And Permission Tests
Important Notes
Test scripts produced by AI must be manually reviewed before they can be added to the CI/CD pipeline. Unreviewed testing can produce false positives, giving you a false sense of security.
7. Advanced: Batch Generate Tests with Copilot Chat
Copilot Chat can generate the entire test file at once, which is more efficient than line-by-line auto-completion.
-
Full Page Test Generation: "@Workspace Helped Me Use Playwright To Generate A Complete Login Function Test Based On The Method Of Loginpage.Ts, Including Positive, Negative And Boundary Values"
-
Generate Based On The Api File: "Help Me Generate Api Integration Tests Based On The Following Swagger Api Definition" And Then Paste The Api Spec
-
Generate A Regression Test Based On The Bug Report: "This Bug Is That The Form Crashes When The User Enters Special Characters. Help Me Write A Regression Test To Ensure That The Fix Is Effective."
-
Batch Production Of Page Object: "Based On This Html Page Structure, Help Me Create A Page Object Model, Including All Interactive Elements."
Tip
- Use the @workspace tag to let Copilot read the context of the entire project, and the generated code will be more in line with the project style.
- After generation, use npx playwright test --debug to execute step by step to quickly find out what needs to be corrected.
8. Best practices and team introduction suggestions
The following is a summary of the experience of successfully importing Copilot to write tests in the team.
-
Establish "Copilot Test Specifications": Define Which Tests Are Suitable For Ai Output (Crud, Form Validation, Api Testing) And Which Must Be Handwritten (Security Testing, Business Logic, Exploratory Testing)
-
Write The Page Object First And Then Let Ai Write The Test: With A Good Page Object Layer, The Quality Of The Tests Produced By Copilot Will Be Greatly Improved.
-
Create Test Templates: Put A Few Well-Written Tests As Examples In The Project, And Copilot Will Learn Your Style
-
Code Review Must Include "Ai Output Annotation": Let The Reviewer Know What Is Generated By Ai And Focus On Reviewing These Parts
-
Review The Test Quality Of Ai Output Every Week: Track The False Positive Rate And Maintenance Cost Of Ai Tests, And Continuously Optimize The Prompt Strategy
Key Takeaways
- 1 GitHub Copilot can automatically generate test scripts based on annotations and context, and QA can write tests 2-3 times faster
- 2 The key is not to let AI write tests from scratch, but to "give good context" to let AI produce high-quality first drafts of tests.
- 3 Playwright + Copilot is currently the AI testing combination with the highest CP value, and both have free plans
- 4 Testing of AI output must be manually reviewed, paying special attention to whether the assertions are complete and whether the waiting strategy is reasonable.
Related Links
Related Quick Guides
AI automated testing tools complete comparison 2026: Testim, mabl, Katalon AI and other 6 major tool reviews
An in-depth comparison of the top 6 AI automated testing tools in 2026, covering the functions, prices, and applicable scenarios of Testim, mabl, Katalon AI, Applitools, Codium AI, and Playwright AI.
AI-assisted testing practice: 5 scenarios to double your testing efficiency
From test case generation to visual regression, we will teach you step by step how to introduce AI tools into daily QA work, with practical steps and tool recommendations.
Guide to AI-driven test report analysis and defect prediction: Let data help you find high-risk modules
Teach the QA team to use AI tools to analyze test reports, predict high-risk modules, automatically generate defect trend reports, and transform from passive bug repair to proactive prevention, with practical tools and operating steps.
General Disclaimer
The information provided on this site is for reference only. We do not guarantee its completeness or accuracy. Users should determine the applicability of the information on their own.