我已经自动化了一个网络应用程序,可以打开相机并拍照。在我的本地计算机上,Selenium 脚本执行并捕获照片(在无头模式下运行)。但是,当我通过 Jenkins 服务器运行脚本时,没有捕获任何照片。自动化脚本是用 Java 11 编写的。我使用 Firefox 作为浏览器。我的 Selenium 版本是:“4.1.2”。在启动浏览器之前,我已通过以下配置文件授予对相机的访问权限:
// set the profile to allow camera access
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("media.navigator.permission.disabled", true);
firefoxOptions.setProfile(profile);
System.setProperty(systemProperty, driverPath);
webDriver = new FirefoxDriver(firefoxOptions);
逻辑告诉我这是因为 Jenkins 服务器没有摄像头。如果是这样的话,有没有办法伪造相机?感谢您对詹金斯服务器上的任何指点。相机
打开相机并拍照很简单,片段:
@Step ("capture photo")
public ConfirmPhotoPO capturePhoto(){
SeleniumWaitUtil.waitUntilElementToBeClickable(captureBtn);
// try capture a photo, and check if we can upload
clickElement(captureBtn);
return new ConfirmPhotoPO();
}
该应用程序是一个 Angular Web 应用程序。通过上述内容,期望打开相机页面并单击按钮并拍摄照片。但是,仅打开了相机页面(其中有拍摄照片按钮),但没有拍摄照片。
问题可能是相机没有任何来捕捉。提供一些可以在这一行捕获的模拟相机输入 -
clickElement(captureBtn);
还要确保虚拟环境可以处理相机输入模拟。
解决方案是启用一个假视频流,这是一个配置。作为 FireFox 选项提供。我将以下首选项添加到 FireFox 配置文件中
“media.navigator.streams.fake”,true
:
FirefoxOptions firefoxOptions = new FireFoxOptions();
// create a FireFox profile
FirefoxProfile profile = new FirefoxProfile();
// use the profile to enable access to the camera when the browser starts
profile.setPreference("media.navigator.permission.disabled", true);
// open a fake video stream that will allow the photo to be captured
profile.setPreference("media.navigator.streams.fake", true);
firefoxOptions.setProfile(profile);
通过上述设置,浏览器将启动,使用指定的配置文件授予对摄像头的访问权限,当摄像头打开时,会生成一个假视频流,该视频流由摄像头从被测应用程序捕获。