{"meta":{"title":"Creating end-to-end tests for a webpage","intro":"Copilot Chat can help with generating end-to-end tests.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/copilot","title":"GitHub Copilot"},{"href":"/en/copilot/tutorials","title":"Tutorials"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat Cookbook"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/testing-code","title":"Testing code"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/testing-code/create-end-to-end-tests","title":"Create end-to-end tests"}],"documentType":"article"},"body":"# Creating end-to-end tests for a webpage\n\nCopilot Chat can help with generating end-to-end tests.\n\nCreating end-to-end tests for a webpage can be time-consuming and complex as the HTML will be generated dynamically. Copilot Chat can help you create end-to-end tests for a webpage by suggesting the necessary code to interact with the webpage and validate the expected results.\n\n## Example scenario\n\nImagine a React application that displays product details on a webpage. You need to create end-to-end tests to ensure the product details are displayed correctly. You can ask Copilot Chat to generate these tests for you.\n\n```javascript id=product-details\nimport React, { useState, useEffect } from 'react';\nimport PropTypes from 'prop-types';\n\nconst ProductDetails = ({ productId = '1' }) => {\n  const [product, setProduct] = useState(null);\n  const [loading, setLoading] = useState(true);\n  const [error, setError] = useState(null);\n\n  useEffect(() => {\n    const fetchProduct = async () => {\n      try {\n        const response = await fetch(`/api/product/${productId}`);\n        if (!response.ok) {\n          throw new Error('Product not found');\n        }\n        const data = await response.json();\n        setProduct(data);\n        setLoading(false);\n      } catch (err) {\n        setError(err.message);\n        setLoading(false);\n      }\n    };\n\n    fetchProduct();\n    return;\n  }, [productId]); // Add productId to dependency array\n\n  if (loading) return <div>Loading...</div>;\n  if (error) return <div>Error: {error}</div>;\n\n  return (\n    <div>\n      {product && (\n        <div>\n          <h2>{product.name}</h2>\n          <p>{product.description}</p>\n          <p>Price: ${product.price}</p>\n        </div>\n      )}\n    </div>\n  );\n};\n\nProductDetails.propTypes = {\n  productId: PropTypes.string\n};\n\nexport default ProductDetails;\n```\n\n## Example prompt\n\n> \\[!NOTE] This example uses Playwright for end-to-end testing, but you can use other frameworks like Selenium or Cypress.\n\n```copilot copy prompt ref=product-details\nUsing Playwright, generate an e2e test to ensure the product displays correctly.\n```\n\nIf you have an existing test you wish Copilot to use as a model, you can use `#file: path/to/test-file.js` to provide the path to the file, and include this information in the prompt.\n\n## Example response\n\n> \\[!NOTE] The following response is an example. Copilot Chat responses are non-deterministic, so you may get a different response from the one shown here.\n\nCopilot will respond with an end-to-end test that you can use for your application.\n\n```typescript\nimport { test, expect } from '@playwright/test';\n\ntest.describe('ProductDetails Component', () => {\n  test.beforeEach(async ({ page }) => {\n    // Set up API mocking\n    await page.route('/api/product/1', async (route) => {\n      await route.fulfill({\n        status: 200,\n        contentType: 'application/json',\n        body: JSON.stringify({\n          name: 'Test Product',\n          description: 'This is a test product',\n          price: 99.99\n        })\n      });\n    });\n\n    // Mock error response for product ID 999\n    await page.route('/api/product/999', async (route) => {\n      await route.fulfill({\n        status: 404,\n        contentType: 'application/json',\n        body: JSON.stringify({ error: 'Product not found' })\n      });\n    });\n  });\n\n  test('displays loading state initially', async ({ page }) => {\n    await page.goto('/product/1');\n    const loadingElement = page.locator('text=Loading...');\n    await expect(loadingElement).toBeVisible();\n  });\n\n  test('displays product details correctly', async ({ page }) => {\n    await page.goto('/product/1');\n\n    // Wait for loading to disappear\n    await expect(page.getByText('Loading...')).toBeHidden();\n\n    // Check all product details\n    await expect(page.locator('h2')).toHaveText('Test Product');\n    await expect(page.locator('text=This is a test product')).toBeVisible();\n    await expect(page.locator('text=Price: $99.99')).toBeVisible();\n  });\n\n  test('displays error message when product not found', async ({ page }) => {\n    await page.goto('/product/999');\n\n    // Wait for loading to disappear\n    await expect(page.getByText('Loading...')).toBeHidden();\n\n    // Check error message\n    await expect(page.locator('text=Error: Product not found')).toBeVisible();\n  });\n});\n```\n\n## Further reading\n\n* [Prompt engineering for GitHub Copilot Chat](/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [Best practices for using GitHub Copilot](/en/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}