【问题标题】:Testcafe how to declare ClientFunction in Factory pageTestcafe如何在Factory页面声明ClientFunction
【发布时间】:2021-08-06 17:31:27
【问题描述】:

我试图在工厂页面上声明一个 ClientFunction,然后在测试页面上调用它。但我做错了什么,它不起作用。我有两个页面,一个是工厂页面,第二个是测试页面。 在测试页面中,我有 ClientFunction,它工作正常。当我尝试移至工厂页面时,它不起作用。

import {Selector, ClientFunction} from "testcafe";
import videoPage from "../pages/video_player_page";

const videoElement = Selector("#bod-video-player_html5_api");

const currentTime = ClientFunction(() => {
  return videoElement().currentTime;
}, {dependencies: {videoElement}});

test
    .page(`some video page url`)
    ("Verify video functionality in LO State ", async (t) => {
     let m1 = currentTime();
           console.log("m1 = " + m1);
      await t
          .click(videoPage.fwdButton)
          .expect(videoPage.videoPlayer.hasClass("vjs-seeking")).notOk();
          let m2 = currentTime();
          console.log("m2 = " + m2);
      await t.expect(m2).gt(m1+25, "fwd button not working");
});

在我的工厂页面中

import {Selector, t, ClientFunction} from "testcafe";
import {selectors} from "../constants/video_player_page_constants";
import {errorMessages} from "../constants/error_messages/video_player_page_messages";

class VideoPlayerPage {
  constructor() {
    this.skipAd = Selector(`${selectors.skipAdLocator}`, {timeout: 50000});
    this.waiverModal = Selector(`${selectors.warningModalInVideoLocator}`);
    this.videoPlayer = Selector(`${selectors.videoPlayerLocator}`);
    this.videoPlaying = selectors.videoPlaying;
    this.videoPaused = selectors.videoPaused;
    this.waiverModalCheckbox = Selector(`${selectors.warningModalCheckbox}`);
    this.waiverModalCheckboxCss = selectors.warningModalCheckbox;
    this.acceptButton = Selector(`${selectors.warningModalAcceptButton}`);
    this.playPauseButton = Selector(`${selectors.playPauseButtonLocator}`);
    this.fwdButton = Selector(`${selectors.fwdButtonLocator}`);
    this.bwdButton = Selector(`${selectors.bwdButtonLocator}`);
    this.videoElement = Selector("#bod-video-player_html5_api");
  }


  async acceptWaiver() {
    await t
        .expect(this.waiverModal.exists).ok(errorMessages.warningModalNotExist)
        .hover(this.waiverModalCheckboxCss)
        .click(this.waiverModalCheckbox)
        .click(this.acceptButton);
  }

  async skipAdd() {
    if (await this.skipAd.visible) {
      await t.click(this.skipAd);
    }
  }

} export default new VideoPlayerPage();

如何在Factory页面声明ClientFunction,感谢帮助。

【问题讨论】:

    标签: javascript testing automated-tests e2e-testing testcafe


    【解决方案1】:

    你可以这样做:

    import { ClientFunction } from 'testcafe';
    
    class VideoPlayerPage {
        currentTime () {
            return ClientFunction(() => {
                const videoEl = document.querySelector('#bod-video-player_html5_api');
                return videoEl.currentTime;
            })();
        }
    }
    

    当我尝试移动到工厂页面时,它不起作用。

    我猜那是因为您只导出了 VideoPlayerPage 的一个实例,所以如果您只是复制并粘贴您的 ClientFunction 代码,那么它首先不会被导出,因此您无法在测试文件中使用它。

    编辑:

    如果您想将选择器作为参数发送给 ClientFunction,可以这样做:

    import { ClientFunction } from 'testcafe';
    
    class VideoPlayerPage {
        constructor () {
            this.videoSelector = '#bod-video-player_html5_api';
        }
    
        currentTime () {
            return ClientFunction(selector => {
                const videoEl = document.querySelector(selector);
                return videoEl.currentTime;
            })(this.videoSelector);
        }
    }
    

    【讨论】:

    • 请提供有关此建议的更多信息。我不擅长 javascript 对象,尝试做某事但失败了。
    • @hamona:好的,我更新了我的答案。但我认为你应该尝试学习它,它比从 SO 复制和粘贴代码要好得多。此外,如果某些事情不起作用,请始终分享更多详细信息,包括错误消息。我没有坐在你身边,所以我真的不知道你遇到了什么问题。
    • 嗨,我尝试了您的解决方案,但它失败了,因为该函数返回 [object Object],但应该是数字。 code m1 = [object Object] m2 = [object Object] ✖ Verify 'Play Simple workout' functionality in LO State 1) AssertionError: not bigger: the argument to above must be a number code 这是有错误的截图。 i.imgur.com/dDdIOEK.png
    • ClientFunction 返回一个 Promise,参见文档中的示例 (testcafe.io/documentation/402671/reference/test-api/…)。如果你想等到 Promise 解决,你必须等待它。
    • 好的,谢谢,一切正常。谢谢您的帮助。最后一个问题:)。我怎样才能在“document.querySelector”中插入构造函数变量。就在这里...'code videoEl = document.querySelector('#bod-video-player_html5_api');代码'
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    相关资源
    最近更新 更多