我无法通过TestCafe获得预期的URL

时间:2019-07-15 07:01:49

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

我要断言在用户登录主页后,我位于“主页”上。我做了一个函数“ 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" |

2 个答案:

答案 0 :(得分:3)

您似乎正在使用一些基于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 })();

答案 1 :(得分:0)

要在assert语句之外获取url,可以这样调用: FROM rust AS build WORKDIR /usr/src RUN rustup target add x86_64-unknown-linux-musl RUN apt-get update && apt-get upgrade -y && apt-get install -y build-essential git clang llvm-dev libclang-dev libssl-dev pkg-config libpq-dev musl-tools brotli RUN USER=root cargo new loxe-api WORKDIR /usr/src/loxe-api ENV PKG_CONFIG_ALLOW_CROSS=1 ENV OPENSSL_INCLUDE_DIR="/usr/include/openssl" ENV RUSTFLAGS="-C target-feature=+crt-static" RUN cargo install --target x86_64-unknown-linux-musl --path . FROM debian COPY --from=build /usr/local/cargo/bin/loxe-api . USER 1000 CMD ["./loxe-api"]