{"meta":{"title":"Web ページのエンドツーエンド テストの作成","intro":"Copilot Chat は、エンドツーエンドのテストの生成に役立ちます。","product":"GitHub Copilot","breadcrumbs":[{"href":"/ja/copilot","title":"GitHub Copilot"},{"href":"/ja/copilot/tutorials","title":"チュートリアル"},{"href":"/ja/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot クックブック"},{"href":"/ja/copilot/tutorials/copilot-cookbook/testing-code","title":"コードのテスト"},{"href":"/ja/copilot/tutorials/copilot-cookbook/testing-code/create-end-to-end-tests","title":"エンドツーエンド テストを作成する"}],"documentType":"article"},"body":"# Web ページのエンドツーエンド テストの作成\n\nCopilot Chat は、エンドツーエンドのテストの生成に役立ちます。\n\nHTML が動的に生成されるため、Web ページのエンドツーエンド テストの作成は時間がかかり、複雑になる場合があります。\nCopilot Chat は、Web ページを操作して期待される結果を検証するために必要なコードを提案することで、Web ページのエンド ツー エンドテストを作成するのに役立ちます。\n\n## サンプル シナリオ\n\nWeb ページに製品の詳細を表示する React アプリケーションがあるとします。 製品の詳細が正しく表示されることを確認するには、エンドツーエンド テストを作成する必要があります。 これらのテストを生成するように Copilot Chat に依頼できます。\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## プロンプトの例\n\n> \\[!NOTE] この例ではエンドツーエンド テストに Playwright を使っていますが、Selenium や Cypress などの他のフレームワークを使うこともできます。\n\n```copilot copy prompt ref=product-details\nUsing Playwright, generate an e2e test to ensure the product displays correctly.\n```\n\nモデルとして使用したい既存のテストがある場合は、Copilot`#file: path/to/test-file.js` を使用してそのファイルへのパスを指定し、その情報をプロンプトに含めることができます。\n\n## 応答の例\n\n> \\[!NOTE] 次の応答は例です。 Copilot Chat の応答は非決定的であるため、ここで示したものとは異なる応答が返される可能性があります。\n\nCopilot は、アプリケーションに使用できるエンドツーエンドのテストで応答します。\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## 詳細については、次を参照してください。\n\n* [GitHub Copilot Chat のプロンプト エンジニアリング](/ja/copilot/concepts/prompting/prompt-engineering)\n* [GitHub Copilot の使用に関するベスト プラクティス](/ja/copilot/get-started/best-practices)"}