Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cant get the expected URL with TestCafe

Tags:

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" |
like image 720
AlexMos Avatar asked Jul 15 '19 07:07

AlexMos


1 Answers

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

like image 82
Alex Kamaev Avatar answered Oct 01 '22 23:10

Alex Kamaev