Skip to content

Commit ccfef9e

Browse files
GS added
1 parent c73bd3c commit ccfef9e

4 files changed

Lines changed: 171 additions & 3 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
layout: docs
3+
title: Getting Started
4+
permalink: /documentation/getting-started/
5+
---
6+
# Getting Started
7+
8+
This guide provides step-by-step instructions on how to create a functional web test with TestCafe and contains the following sections.
9+
10+
* [Installing TestCafe](#installing-testcafe)
11+
* [Creating a Test](#creating-a-test)
12+
* [Running the Test](#running-the-test)
13+
* [Viewing the Test Results](#viewing-the-test-results)
14+
* [Writing Test Code](#writing-test-code)
15+
* [Performing Actions on the Page](#performing-actions-on-the-page)
16+
* [Observing the Page State](#observing-the-page-state)
17+
* [Assertions](#assertions)
18+
19+
## Installing TestCafe
20+
21+
Make sure that [Node.js](https://nodejs.org/) and [npm](https://www.npmjs.com/) are installed on your computer, then run a single command:
22+
23+
```bash
24+
npm install -g testcafe
25+
```
26+
27+
## Creating a Test
28+
29+
To create a test, create a new .js file anywhere on your computer.
30+
This file must have the special structure: tests must be organized into fixtures. So, first you need to declare a fixture by using the [fixture](../test-api/test-code-structure.md#fixtures) function.
31+
32+
```js
33+
fixture `Getting Started`
34+
```
35+
36+
In this tutorial, you will create a test for the [http://testcafe.devexpress.com/example](http://testcafe.devexpress.com/example) sample page.
37+
Specify this page as a start page for the fixture by using the [page](../test-api/test-code-structure.md#specifying-the-start-webpage) function.
38+
39+
```js
40+
fixture `Getting Started`
41+
.page('http://testcafe.devexpress.com/example');
42+
```
43+
44+
Then create the [test](/test-api/test-code-structure.md#tests) function where you will further place test code.
45+
46+
```js
47+
fixture `Getting Started`
48+
.page('http://testcafe.devexpress.com/example');
49+
50+
test('Test1', async t => {
51+
// Test code
52+
});
53+
```
54+
55+
## Running the Test
56+
57+
You can simply run the test from a command shell, by calling a single command where you specify the [target browser](../using-testcafe/command-line-interface.md#browser-list) and [file path](../using-testcafe/command-line-interface.md#file-pathglob-pattern).
58+
59+
```bash
60+
testcafe safari test1.js
61+
```
62+
63+
TestCafe will automatically open the chosen browser and start the test execution within it.
64+
65+
For more information on how to configure the test run, see [Command Line Interface](../using-testcafe/command-line-interface.md).
66+
67+
## Viewing the Test Results
68+
69+
While the test is running, TestCafe is gathering information about the test run and outputing the report right into a command shell.
70+
71+
![Test Report](../../images/report.png)
72+
73+
For more information, see [Reporters](../using-testcafe/common-concepts/reporters.md).
74+
75+
## Writing Test Code
76+
77+
### Performing Actions on the Page
78+
79+
Every test should be capable of interacting with a page content. To perform user actions, TestCafe provides a number of [actions](../test-api/actions.md): `click`, `hover`, `typeText`, `setFilesToUpload`, etc.
80+
They can be called in a chain.
81+
82+
The following fixture contains a simple test that types a developer name into a text editor and then clicks the Submit button.
83+
84+
```js
85+
fixture `Getting Started`
86+
.page('http://testcafe.devexpress.com/example');
87+
88+
test('Test1', async t => {
89+
await t
90+
.typeText('#developer-name', 'John Smith')
91+
.click('#submit-button');
92+
});
93+
```
94+
95+
All test actions are implemented as async functions of the [test controller object](../test-api/test-code-structure.md#test-controller) `t`. This object is used to access test run API.
96+
To wait for actions to be complete, use the `await` keyword when calling these actions or action chains.
97+
98+
### Observing Page State
99+
100+
TestCafe allows you to observe the page state.
101+
For this purpose, it offers special kinds of functions that will execute your code on the client: [Selector](../test-api/executing-client-code/index.md#selector-functions) used to get direct access to DOM elements
102+
and [ClientFunction](test-api/executing-client-code/index.md#client-functions) used to obtain arbitrary data from the client side.
103+
You call these functions as regular async functions, that is you can obtain their results and use parameters to pass data to them.
104+
105+
For example, clicking the Submit button on the sample web page opens a "Thank you" page.
106+
To get access to DOM elements on the opened page, the `Selector` function can be used.
107+
The following example demonstrates how to access the article header element and obtain its actual text.
108+
109+
```js
110+
import { Selector } from 'testcafe';
111+
112+
// Declare the parameterized Selector function
113+
// to get access to a DOM element identified by the `id` attribute
114+
const getElementById = Selector(id => document.querySelector(`#${id}`));
115+
116+
fixture `Getting Started`
117+
.page('http://testcafe.devexpress.com/example');
118+
119+
test('Test1', async t => {
120+
await t
121+
.typeText('#developer-name', 'John Smith')
122+
.click('#submit-button');
123+
124+
// Use the Selector function to get access to the article header
125+
const articleHeader = await getElementById('article-header');
126+
127+
// Obtain the text of the article header
128+
const headerText = articleHeader.innerText;
129+
});
130+
```
131+
132+
For more information, see [Executing Client Code](../test-api/executing-client-code.md).
133+
134+
### Assertions
135+
136+
A functional test also should check the result of actions performed.
137+
For example, the article header on the "Thank you" page should address a user by the entered name.
138+
To check if the header is correct, you have to add an assertion to the test.
139+
140+
You can use assertions from Node's built-in [assert](https://nodejs.org/api/assert.html) module or from any third-party assertion library.
141+
Before calling assertions, make sure an assertion library is installed into your project.
142+
143+
The following test demonstrates how to use an assertion from [Chai Assertion Library](http://chaijs.com/api/bdd/).
144+
Before running the test, install the assertion library by calling the `npm install -g chai` command.
145+
146+
```js
147+
import { expect } from 'chai';
148+
import { Selector } from 'testcafe';
149+
150+
// Declare the parameterized hybrid function
151+
// to obtain text content of an element identified by the `id` attribute
152+
const getElementById = Selector(id => document.querySelector(`#${id}`));
153+
154+
fixture `Getting Started`
155+
.page('http://testcafe.devexpress.com/example');
156+
157+
test('Test1', async t => {
158+
await t
159+
.typeText('#developer-name', 'John Smith')
160+
.click('#submit-button');
161+
162+
// Use the Selector function to get access to the article header
163+
const articleHeader = await getElementById('article-header');
164+
165+
// Use the assertion to check if the actual header text is equal to the expected one
166+
expect(articleHeader.innerText).to.equal('Thank you, John Smith!');
167+
});
168+
```

docs/articles/documentation/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ inMainMenu: true
66
---
77
# Documentation
88

9-
* [Getting Started](.)
9+
* [Getting Started](getting-started/index.md)
1010
* [Test API](.)
1111
* [Using TestCafe](using-testcafe/index.md)
12-
* [Extending TestCafe](.)
12+
* [Extending TestCafe](extending-testcafe/index.md)
1313
* [Recipes](.)

docs/articles/images/report.png

28.7 KB
Loading

docs/nav/nav-menu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
- item: GETTING STARTED
2-
url: .
2+
url: /testcafe/documentation/getting-started/
33
- folder: TEST API
44
url: .
55
content:

0 commit comments

Comments
 (0)