5

I want to assert that I am on the "Home Page" after the user is logged in the Home Page. I made a function 'onPage' to return me the contains of the URL.

Here is the code:

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);
}

And here is my BDD scenario:

  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" |
Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
AlexMos
  • 81
  • 1
  • 5

2 Answers2

4

It looks like you are using some TestCafe-based BDD framework, so I do not precisely know how to do it with your syntax. However, in TestCafe this issue can be easily solved using the ClientFunctions mechanism.

Please see the following code:

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

Then you can use the myUrl value in your assertions.


UPDATE:

Here is a working example which includes the use of TestCafe syntax: 

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);
});

You'll need to implement something similar. For example, you can use the approach suggested in the error text: const myUrl = await getURL.with({ boundTestRun: testController })();

Arseniy Rubtsov
  • 880
  • 4
  • 10
Alex Kamaev
  • 6,198
  • 1
  • 14
  • 29
  • I also tried this and when i run the test, the following error is displayed: ClientFunction cannot implicitly resolve the test run in context of which it should be executed. If you need to call ClientFunction from the Node.js API callback, pass the test controller manually via ClientFunction's `.with({ boundTestRun: t })` method first. Note that you cannot execute ClientFunction outside the test code. – AlexMos Jul 15 '19 at 08:45
  • 1
    Yes, the problem is solved. Thank you a lot, Alex Kamaev. – AlexMos Jul 16 '19 at 11:41
0

to get the url outside assert statement, you can call it like: let myUrl = await getURL.with({ boundTestRun: t })();

hasan.in
  • 559
  • 6
  • 16