Leveraging AI (Large Language Models) to write scripted automated tests
Recently, I was asked by a colleague how I use AI or Large Language Models (LLMs) to write Playwright tests.
Here’s a summary of my approach:
1) Generate Playwright Test Skeletons
Some examples of Large Language Models:
OpenAI ChatGPT: https://chatgpt.com/
Google Gemini: https://gemini.google.com/app
Perplexity AI: https://www.perplexity.ai/
Microsoft Bing Copilot: https://www.bing.com/chat
Create 10 Playwright TypeScript tests for https://www.example.com.
The Large Language Model (LLM) chatbot provides a foundational test structure, which can be refined and expanded based on specific requirements. To ensure comprehensive coverage, I often run the same query through multiple LLMs and then consolidate the generated ideas into a robust test suite.
2) Request Specific Syntax for Custom Functions or Assertions
领英推荐
i) What is an alternative Playwright TypeScript wait syntax if await page.waitForTimeout(3000); fails?
ii) List typical web page element assertion syntaxes for Playwright TypeScript.
iii) Write a custom function in Playwright TypeScript script to handle multiple overlays that appear on the Webpage in different Web page components? (all overlay objects share the same class name 'overlay').
Test automation frameworks offer a wide range of commands, but memorizing all the possible syntaxes can be challenging. LLMs become invaluable in this context by providing exact syntax or crafting custom functions tailored to specific scenarios.
The specific example queries/prompts mentioned above allow me to bypass documentation searches and get directly to coding, reducing time spent on manual lookups.
3) Debug and Troubleshoot Failing Tests
This Playwright test script encountered with error as follows. Please help to fix the error.
(A) Playwright test script
"""
// Comparison using Regular Expression
await expect(page.locator("[class*='countAndSortByArticles']", {timeout: 10000})).toContainText(/^[1-9] Articles$/);
"""
(B) Error:
"""
[chromium] ? loggedInHomePageSearch.spec.ts:238:9 ? Home page visual check using logged in user ? Search using valid article description with at least 3 characters should render auto-suggestion drop down options on "Home page for logged in user
Error: Timed out 5000ms waiting for expect(locator).toContainText(expected)
Locator: locator('[class*=\'countAndSortByArticles\']')
Expected pattern: /^[1-9] Articles$/
Received string: " 5 Articles Sort by: Name (ascending)·················································································································
Name (descending)·····························································································
Name (ascending)·································································································
"""
4. Convert Cypress Tests to Playwright
Convert this Cypress JavaScript test script to a Playwright Typescript test script.
"""
describe('Webpage header visual check for anonymous user', () => {
beforeEach(() => {
cy.viewport(1920, 1080);
cy.visit(Cypress.env('baseUrl'));
const bannerClosed = "OptanonAlertBoxClosed";
const bannerClosedValue = "2023-12-22T04:41:54.468Z";
Cypress.on("window:before:load", (window) => {
window.document.cookie = `${bannerClosed}=${bannerClosedValue}`;
});
});
it('should display Landing page with default header text', () => {
cy.contains('Landing Page').should('be.visible');
cy.contains('Products').should('be.visible');
cy.contains('Support').should('be.visible');
cy.contains('Login').should('be.visible');
});
it('click on "Login" redirects to federation login URL', () => {
cy.contains('Login').click({ force: true });
cy.url().should('include', '/app/login');
});
// Additional tests follow...
});
"""
Migrating tests from one framework to another is a common task in test automation. Using LLMs, I can quickly convert scripts written in one framework to scripts compatible with another framework.
#playwright #llm #ai #automatedtest #scriptedtest #autotestgeneration