Show List
Assertions
Here's a sample TestCafe test script that demonstrates performing various assertions:
import { Selector } from 'testcafe';
fixture('Assertions Test')
.page('https://www.example.com');
test('Perform Various Assertions', async t => {
// Selector for elements on the page
const title = Selector('title');
const header = Selector('h1');
const paragraph = Selector('p');
const link = Selector('a');
// Perform assertions
await t
// Assert that the title of the page is as expected
.expect(title.innerText).eql('Example Domain')
// Assert that the header text contains specific text
.expect(header.innerText).contains('Welcome')
// Assert that the paragraph text matches a regular expression
.expect(paragraph.innerText).match(/lorem ipsum/)
// Assert that the link exists and has a specific attribute value
.expect(link.exists).ok()
.expect(link.getAttribute('href')).eql('https://www.example.com/link');
// Perform custom assertions
const elementCount = await Selector('div').count;
await t.expect(elementCount).gte(5);
});
In this test:
- We import the
Selector
function from TestCafe to select elements on the web page. - We define a fixture named 'Assertions Test' and set the page to 'https://www.example.com'.
- Inside the test function:
- We define selectors for various elements on the page (
title
,header
,paragraph
,link
). - We perform several assertions using TestCafe's
expect
function:- Assert that the title of the page is as expected.
- Assert that the header text contains specific text.
- Assert that the paragraph text matches a regular expression.
- Assert that a link exists and has a specific attribute value.
- We also perform a custom assertion to check the count of
<div>
elements on the page.- We use
Selector('div').count
to get the count of<div>
elements. - We use TestCafe's
expect
function to assert that the count is greater than or equal to 5.
- We use
- We define selectors for various elements on the page (
You can adjust the selectors and assertions according to the structure and content of the web page you are testing.
Leave a Comment