Testcafe ClientFunction для установки свойства видео currentTime - PullRequest
1 голос
/ 22 марта 2019

Я пытаюсь установить свойства видео с помощью Testcafe на play(), pause(), получить текущее время воспроизведения и установить текущее время.

Проблема в том, что я жестко кодирую установленное время, и в идеале я бы хотел, чтобы это была функция, которой я мог бы передать любое значение time, которое я хочу.

Я написал следующий простой тест:

import { ClientFunction } from 'testcafe';

const URL = 'https://www.youtube.com/watch?v=RWQtB6Xv01Q';

fixture `Portal Experience playback`
  .page `${URL}`;

function sleep (ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

test('Testing YouTube', async t => {
  const play = ClientFunction(() => document.querySelector('.video-stream').play());
  const pause = ClientFunction(() => document.querySelector('.video-stream').pause());
  const currentTime = ClientFunction(() => document.querySelector('.video-stream').currentTime);
  const setTime = await t.eval(() => document.querySelector('.video-stream').currentTime = 5);

  await setTime;
  console.info(`Video time is ${await currentTime()}`);
  await play;
  await sleep(5000);
  console.info(`Video time is ${await currentTime()}`);
  await pause;

});

play, pause и currentTime Я могу просто скопировать и перейти к классу внутри модели страницы.

Модель страницы будет:

export class Player {
  constructor () {
    this.play = ClientFunction(() => document.querySelector('.video-stream').play());
    this.pause = ClientFunction(() => document.querySelector('.video-stream').pause());
    this.currentTime = ClientFunction(() => document.querySelector('.video-stream').currentTime);
  }

  // a function to set video time

}

Как мне превратить setTime в функцию внутри модели страницы?

1 Ответ

2 голосов
/ 24 марта 2019

Вам необходимо указать параметр time в клиентской функции setTime:

ClientFunction((time) => document.querySelector('.video-stream').currentTime = time);

Модифицированный тест:

test('Testing YouTube', async t => {
    const play        = ClientFunction(() => document.querySelector('.video-stream').play());
    const pause       = ClientFunction(() => document.querySelector('.video-stream').pause());
    const currentTime = ClientFunction(() => document.querySelector('.video-stream').currentTime);
    const setTime     = ClientFunction((time) => document.querySelector('.video-stream').currentTime = time);

    await pause();

    await setTime(60);
    console.info(`Video time is ${await currentTime()}`);

    await play();

    await t.wait(10000);
    console.info(`Video time is ${await currentTime()}`);
});

Мы планируем добавить флаг --autoplay-policy=no-user-gesture-required к флагам Chrome по умолчанию . В настоящее время вы должны выполнить тест следующим образом :

testcafe "chrome --autoplay-policy=no-user-gesture-required" test.js

Результат:

 Running tests in:
 - Chrome 73.0.3683 / Windows 7.0.0

 Portal Experience playback
 Video time is 60
 Video time is 70.130405
  √ Testing YouTube


  1 passed (22s)
...