【问题标题】:I cant get the expected URL with TestCafe我无法使用 TestCafe 获得预期的 URL
【发布时间】:2019-11-23 20:34:46
【问题描述】:

我想在用户登录主页后断言我在“主页”上。我创建了一个函数“onPage”来返回 URL 的包含内容。

代码如下:

function onPage (secondPage, URL) {
  if (secondPage === 'Home Page') {
    return await                
            testController.expect(URL.textContent).contains('URL-of-the-Home-Page');
  }
}


Then(THEN.THE_HOME_PAGE_IS_OPENED, async (secondPage) => {
  await testController.expect(onPage(secondPage, URL)).eql(true);
}

这是我的 BDD 场景:

  Scenario Outline: User is logged in the 
    Given the <namePage> is opened
      And the user populate <username>
      And the user populate <password>
     When the user clicks on Login button
     Then he is on <secondPage>

    Examples: 
      | namePage     | username | password | secondPage  | 
      | "Login Page" | "admin"  | "admin"  | "Home Page" |

【问题讨论】:

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


    【解决方案1】:

    看起来你正在使用一些基于 TestCafe 的 BDD 框架,所以我不知道如何用你的语法来做。但是,在 TestCafe 中,使用ClientFunctions 机制可以轻松解决此问题。

    请看以下代码:

    const getURL = ClientFunction(() => window.location.href);
    const myUrl = await getURL();
    

    然后您可以在断言中使用myUrl 值。


    更新:

    这是一个包含使用 TestCafe 语法的工作示例:

    import { ClientFunction } from 'testcafe';
     
    const URL = 'https://example.com/';
    const getURL = ClientFunction(() => window.location.href);
     
    fixture`My Fixture`
        .page(URL);
     
    test('Assert page URL', async t => {
        await t.expect(getURL()).eql(URL);
    });
    

    您需要实现类似的东西。例如,您可以使用错误文本中建议的方法:const myUrl = await getURL.with({ boundTestRun: testController })();

    【讨论】:

    • 我也试过这个,当我运行测试时,会显示以下错误:ClientFunction 无法在应该执行的上下文中隐式解析测试运行。如果您需要从 Node.js API 回调中调用 ClientFunction,请先通过 ClientFunction 的 .with({ boundTestRun: t }) 方法手动传递测试控制器。请注意,您不能在测试代码之外执行 ClientFunction。
    • 是的,问题解决了。非常感谢,Alex Kamaev。
    【解决方案2】:

    要在 assert 语句之外获取 url,你可以这样称呼它: let myUrl = await getURL.with({ boundTestRun: t })();

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 2014-07-04
      相关资源
      最近更新 更多