How to use fixtures in testcafe

Fixtures are really important to manage the testcases or divide the testcases into categories. In this tutorial, will see examples to use fixtures in testcafe.

What are fixtures in testcafe

Fixtures are used to divide the list of testcases or bulk testcases into categories. It will help to manage the testcases module-wise like login, registration, Data uploading, etc. it depends on the automation requirements.

Simply, we can define fixture "fixture_name"it in the test.js file.

How to use fixtures in testcafe

To use fixtures in testcafe, Simply write the fixture function with the name of categories. in the below code, “loginpage” is a fixture

fixture `LoginPage`
test('My first test case', async t => {
    // Test code
});

Can we write multiple tests in a testcafe fixture?

Yes, We can write multiple testcase scripts in a single fixture. there is no limit.

fixture `Date_Picker_Testcases`
    .page `https://www.seleniumeasy.com/test/bootstrap-date-picker-demo.html`;

test('My first test for Home Page', async t => {
    // Test code
});

test('My first test case', async t => {
    // Test code
});

Can we write multiple fixtures in a testcafe test file?

Yes, We can write multiple fixtures in one test.js file. below is the example.

fixture `LoginPage`
test('My first test case', async t => {
    // Test code
});

fixture `Date_Picker_Testcases`
    .page `https://www.seleniumeasy.com/test/bootstrap-date-picker-demo.html`;

test('My first test for Home Page', async t => {
    // Test code
});


fixture `From Page Testcases`
    .page `https://www.seleniumeasy.com/test/basic-first-form-demo.html`;

test('My first test for Input', async t => {
    // Test code
});

How to run tests for a specific fixture?

Let’s assume we have multiple fixtures in a single test.js  file and we need to run only testcases of the single fixture then you just need to specify the name of the fixture with the run command. testcafe chrome test.js -f "Date_Picker_Testcases"

testcafe chrome test.js -f "Date_Picker_Testcases"

How to open the page with a testcafe fixture?

.page function is used to open a webpage in testcafe. To open a webpage with testcafe fixture just write the code

.page `URL Name`;

Note: To define the fixture name use backtick ` ` that is below the ESC button of your keyboard.