Back to Blog

Testing With TestCafe

Avanish Pandey

November 23, 2022

Testing With TestCafe

Testing with TestCafe is another elective motor for driving programs. It is operated by novel innovation, which gives quick and straightforward cross-program testing for work area and versatile programs. Not at all like WebDriver or Puppeteer, TestCafe doesn’t control a program by any stretch of the imagination. It’s anything but a program itself, similar to Nightmare or Cypress. TestCafe centre is an intermediary worker that runs behind the scene and changes all HTML and JS to incorporate code required for test robotization.

This is an intelligent thought. Be that as it may, to utilize TestCafe on a routine, you must comprehend its advantages and constraints:


Quick. The program is controlled from inside a website page. This makes trial inside a program as quickly as your program can deliver a page with no additional organization demands.

Basic Cross-Browser Support. Since TestCafe just dispatches programs, it can mechanize program on a work area or versatile. Dissimilar to WebDriver, you needn’t bother with an extraordinary adaptation of the program

and driver to get ready to run tests. Arrangement streamlined. All you need is only a program introduced, and you are all set.

Stable to Execution. Since a test is executed inside a program, the organization’s dormancy impacts are decreased. Not at all, like WebDriver, you won’t hit stale component special cases or components, not interactable exemptions, as from inside an internet browser, all DOM components are available.


Enchantment. Programs executed in TestCafe don’t know that they run in test mode. So at certain edges, mechanization control can be broken. It’s likewise tough to investigate potential issues, as you don’t have the foggiest idea how really a page is parsed to infuse computerization contents.

No Browser Control. Since TestCafe doesn’t control programs, you can’t computerize all clients’ activities. For example, TestCafe can’t open new tabs or open another program window in undercover mode. There can be additionally a few issues running tests on outsider workers or inside iframes.

Mimicked Events. Occasions like snap or DoubleClick are recreated by JavaScript inside. Inside WebDriver or Puppeteer, where those occasions are dispatched by a program called local occasions. Local occasions are nearer to the genuine client experience. Sometimes, recreated occasions wouldn’t speak to real client experience, prompting bogus positive outcomes. For example, a catch that can’t be genuinely clicked by a client would be interactive inside TestCafe.

Anyway, TestCafe is a decent choice to begin if you need cross-program testing. Furthermore, here is the motivation to utilize TestCafe with CodeceptJS: on the off chance that you hit an edge case or issue, you can, without much of a stretch, change your tests to WebDriver. As all aides in CodeceptJS share a similar language structure.

CodeceptJS is a rich testing system that additionally gives highlights missing in unique TestCafe:

Cucumber reconciliation Genuine Page Objects

Information Management through API furthermore, others

Writing Tests

To begin utilizing TestCafe with CodeceptJS, introduce both using npm.

On the off chance that you don’t have package.json in your task, make it with npm init – y.

npm I codeceptjs testcafe – spare dev

At that point, you have to instate a task, choosing TestCafe when inquired:

npx codeceptjs init

A first test ought to be made with codeceptjs gt order.

npx codeceptjs gt

In contrast to most of the opposite start to finish (e2e) testing instruments, TestCafe isn’t subject to Selenium or WebDriver. Instead, it infuses contents into the program to discuss legitimately with the DOM and handle occasions. It takes a shot at any advanced program that underpins HTML5 with no modules. Further, it bolsters all major working frameworks and can run while on numerous programs and machines.

Making a Test

TestCafe permits you to compose tests utilizing TypeScript or JavaScript (with its cutting edge highlights like async/anticipate). You get all the upsides of specific dialects like rich coding help, effortless versatility, registration you-type code check, etc. by utilizing TypeScript to compose your TestCafe tests. For more data about writing tests in TypeScript, see TypeScript Support.

To make a test, make another .js or .ts record. This document must have a unique structure – tests must be composed into installations.

Running the Test

You can run the test from an order shell by calling a solitary order to determine the objective program and document way.

testcafe chrome test1.js

TestCafe naturally opens the picked program and starts test execution inside it.

Make a point to keep the program tab that is running tests dynamically. Try not to limit the program window. Latent tabs and limited program windows change to a lower asset utilization mode where tests are not ensured to execute accurately.

Try not to zoom pages while testing. Tests might be unsteady if the page is zoomed in or out.

Composing Test Code

Performing Actions on the Page

