Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Modern Web Testing with TestCafe

You're reading from   Modern Web Testing with TestCafe Get to grips with end-to-end web testing with TestCafe and JavaScript

Arrow left icon
Product type Paperback
Published in Sep 2020
Publisher Packt
ISBN-13 9781800200951
Length 168 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Dmytro Shpakovskyi Dmytro Shpakovskyi
Author Profile Icon Dmytro Shpakovskyi
Dmytro Shpakovskyi
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Chapter 1: Why TestCafe? 2. Chapter 2: Exploring TestCafe Under the Hood FREE CHAPTER 3. Chapter 3: Setting Up the Environment 4. Chapter 4: Building a Test Suite with TestCafe 5. Chapter 5: Improving the Tests 6. Chapter 6: Refactoring with PageObjects 7. Chapter 7: Findings from TestCafe 8. Other Books You May Enjoy

Executing custom client-side code

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:

  • Get elements from the web page for further actions. TestCafe allows you to create selectors based on client-side code that returns DOM nodes. You can write this code in the server-side test and TestCafe will run these functions in the browser when it needs to locate an element:
    const { Selector } = require('testcafe');const testElement = Selector(() => {    return document.querySelector('.test-class-name');});await t.click(testElement);
  • Obtain data from a client function that returns any serializable object from the client side (including any objects that can be converted to JSON). Unlike selectors, test code can access the object this client function returns. Usually, the data obtained from client functions is used to assert different page parameters. Here is an example of getting and verifying a page URL:
    const { ClientFunction } = require('testcafe');const getPageUrl = ClientFunction(() => {    return window.location.href;});await t.expect(getPageUrl).eql('https://test-site.com');
  • Inject custom code into the tested page. Injected scripts can then be used to add helper functions or to mock browser API:
    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.

You have been reading a chapter from
Modern Web Testing with TestCafe
Published in: Sep 2020
Publisher: Packt
ISBN-13: 9781800200951
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image