Automating Playwright Tests from a Sitemap with Mistral AI
In this blog post, we’ll explore how to automate the generation of Playwright tests for web pages listed in a sitemap using the free Mistral API. This process involves fetching the sitemap, extracting URLs, and using Mistral AI to generate test scripts for each URL.
This is primarily a testing with the Mistral AI library for myself.
Script Overview
Requirement: Get the Mistral API Key and create .env:
1
MISTRAL_API_KEY=your_actual_api_key
1
2
# Fetch sitemap from URLsitemap_url="https://d-oit.github.io/en/sitemap.xml"
The script uses the requests library to fetch the sitemap from the specified URL.
For each URL, the script sends a prompt to Mistral AI to generate a Playwright test script.
As result the script returns the test code with markdown formatting. As an easy way we remove the markdown code formatting and have as result a .js file for using with Playwright.
importosimportrequestsimportxml.etree.ElementTreeasETfrommistralaiimportMistralfromdotenvimportload_dotenv# Fetch sitemap from URLsitemap_url="https://d-oit.github.io/en/sitemap.xml"response=requests.get(sitemap_url)root=ET.fromstring(response.content)# Extract URLs from sitemapurls=[url.textforurlinroot.findall('.//{http://www.sitemaps.org/schemas/sitemap/0.9}loc')]load_dotenv()# Set up Mistral APIapi_key=os.environ["MISTRAL_API_KEY"]client=Mistral(api_key=api_key)# Generate and save Playwright testsforurlinurls:prompt=f"Generate a production ready code to run it with Playwright test for all known use cases in JavaScript for the webpage at {url} .Only show me the code without any explanation, only the code. Thank you."response=client.chat.complete(model="mistral-large-latest",messages=[{"role":"user","content":prompt}])test_code=response.choices[0].message.contenttest_code=test_code.replace("```javascript","").replace("```","")print("url: "+url)# Extract the last part of the URL as the filenameurlName=url.replace("https://","").replace('/','_')# Create a sanitized filename from the URLfilename=f"test_{urlName}.spec.js"withopen(filename,'w')asfile:file.write(test_code)print(f"Generated {len(urls)} test files.")
...
1
pip install requests python-dotenv mistralai lxml
requests: for fetching the sitemap content over HTTP.
python-dotenv: for loading environment variables from a .env file.
mistralai: to interact with the Mistral API for generating code.
lxml: provides the ET (ElementTree) API for parsing XML data (you can also use xml.etree.ElementTree if lxml is unavailable).
Generated Playwright .js file
As a quick workaround for now we replace the markdown code formatting: