如何在基于 Chromium 的功能测试中使用导航器媒体设备? (测试咖啡馆)

How to use navigator media devices in Chromium based functional tests? (TestCafe)

TL;DR 我如何 运行 使用 navigator.mediaDevices 及其方法与 TestCafe 和 Chromium 的功能测试?

我正在尝试使用 TestCafe 编写功能测试来测试一些 WebRTC 代码。我正在使用 React。我有一个枚举设备的自定义挂钩:

function useUpdateDeviceList() {
  const dispatch = useDispatch();

  useEffect(() => {
    async function updateDeviceList() {
      const { audioInputs, videoInputs } = await getInputDeviceInfos();

      dispatch(setAudioInputs(audioInputs));
      dispatch(setVideoInputs(videoInputs));
    }

    updateDeviceList();

    console.log('navigator', navigator);

    navigator.mediaDevices.ondevicechange = updateDeviceList;

    return () => {
      navigator.mediaDevices.ondevicechange = null;
    };
  }, [dispatch]);
}

其中 getInputDeviceInfos 调用 navigator.mediaDevices.enumerateDevices.

当我在浏览器中 运行 这段代码时,它可以完美运行。当它在 TestCafe 的功能测试中得到 运行 时,它抛出。

TypeError: Cannot set property 'ondevicechange' of undefined

彻底的 Google 搜索只给出了将标志 --use-fake-ui-for-media-stream--use-fake-device-for-media-stream 添加到启动命令的答案(前者避免需要授予 camera/microphone权限和阶梯将测试模式提供给 getUserMedia(),而不是根据 docs) 的实时摄像头输入,我这样做了:

"functional-tests:chrome-desktop":"testcafe 'chrome --use-fake-ui-for-media-stream --use-fake-device-for-media-stream' src/**/*.functional-test.js --app 'yarn dev' --app-init-delay 2000",

还是出现同样的错误。上面钩子中的 console.log 显示了没有 mediaDevices.

navigator 对象
Navigator {vendorSub: "", productSub: "20030107", vendor: "Google Inc.", maxTouchPoints: 0, sendBeacon: ƒ, …}
appCodeName: "Mozilla"
appName: "Netscape"
appVersion: "5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
connection: NetworkInformation {onchange: null, effectiveType: "4g", rtt: 100, downlink: 1.55, saveData: false}
cookieEnabled: true
doNotTrack: null
geolocation: Geolocation {}
hardwareConcurrency: 16
language: "en-GB"
languages: (3) ["en-GB", "en-US", "en"]
maxTouchPoints: 0
mediaCapabilities: MediaCapabilities {}
mediaSession: MediaSession {metadata: null, playbackState: "none"}
mimeTypes: MimeTypeArray {0: MimeType, 1: MimeType, application/x-nacl: MimeType, application/x-pnacl: MimeType, length: 2}
onLine: true
permissions: Permissions {}
platform: "MacIntel"
plugins: PluginArray {0: Plugin, Native Client: Plugin, length: 1}
product: "Gecko"
productSub: "20030107"
sendBeacon: ƒ ()
userActivation: UserActivation {hasBeenActive: false, isActive: false}
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
vendor: "Google Inc."
vendorSub: ""
webkitPersistentStorage: DeprecatedStorageQuota {}
webkitTemporaryStorage: DeprecatedStorageQuota {}
__proto__: Navigator

我需要做什么才能通过这些测试?

PS: 以防万一,这是应该通过的简单测试:

import { Selector } from 'testcafe';

const {
  FUNCTIONAL_TESTS_MEETINGS_APP_PROTOCOL: protocol = 'http',
  FUNCTIONAL_TESTS_MEETINGS_APP_HOST: host = 'localhost',
  FUNCTIONAL_TESTS_MEETINGS_APP_PORT: port = 3000,
  FUNCTIONAL_TESTS_MEETINGS_APP_MEETING_ID: meetingId = '327fa7b0-0605-4595-b066-819f201ce593',
} = process.env;

fixture`Meetings page`.page(`${protocol}://${host}:${port}/${meetingId}`);

test.only('page should load and display the correct title', async t => {
  await t.debug();
  const actual = Selector('title').innerText;
  const expected = `Meetings - ${meetingId}`;

  await t.expect(actual).eql(expected);
});

Chrome 不允许从不安全的来源调用 getUserMedia API。因此,您可以使用以下任一方式:

  1. 通过添加此选项将 'localhost' 指定为主机名:--hostname
testcafe --hostname localhost ...
  1. 通过添加此选项使用 SSL 证书:--ssl
testcafe --ssl pfx=/path/to/cert.pfx ...

更多信息,请参考以下示例:Mock Camera/Microphone Access