t.expect Method
Main article: Assertions
The t.expect
method declares a new assertion. It accepts a single argument that define’s the assertion’s first operand. To complete the assertion, chain an assertion method to the t.expect
declaration.
The t.expect
method accepts variables, strings, and arrays:
await t.expect(x).contains('Shamala Hamala');
In addition to static values, you can place one of the following functions into the left-hand side of the assertion. These functions become subject to Smart Assertion Query Mechanism.
-
const getLocationPart = ClientFunction(locationPart => { return window.location[locationPart]; }); await t.expect(getLocationPart('host')).eql('devexpress.github.io');
Selector queries:
await t.expect(Selector('#article-header').innerText).contains('10 Best Vacation Spots');
Two RequestLogger methods — count and contains:
import { RequestLogger } from 'testcafe'; const logger = RequestLogger(); fixture`RequestLogger` .page('https://devexpress.github.io/testcafe/example/') .requestHooks(logger); test('Check request', async t => { await t.expect(logger.contains(record => record.response.statusCode === 200)).ok(); });
HTTP Response properties:
const responseBody = t.request(`http://localhost:3000/helloworld`).body; await t.expect(responseBody).contains('Hello World');
Important
Your operands’ data types depends on the assertion method. For example, assertion methods that compare two numeric values require integer operands.
Note
TestCafe awaits Promises from compatible asynchronous functions. If your assertion includes a custom function that returns a Promise, the assertion fails.
When you create assertions, avoid the use of custom functions that return a Promise. If you can’t work around this limitation, use the allowUnawaitedPromise
option:
await t
.expect(new Promise(resolve => setTimeout(resolve, 100)))
.ok('received a promise', { allowUnawaitedPromise: true });
Example:
import { Selector } from 'testcafe';
fixture`TestController.expect`
.page`https://devexpress.github.io/testcafe/example/`;
test('Check property of element', async t => {
const developerNameInput = Selector('#developer-name');
await t
.expect(developerNameInput.value).eql('', 'input is empty')
.typeText(developerNameInput, 'Peter Parker')
.expect(developerNameInput.value).contains('Peter', 'input contains text "Peter"');
});