Each test ought to be fit for associating with page content. To perform client activities, TestCafe gives various activities: click, float, typeText, setFilesToUpload, and so forth. They can be brought in a chain.

The accompanying installation contains a basic test that types a designer’s name into a content tool and afterward taps the Submit button.

Watching Page State

TestCafe permits you to watch the page state. For this reason, it offers extraordinary sorts of capacities that will execute your code on the customer: Selector used to get immediate admittance to DOM components, and ClientFunction used to acquire subjective information from the customer side. You call these capacities as customary async capacities; that is, you can obtain their outcomes and use boundaries to pass information.

The selector API gives strategies and properties to choose components on the page and get their state.

For instance, tapping the Submit button on the example site page opens a "Thank you" page. To gain admittance to DOM components on the opened page, the Selector capacity can be utilized. The accompanying model shows how to get to the article header component and get its real content.

Before TestCafe, start to finish web testing instruments that worked with genuine programs depended on driving these programs from an external perspective to copy client activities. This methodology

Reliance on outsider devices. This necessary you introduce, arrange, and keep up extra drivers, SDKs, and additional libraries in each test condition.

Restricted program upholds. The scope of accessible programs and stages was limited to those upheld by the driver.

Standard code. These testing apparatuses worked with a similar program occurrence during the whole trial, so the tried web application state (treats, stockpiles, and so forth.) endured from test to test. Subsequently, start to finish tests contain a lot of standard code that clears the state to avoid obstruction between tests.

Low computerization. Inheritance instruments couldn’t interface legitimately with the tried page. Although the way that the client’s test code didn’t meditate in the application code was a bit of leeway, the testing apparatus itself could profit by working inside the page. This would empower the device to robotize numerous standard undertakings.

TestCafe is Different

The possibility that characterized TestCafe engineering was that you don’t generally require an outer driver to run start to finish tests in the program. Instead, the page can run all the test contents that copy client activities.

This would consider genuine cross-program and cross-stage tests that a sudden spike in demand for any gadget that has an advanced program—this vision at the end formed into the accompanying plan ideas.

Customer worker engineering

TestCafe runs test code composed by the client in the Node.js condition on the "worker side." Contents that copy client movement (e.g., robotization contents) run inside the program’s tried page on the equivalent or an alternate gadget. This is the "customer side."

With this crossover approach, TestCafe tests consolidate points of interest of the program contents (worked in pausing, portable testing) and independent worker side code (admittance to the application’s worker and testing condition, better command over test execution).

Node.js API

Test code utilizes a smaller API that comprises only two or three dozen techniques that, in the end, change to client activities on the page. Since this worker side code runs in Node.js, tests should be written in JavaScript, or a language TestCafe can arrange to JavaScript (like TypeScript or CoffeeScript).

The succinct API makes tests simple to compose, read, and keep up. Backing for mainstream web dialects permits most clients to utilize their current aptitudes and limits the expectation to absorb information for new clients.

Page proxying

To give robotization contents (customer side code) to the program, TestCafe runs a converse intermediary in the engine. It intermediaries the tried page on the worker and infuses the contents into its code.

Content infusion empowers TestCafe key highlights like versatile testing, programmed pausing, or client functions for savvy validation out of the container.

Detached tests

Each test begins with a perfect program state. Treats and stockpiles are reset, an unfilled client profile is utilized, and the tried page is reloaded. This forestalls impedance between tests without additional standard code.

Customer Server Model

The customer worker configuration originates from our understanding. An early form of TestCafe infused clients’ test code straightforwardly into the page. This appeared to be the most effortless approach to break liberated from web drivers. Afterward, we understood that this design confined test code capacities, so we chose to decouple the test motor. We uncovered Node.js API for all test activities and actualized a test sprinter

that executes test contents in a worker cycle. TestCafe, on its end, creates computerization contents (customer code) and runs them in the program.

Why Server-Side Test Code

Node.js test code gives you more authority over the testing condition.

You can set up an information base or dispatch a web administration from the tests.

You can get to the worker’s document framework to peruse information tests or check the transferred records. Tests can utilize all Node.js highlights and outside Node.js modules.

Tests turned out to be quicker and more steady as test rationale is currently isolated from computerization


Test code can’t intrude on page execution because TestCafe doesn’t infuse client composed code ‘with no guarantees.’

Most recent linguistic structure highlights like async/anticipate are upheld.

Since Node.js code executes on the worker, it doesn’t have quick admittance to the DOM, program API, etc. We made up for this compromise with help for custom customer side capacities.

