kiwigrid / gherkin-testcafe

Run testcafe tests with gherkin syntax
MIT License
68 stars 2 forks source link

Not able to verify the URL with "getLocation" #38

Closed dthisner closed 5 years ago

dthisner commented 5 years ago

Hello,

Thanks again for this amazing plugin :)

I am stuck, I am trying to verify that the URL is correct and I tried require "ClientFunction" with no luck.

This is what I got:

Then(/^the URL should end with "(.+)"$/, async (t, [url]) => {
  const ClientFunction = require('testcafe');
  const getLocation = ClientFunction(() => window.location.href);

  await t.expect(getLocation()).contains(url);
});

I am getting following error:

1<process.argv.length?process.argv[1].replace(/\\/g,"/"):"unknown-program");b.arguments=process.argv.slice(2);"undefined"!==typeof module&&(module.exports=b);process.on("uncaughtException",function(a){if(!(a instanceof y))throw a;});b.inspect=function(){return"[Emscripten Module object]"}}else if(x)b.print||(b.print=print),"undefined"!=typeof printErr&&(b.printErr=printErr),b.read="undefined"!=typeof read?read:function(){throw"no read() available (jsc?)";},b.readBinary=function(a){if("function"===
                                                                                                                                                                                                                              ^

ReferenceError: window is not defined
    at Server.ClientFunction (/Users/dennis/Projects/src/comp.co/project/project/frontend/src/tests/e2e/stepDefinitions/grain/grain.js:35:60)
    at Object.onceWrapper (events.js:273:13)
    at Server.emit (events.js:187:15)
    at Server.EventEmitter.emit (domain.js:441:20)
    at emitListeningNT (net.js:1324:10)
    at process._tickCallback (internal/process/next_tick.js:63:19)

What am I doing wrong?

I also tried: document.location.href

Then I am getting:

1<process.argv.length?process.argv[1].replace(/\\/g,"/"):"unknown-program");b.arguments=process.argv.slice(2);"undefined"!==typeof module&&(module.exports=b);process.on("uncaughtException",function(a){if(!(a instanceof y))throw a;});b.inspect=function(){return"[Emscripten Module object]"}}else if(x)b.print||(b.print=print),"undefined"!=typeof printErr&&(b.printErr=printErr),b.read="undefined"!=typeof read?read:function(){throw"no read() available (jsc?)";},b.readBinary=function(a){if("function"===
                                                                                                                                                                                                                              ^

ReferenceError: document is not defined
    at Server.ClientFunction (/Users/dennis/Projects/src/comp.co/project/project/frontend/src/tests/e2e/stepDefinitions/grain.js:35:62)
    at Object.onceWrapper (events.js:273:13)
    at Server.emit (events.js:187:15)
    at Server.EventEmitter.emit (domain.js:441:20)
    at emitListeningNT (net.js:1324:10)
    at process._tickCallback (internal/process/next_tick.js:63:19)
eddiegroves commented 5 years ago

Try using .with({ boundTestRun: t })

https://devexpress.github.io/testcafe/documentation/test-api/obtaining-data-from-the-client/#optionsboundtestrun

e.g

  const getLocation = ClientFunction(() => window.location.href);
  await t.expect(getLocation.with({ boundTestRun: t })()).contains(url);
dthisner commented 5 years ago

Thanks, I will give it a shot :)

dthisner commented 5 years ago

@eddiegroves Thanks for your answer, I am getting:

ReferenceError: window is not defined
    at Server.ClientFunction (/Users/dennis/Projects/src/applydigital.co/website-agi/AgiWebsite/frontend/src/tests/e2e/stepDefinitions/brands/westeel.js:79:60)
    at Object.onceWrapper (events.js:273:13)
    at Server.emit (events.js:187:15)
    at Server.EventEmitter.emit (domain.js:441:20)
    at emitListeningNT (net.js:1324:10)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Code:

Then(/^I can see that the URL contains "(.+)"$/, async (t, [URL]) => {
  const ClientFunction = require('testcafe');
  const getLocation = ClientFunction(() => window.location.href);

  await t.expect(getLocation.with({ boundTestRun: t })()).contains(URL);
});
eddiegroves commented 5 years ago

@dthisner

This example should work - ClientFunction needs to be imported correctly:

const { Given, Then } = require('cucumber');
const { ClientFunction } = require('testcafe');

Given("I open Google's search page", async t => {
  await t.navigateTo('https://www.google.com');
});

Then(/^I can see that the URL contains "(.+)"$/, async (t, [url]) => {
  const getLocation = ClientFunction(() => window.location.href);
  await t.expect(getLocation.with({ boundTestRun: t })()).contains(url);
});
Feature: The big search feature

  I want to open Google

  Scenario: Open Google
    Given I open Google's search page
    Then I can see that the URL contains "google.com"
dthisner commented 5 years ago

works like a charm! thank you :)