You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Electron-Spectron中通过WebdriverIO获取video标签的源?

如何在Electron-Spectron中检查视频是否停止并获取视频源?

其实你不用纠结WebdriverIO有没有getElementById这种直白的API——它提供了更灵活的选择器方式,完全能满足你的需求。Spectron的app.client本质就是WebdriverIO的客户端实例,咱们可以用它的选择器和属性获取方法来搞定。

先给你捋清楚可行的步骤:

  • 定位视频元素:用WebdriverIO的$('#videoContainer')就可以直接通过ID定位到你的video标签,这和getElementById的效果完全一致,而且写法更简洁。
  • 检查视频是否停止:视频元素有个原生的paused属性,当视频处于停止状态时这个属性会返回true,我们可以用getAttribute方法获取它。
  • 获取视频源:同样用getAttribute获取视频元素的src属性就能拿到视频源地址。

下面是修改后的测试代码示例,先给你写贴合你原来风格的回调式写法:

.then("the player should stop the program", function() {
  // 通过ID定位视频元素
  return app.client.$('#videoContainer')
    .then(videoElement => {
      // 先检查视频是否停止
      return videoElement.getAttribute('paused')
        .then(isPaused => {
          console.log('视频是否处于停止状态:', isPaused);
          // 这里可以添加断言逻辑,比如验证isPaused为true
          // expect(isPaused).toBe(true);
          
          // 再获取视频源地址
          return videoElement.getAttribute('src')
            .then(videoSrc => {
              console.log('当前视频源:', videoSrc);
              // 返回结果供后续断言使用
              return { isPaused, videoSrc };
            });
        });
    });
});

如果你项目支持ES6的async/await,写法会更清晰易读:

.then("the player should stop the program", async function() {
  // 定位视频元素
  const videoElement = await app.client.$('#videoContainer');
  // 获取视频停止状态
  const isPaused = await videoElement.getAttribute('paused');
  console.log('视频是否处于停止状态:', isPaused);
  // 断言视频确实停止
  expect(isPaused).toBe(true);
  
  // 获取视频源
  const videoSrc = await videoElement.getAttribute('src');
  console.log('当前视频源:', videoSrc);
});

额外提一句:如果你的视频是播放结束后停止,还可以检查ended属性(返回true表示播放完毕);要是需要更精准的验证,也可以获取currentTime属性,观察它是否不再变化。

内容的提问来源于stack exchange,提问作者JeffChen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:17:30