Testing With TestCafe: TestCafe API

Test code utilizes TestCafe API strategies to communicate with the tried page. Coming up next are three fundamental kinds of connection:

Perform activities on the page. TestCafe can imitate all end-client actions: click, double-tap, type, float, simplified, select content, transfer, etc. The programming interface gives a straightforward solitary technique for each activity.

Execute attestations. Attestations permit you to check component properties and other page boundaries. You can utilize conditions like ‘rises to’, ‘contains’, ‘more noteworthy’, ‘less’, ‘coordinate’, ‘kind of’, and so forth.

Select components. TestCafe utilizes a serious system to find target components for an activity or attestation. From the client’s perspective, this undertaking is as simple as indicating a standard CSS selector. You can, moreover chain techniques for more mind-boggling questions (like select ‘class name’ at that point continue to its next kin and take its third youngster). In the engine, TestCafe uses an amazing selector system with work in holding up adjusted to nonconcurrent page occasions.

Different capacities accessible in TestCafe API are demand ridiculing, client parts for speedy validation, exchange taking care of, troubleshooting, and then some.

Custom Client-Side Code

In cutting edge situations, TestCafe permits you to compose code to be executed on the tried page. You can utilize custom customer side code to play out the accompanying undertakings:

Distinguish page components. Indeed, even complex CSS selectors are not in every case incredible enough to distinguish a page component in a real web application. TestCafe permits you to make selectors dependent on customer side code that profits DOM components or hubs – you can compose this code directly inside the worker side test. This doesn’t bring live DOM objects to the Node.js condition. TestCafe will execute these capacities in the program when it needs to find a page component.

Get information. You can compose a customer work that profits any serializable item from the customer side (this incorporates all articles that can be changed over to JSON). Dissimilar to with selectors, test code can get to the report this customer work returns. You can utilize information acquired from customer capacities to check various boundaries of the page state.

Infuse contents. TestCafe additionally permits you to infuse custom code into the tried page. You can utilize the infused contents to include assistant capacities or fake program API.

Note that a best practice rule is to dodge page adjustments in custom customer side code. Activities that change DOM ought to be performed through worker side API simply because there’s no dependable method to achieve them physically without meddling with TestCafe inward components. The standard is to utilize customer side codes to investigate the page, find, and return data to the worker. 

Turn around Proxy

You may see that when you run TestCafe tests, the program’s location bar shows a URL that doesn’t have a place with your site. This is because TestCafe intermediaries the tried pages.

TestCafe switch intermediary (testcafe-hammerhead) runs locally on your PC. It empowers TestCafe to include mechanization contents such that neither the current page code nor the assets it speaks with can tell that the page has been adjusted.

When TestCafe intermediaries the tried page, it infuses computerization contents and modifies all URLs inside (in the markup, contents, etc.) to highlight the intermediary. At the point when the program alludes to these new URLs, the first assets are likewise proxied and changed similarly to let robotization proceed consistently. TestCafe likewise taunts the programming API to disguise robotization contents from the remainder of the page code.

The proxying instrument guarantees that the page has all the earmarks of being facilitated at the first URL, even to the test code. This is why you can utilize your site’s URLs in tests, and negligence the URL you find in the program’s location bar.

Computerization Scripts

TestCafe computerization contents imitate end-client activities on the tried page. They are intended to impersonate the conduct of various programs in everything about it. Computerization contents dispatch occasions, center components, or cycle qualities the way the program that runs them would do. They unique out program points of interest and empower you to compose cross-program elevated level test code.

Confined Test Runs

TestCafe confines each trial from ensuing tests and tests that run in equal. After a test is finished, TestCafe resets the program state:

erases treats

clears nearby and meeting stockpiles reloads the tried page

This spares you from composing code to reset the application state and deal with your tests’ progressions. Therefore, your tests are steady, simple to compose and peruse, and liberated from standard code.

On the off chance that you run a few tests in equal, each trial gets a free worker side setting. This forestalls worker side impacts also.


TestCafe is an independent start to finish testing arrangement that consolidates unrivaled convenience with cutting edge robotization and robust implicit security systems. TestCafe formally bolsters every significant program and stage, and it is intended to run in most present-day programs past authoritatively upheld.

Avanish Pandey

November 23, 2022


Subscribe to our Newsletter

Sign up to receive and connect to our newsletter

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Latest Article