3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Python]Playwrightを用いたE2Eテスト方法メモ

Posted at
  • ブラウザ自動操作ツールPlaywrightを用いたテスト方法についてメモする。

事前準備

  • ライブラリ類インストール

    pip install playwright
    playwright install
    pip install pytest-playwright
    
  • テストレポートツールallureインストール

    • 筆者はWindows環境を利用しているため、パッケージ管理ツールscoopを使用する。

      • scoopインストール方法はこちらを参照。
      scoop install allure
      pip install allure-pytest
      

テストコード

  • test.py

    1. 「Playwright Python」でGoogle検索
    2. 検索結果に含まれるPlaywright-Python公式GitHubページにアクセス
    3. ページタイトルを検証
    from playwright.sync_api import Playwright, sync_playwright, Page
    import allure
    
    
    @allure.title('「Playwright Python」でGoogle検索し、検索結果に含まれる公式GitHubページにアクセスする')
    def test_google(playwright: Playwright) -> None:
    
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
    
        page = context.new_page()
    
        page.goto("https://www.google.com")
    
        page.goto(
            "https://www.google.com/search?q=Playwright+Python&oq=Playwright+Python")
    
        # Click text=microsoft / playwright-python Public - GitHub
        page.click("text=microsoft / playwright-python Public - GitHub")
    
        # ページタイトル検証
        assert page.title() == "GitHub - microsoft/playwright-python: Python version of the Playwright testing and automation library."
    	# Close page
        page.close()
    
        # ---------------------
        context.close()
        browser.close()
    
    with sync_playwright() as playwright:
        test_google(playwright)
    
    

動作確認

  1. テストコード実行

    pytest test.py --alluredir=allure_results
    =========================================================== test session starts =========================================================== 
    platform win32 -- Python 3.8.3, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
    rootdir: C:\Users\user\Documents\dev\vscode\ws_git\e2e_test\playwright\test
    plugins: allure-pytest-2.9.43, anyio-3.2.1, asyncio-0.15.1, base-url-1.4.2, cov-2.11.1, playwright-0.2.0
    collected 1 item                                                                                                                            
    
    test.py .                                                                                                                            [100%] 
    
    =========================================================== 1 passed in 10.18s ============================================================
    
  2. テスト結果確認(レポートページが起動する)

    allure serve allure_results
    

    allure_report.png

参考情報

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?