Testcafe: "window is not defined" exception when trying to access window object

Created on 8 May 2017  路  3Comments  路  Source: DevExpress/testcafe

I'm writing my first test and trying to access some global variables from window object.
window.top["myobj"] throws "window is not defined" exception
await Selector("window")()returns null.

What's correct way to access window object?

  • operating system: Windows 10
  • testcafe version: 0.15.0
  • node.js version: 7.8.0
Auto-locked question

All 3 comments

Window is not a HTML node, therefore then you pass window as a CSS selector it searches for elements with tag name window (the same way as e.g. document.querySelector does). What you need is t.eval that allows you evaluate arbitrary code on client side and obtain its results:

test('Some test', async t => 啸
     const prop = await t.eval(() => window.top["myobj"]);
});

In case you need a reusable solution, you can stick with ClientFunction:

import { ClientFunction } from 'testcafe';

const getWindowTopProp = ClientFunction(prop => window.top[prop]);

fixture `Some fixture`;

test('Some test', async () => {
    const prop = await getWindowTopProp('myobj');
});

Any additional information can be found in docs.

Is there a way to set the prop value on the fixture itself? For example, I would like to use a value from the client side multiple times in some tests. Possible to keep it in a fixture hook to share this variable with the tests?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or feature requests. For TestCafe API, usage and configuration inquiries, we recommend asking them on StackOverflow.

Was this page helpful?
0 / 5 - 0 ratings