-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Modern Web Testing with TestCafe
By :

With TestCafe, you can create client functions that can run on the client side (in the browser) and return any serializable value. For example, you can obtain the URL of the current page, set cookies, or even manipulate any elements on the page.
In some complex scenarios, TestCafe helps you write code to be executed on the tested page. Here are several examples of tasks that can be done with custom client-side code:
const { Selector } = require('testcafe');const testElement = Selector(() => { return document.querySelector('.test-class-name');});await t.click(testElement);
const { ClientFunction } = require('testcafe');const getPageUrl = ClientFunction(() => { return window.location.href;});await t.expect(getPageUrl).eql('https://test-site.com');
fixture('My second test Fixture') .page('https://test-site.com') .clientScripts( 'assets/jquery-latest.js', 'scripts/location-mock.js' );
Note
It is recommended that you avoid changing the DOM with custom client-side code. A rule of thumb is to use client-side code only to explore the page, find and return information to the server.
You can find more examples of client-side scripts and injections at the following links:
As we just discovered, TestCafe client functions are quite useful for different browser manipulations and getting additional data to verify in our tests.