|
| 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 | +You can optionally specify a start page URL for the fixture by using the [page](../test-api/test-code-structure.md#specifying-the-start-webpage) function. |
| 33 | +In this tutorial, you will create a test for the [http://testcafe.devexpress.com/example](http://testcafe.devexpress.com/example) sample page. |
| 34 | + |
| 35 | +Then create the [test](/test-api/test-code-structure.md#tests) function where you will further place test code. |
| 36 | + |
| 37 | +```js |
| 38 | +fixture `Getting Started` |
| 39 | + .page('http://testcafe.devexpress.com/example'); |
| 40 | + |
| 41 | +test('Test1', async t => { |
| 42 | + // Test code |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +## Running the Test |
| 47 | + |
| 48 | +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). |
| 49 | + |
| 50 | +```bash |
| 51 | +testcafe safari test1.js |
| 52 | +``` |
| 53 | + |
| 54 | +TestCafe will automatically open the chosen browser and start the test execution within it. |
| 55 | + |
| 56 | +For more information on how to configure the test run, see [Command Line Interface](../using-testcafe/command-line-interface.md). |
| 57 | + |
| 58 | +## Viewing the Test Results |
| 59 | + |
| 60 | +While the test is running, TestCafe is gathering information about the test run and outputing the report right into a command shell. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +For more information, see [Reporters](../using-testcafe/common-concepts/reporters.md). |
| 65 | + |
| 66 | +## Writing Test Code |
| 67 | + |
| 68 | +### Performing Actions on the Page |
| 69 | + |
| 70 | +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. |
| 71 | +They can be called in a chain. |
| 72 | + |
| 73 | +The following fixture contains a simple test that types a developer name into a text editor and then clicks the Submit button. |
| 74 | + |
| 75 | +```js |
| 76 | +fixture `Getting Started` |
| 77 | + .page('http://testcafe.devexpress.com/example'); |
| 78 | + |
| 79 | +test('Test1', async t => { |
| 80 | + await t |
| 81 | + .typeText('#developer-name', 'John Smith') |
| 82 | + .click('#submit-button'); |
| 83 | +}); |
| 84 | +``` |
| 85 | + |
| 86 | +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. |
| 87 | +To wait for actions to be complete, use the `await` keyword when calling these actions or action chains. |
| 88 | + |
| 89 | +### Observing Page State |
| 90 | + |
| 91 | +TestCafe allows you to observe the page state. |
| 92 | +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 |
| 93 | +and [ClientFunction](test-api/executing-client-code/index.md#client-functions) used to obtain arbitrary data from the client side. |
| 94 | +You call these functions as regular async functions, that is you can obtain their results and use parameters to pass data to them. |
| 95 | + |
| 96 | +For example, clicking the Submit button on the sample web page opens a "Thank you" page. |
| 97 | +To get access to DOM elements on the opened page, the `Selector` function can be used. |
| 98 | +The following example demonstrates how to access the article header element and obtain its actual text. |
| 99 | + |
| 100 | +```js |
| 101 | +import { Selector } from 'testcafe'; |
| 102 | + |
| 103 | +// Declare the parameterized Selector function |
| 104 | +// to get access to a DOM element identified by the `id` attribute |
| 105 | +const getElementById = Selector(id => document.querySelector(`#${id}`)); |
| 106 | + |
| 107 | +fixture `Getting Started` |
| 108 | + .page('http://testcafe.devexpress.com/example'); |
| 109 | + |
| 110 | +test('Test1', async t => { |
| 111 | + await t |
| 112 | + .typeText('#developer-name', 'John Smith') |
| 113 | + .click('#submit-button'); |
| 114 | + |
| 115 | + // Use the Selector function to get access to the article header |
| 116 | + const articleHeader = await getElementById('article-header'); |
| 117 | + |
| 118 | + // Obtain the text of the article header |
| 119 | + const headerText = articleHeader.textContent; |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +For more information, see [Executing Client Code](../test-api/executing-client-code.md). |
| 124 | + |
| 125 | +### Assertions |
| 126 | + |
| 127 | +A functional test also should check the result of actions performed. |
| 128 | +For example, the article header on the "Thank you" page should address a user by the entered name. |
| 129 | +To check if the header is correct, you have to add an assertion to the test. |
| 130 | + |
| 131 | +You can use assertions from Node's built-in [assert](https://nodejs.org/api/assert.html) module or from any third-party assertion library. |
| 132 | +Before calling assertions, make sure an assertion library is installed into your project. |
| 133 | + |
| 134 | +The following test demonstrates how to use an assertion from [Chai Assertion Library](http://chaijs.com/api/bdd/). |
| 135 | +Before running the test, install the assertion library by calling the `npm install -g chai` command. |
| 136 | + |
| 137 | +```js |
| 138 | +import { expect } from 'chai'; |
| 139 | +import { Selector } from 'testcafe'; |
| 140 | + |
| 141 | +// Declare the parameterized hybrid function |
| 142 | +// to obtain text content of an element identified by the `id` attribute |
| 143 | +const getElementById = Selector((id) => document.querySelector(`#${id}`)); |
| 144 | + |
| 145 | +fixture `Getting Started` |
| 146 | + .page('http://testcafe.devexpress.com/example'); |
| 147 | + |
| 148 | +test('Test1', async t => { |
| 149 | + await t |
| 150 | + .typeText('#developer-name', 'John Smith') |
| 151 | + .click('#submit-button'); |
| 152 | + |
| 153 | + // Use the Selector function to get access to the article header |
| 154 | + const articleHeader = await getElementById('article-header'); |
| 155 | + |
| 156 | + // Use the assertion to check if the actual header text is equal to the expected one |
| 157 | + expect(articleHeader.displayedText).to.equal('Thank you, John Smith!'); |
| 158 | +}); |
| 159 | +``` |
0 commit comments