LoginSignup
0
0

More than 1 year has passed since last update.

【TestCafe】TestCafeで変数をまとめる方法

Posted at

ctxオブジェクトを使って変数をまとめる

TestCafeでE2Eテストを行うコードを書いていると同じ文字だったり、数字を使ったりすることがあります。
TestCafeではctxオブジェクトを使うと、それらを変数としてまとめることができ、とても便利です。
また、メンテナンスも楽になります。

まとめてみる

例えば、下のようなテストコードがあるとします。
極端な例ですが、要素1,2,3がすべてtestを持っているとすると、毎回記述しなければなりません。
これを変数に置き換えてあげることで、変数の値のみを変えるだけで済みます。

test1
fixture `Example`
  .page `https://www.example.com`;

test('test1', async t => {
  await t
    .expect("要素1").eql('test')
    .expect("要素2").eql('test')
    .expect("要素3").eql('test')
});

変数を使う

test1
fixture `Example`
  .page `https://www.example.com`;

test('test1', async t => {
  t.ctx.value = 'test';
  await t
    .expect("要素1").eql(t.ctx.value)
    .expect("要素2").eql(t.ctx.value)
    .expect("要素3").eql(t.ctx.value)
});
0
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
0
0