LoginSignup
1
1

More than 1 year has passed since last update.

【python】playwrightでパラメータテストする方法

Last updated at Posted at 2022-05-20

1.やりたいこと

playwrightで作ったテストをデータ駆動でテスト実施できるようにします。
これを実現するためにpytestを活用して実現したいと思います。pytestの「@pytest.mark.parametrize()」は、対象のテスト関数にパラメータを渡し、パラメータセットの分だけ繰り返しテストをする仕組みです。

2.環境

playwrighとpytestが入っている環境を用意します。環境構築方法は、別記事に記載しています。
 ・macOS 12.4(monterey)
 ・Python 3.7.6
 ・pip 22.1
 ・pytest 6.2.5
 ・playwright 1.16.2

フレームワーク 内容
Playwright マイクロソフト社が作っているブラウザを自動で操作する
pytest Pythonで書いたプログラムをテストする

3.環境設定

rootフォルダの直下に実行ファイルを作成します。
※ 実行ファイルのファイル名は、pytestの仕様上「test_xxx.py」としてください。

root
|- test_sample.py

4.コード

実行ファイルに下記のようなプログラムを書きます。
自動操作対象のWebサイトは、UI Test Automation Playgroundを利用します。

test_sample.py
from playwright.sync_api import Page
import pytest

# パラメータセット
@pytest.mark.parametrize("target_Button,expected_result",[
("text=Click","Click"),
("text=Dynamic ID","Dynamic ID")
])

# テスト関数
def test_click(page: Page, target_Button, expected_result):
    page.goto("http://www.uitestingplayground.com/")
    #pytest.mark.parametrizeのtarget_Buttonの値をクリックする
    page.click(target_Button)
    #pytest.mark.parametrizeのexpected_resultの値があるか確認する
    assert page.inner_text("xpath = /html/body/section/div/h3") == expected_result

5.実行

ターミナルを起動後rootフォルダーに移動し、「test_sample.py」を実行します。

実行結果
test_sample.pyのtest_click()が2回テストを行い、その結果が返ってきました。

スクリーンショット 2022-05-20 13.55.58.png

6.関連記事

【python】playwrightでE2E評価環境を作る
【python】playwrightの実行中の操作を録画する方法
【python】playwrightでログイン処理省略する方法

1
1
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
1
1