6.2.1 • Published 1 day ago

@applitools/eyes-testcafe v6.2.1

Weekly downloads
2,287
License
SEE LICENSE IN LI...
Repository
-
Last release
1 day ago

Eyes-Testcafe

Applitools Eyes SDK for Testcafe.

Installation

Install Eyes-Testcafe as a local dev dependency in your tested project:

npm i -D @applitools/eyes-testcafe

Applitools API key

In order to authenticate via the Applitools server, you need to supply the Eyes-Testcafe SDK with the API key you got from Applitools. Read more about how to obtain the API key here.

To to this, set the environment variable APPLITOOLS_API_KEY to the API key before running your tests. For example, on Linux/Mac:

export APPLITOOLS_API_KEY=<your_key>
npx testcafe chrome:headless tests

And on Windows:

set APPLITOOLS_API_KEY=<your_key>
npx testcafe chrome:headless tests

It's also possible to specify the API key in the applitools.config.js file. The property name is apiKey. For example:

module.exports = {
  apiKey: 'YOUR_API_KEY',
  ...
}

See the Advanced configuration section below for more information on using the config file.

Usage

After defining the API key, you will be able to use commands from Eyes-Testcafe in your testcafe tests to take screenshots and use Applitools Eyes to manage them:

Example

import Eyes from '@applitools/eyes';
const eyes = new Eyes();

fixture`Hello world`
  .page('https://applitools.com/helloworld')
  .afterEach(async () => eyes.close());
  .after(async () => eyes.waitForResults())
  
test('Cookies', async t => {
  await eyes.open({
      appName: 'Hello World!',
      testName: 'My first JavaScript test!',
      browser: [{ width: 800, height: 600, name: 'firefox' }],
      t
  });
  await eyes.checkWindow('Main Page');
  await t.click('button')
  await eyes.checkWindow('Click!');
});

API

open

Create an Applitools test. This will start a session with the Applitools server.

eyes.open({
  appName: '',
  testName: '',
  t
});

It's possible to pass a config object to open with all the possible configuration properties. Read the Advanced configuration section for a detailed description.

checkWindow

Generate a screenshot of the current page and add it to the Applitools Test.

eyes.checkWindow(tag)

OR

eyes.checkWindow({ tag: 'your tag', target: 'your target mode' })

Arguments to eyes.checkWindow

  • tag

    (optional): A logical name for this check.
  • target

    (optional): Possible values are:

    • window: This is the default value. Capture the entire window or only the viewport. If set then add fully as sibling to determine weather to capture full screen or viewport.
    • region: Take a screenshot of a region of the page, specified by coordinates or a selector. If set then add region or selector as siblings for specifying the region/s.
  • fully:

    (optional) In case target is window, determines wether to capture full page or viewport only. if true (default) then captures full page, if false then captures viewport.

        // capture viewport only
        eyes.checkWindow({
          target: 'window',
          fully: 'false',
        });
  • selector

    (optional) In case target is region, this should be the actual css or xpath selector to an element, and the screenshot would be the content of that element. For example:

        // The shorthand string version defaults to css selectors
        eyes.checkWindow({
          target: 'region',
          selector: '.my-element'
        });
    
        // Using a css selector
        eyes.checkWindow({
          target: 'region',
          selector: {
            type: 'css',
            selector: '.my-element' // or '//button'
          }
        });
    
        // Using an xpath selector
      eyes.checkWindow({
        target: 'region',
        selector: {
          type: 'xpath',
          selector: '//button[1]'
        }
      });
  • region

    (optional) In case target is region, this should be an object describing the region's coordinates. For example:

    eyes.checkWindow({
      target: 'region',
      region: {top: 100, left: 0, width: 1000, height: 200}
    });
  • ignore

    (optional): A single or an array of regions to ignore when checking for visual differences. For example:

    eyes.checkWindow({
      ignore: [
        {top: 100, left: 0, width: 1000, height: 100},
        {selector: '.some-div-to-ignore'}
      ]
    });
  • floating

    (optional): A single or an array of floating regions to ignore when checking for visual differences. More information about floating regions can be found in Applitools docs here. For example:

    eyes.checkWindow({
      floating: [
        {top: 100, left: 0, width: 1000, height: 100, maxUpOffset: 20, maxDownOffset: 20, maxLeftOffset: 20, maxRightOffset: 20},
        {selector: '.some-div-to-float', maxUpOffset: 20, maxDownOffset: 20, maxLeftOffset: 20, maxRightOffset: 20}
      ]
    });
  • layout

    (optional): A single or an array of regions to match as layout level. For example:

    eyes.checkWindow({
      layout: [
        {top: 100, left: 0, width: 1000, height: 100},
        {selector: '.some-div-to-test-as-layout'}
      ]
    });
  • strict

    (optional): A single or an array of regions to match as strict level. For example:

    eyes.checkWindow({
      strict: [
        {top: 100, left: 0, width: 1000, height: 100},
        {selector: '.some-div-to-test-as-strict'}
      ]
    });
  • scriptHooks

    (optional): A set of scripts to be run by the browser during the rendering. It is intended to be used as a means to alter the page's state and structure at the time of rendering. An object with the following properties: - beforeCaptureScreenshot: a script that runs after the page is loaded but before taking the screenshot. For example:

      ```js
      eyes.checkWindow({
        scriptHooks: {
          beforeCaptureScreenshot: "document.body.style.backgroundColor = 'gold'"
        }
      })
      ```
  • sendDom

    (optional): A flag to specify whether a capture of DOM and CSS should be taken when rendering the screenshot. The default value is true. This should only be modified to troubleshoot unexpected behavior, and not for normal production use.

    eyes.checkWindow({sendDom: false})

close

Close the applitools test and check that all screenshots are valid.

It is important to call this at the end of each test, symmetrically to open(or in afterEach(), see Best practice for using the SDK).

Close receives no arguments.

eyes.close();

waitForResults

Wait untill all tests in the fixture are completed and return their results. Note that if you don't wait for the tests to be completed then in case of a visual test failure, eyes cannot fail the fixture.

  • it is recommended to wait for the resulsts in the tescafe after() hook.
await eyes.waitForResults()

waitForResults receives rejectOnErrors,

  • If true (default) and a visual test fails then reject with an Error (in case of a general error reject as well). If the rejection is not handled then Testcafe fails the fixture.
  • If false and a visual test fails then waitForResults resolves with an Error. In case of a general Error reject with the Error.

In case all the tests passed then waitForResults resolves with the test results.


Best practice for using the SDK

Every call to eyes.open and eyes.close defines a test in Applitool Eyes, and all the calls to eyes.checkWindow between them are called "steps". In order to get a test structure in Applitools that corresponds to the test structure in Testcafe, it's best to open/close tests in every test call. You can use afterEach for calling eyes.close()

Also note that after all tests are done you should call eyes.waitForResults, you can use aftre() for calling eyes.waitForResults, this is is done for two reasons: 1. to signal testcafe to wait untill all the tests have been completed. 2. to obtain test results if needed.

fixture`Hello world`
  .page('https://applitools.com/helloworld')
  .afterEach(async () => eyes.close());
  .after(async () => eyes.waitForResults())

Applitools will take screenshots and perform the visual comparisons in the background. Performance of the tests will not be affected during the test run, but there will be a small phase at the end of the test run that waits for visual tests to end.


Concurrency

The default level of concurrency for free accounts is 1. This means that visual tests will not run in parallel during your tests, and will therefore be slow. If your account does support a higher level of concurrency, it's possible to pass a different value by specifying it in the property concurrency in the applitools.config.js file (see Advanced configuration section below).

If you are interested in speeding up your visual tests, contact sdr@applitools.com to get a trial account and faster tests with more concurrency.


Advanced configuration

There are 3 ways to specify test configuration: 1) Arguments to eyes.open() 2) Environment variables 3) The applitools.config.js file

The list above is also the order of precedence, which means that if you pass a property to eyes.open it will override the environment variable, and the environment variable will override the value defined in the applitools.config.js file.

Here are the available configuration properties:

Property nameDefault valueDescription
testNameundefinedThe test name
browser{ width: 800, height: 600, name: 'chrome' }The size and browser of the generated screenshots. This doesn't need to be the same as the browser that Testcafe is running. It could be a different size and also a different browser. Currently, firefox, chrome, edge, ie10 and ie11 are supported. For more info, see the browser section below.Note: for best performance if possible eyes resizes the viewport to the given browser size.
batchIdrandomProvides ability to group tests into batches. Read more about batches here.
batchNameThe name of the first test in the batchProvides a name to the batch (for display purpose only).
baselineEnvNameundefinedThe name of the environment of the baseline.
envNameundefinedA name for the environment in which the application under test is running.
ignoreCaretfalseWhether to ignore or the blinking caret or not when comparing images.
matchLevelStrictThe method to use when comparing two screenshots, which expresses the extent to which the two images are expected to match. Possible values are Strict, Exact, Layout and Content. Read more about match levels here.
baselineBranchNameundefinedThe name of the baseline branch.
parentBranchNameundefinedSets the branch under which new branches are created.
saveFailedTestsfalseSet whether or not failed tests are saved by default.
saveNewTestsfalseSet whether or not new tests are saved by default.
propertiesundefinedCustom properties for the eyes test. The format is an array of objects with name/value properties. For example: [{name: 'My prop', value:'My value'}].
compareWithParentBranchfalse
ignoreBaselinefalse

The following configuration properties cannot be defined using the first method of passing them to eyes.open. They should be defined either in the applitools.config.js file or as environment variables.

Property nameDefault valueDescription
apiKeyundefinedThe API key used for working with the Applitools Eyes server. See more info in the Applitools API key section above
showLogsfalseWhether or not you want to see logs. Logs are written to the same output of the Testcafe process. Note that you can also use DEBUG=eyes* for debugging.
serverUrlDefault Eyes server URLThe URL of Eyes server
proxyundefinedSets the proxy settings to be used in network requests to Eyes server. This can be either a string to the proxy URI, or an object containing the URI, username and password.For example:{uri: 'https://myproxy', username: 'my_user', password: 'my_password'}
isDisabledfalseIf true, all api calls to Eyes-Testcafe are ignored.
failTestcafeOnDifftrueIf true, then the Testcafe test fails if an eyes visual test fails. If false and an eyes test fails, then the Testcafe test does not fail.
tapDirPathundefinedDirectory path of a results file. If set, then a TAP file is created in this directory, the tap file name is created with the name eyes-\<ISO-DATE>.tap and contains the Eyes test results Note that results are scoped per spec file, this means that the results file is created once for each fixture file).
concurrency1The maximum number of tests that can run concurrently. The default value is the allowed amount for free accounts. For paid accounts, set this number to the quota set for your account.

Method 1: Arguments for eyes.open

Pass a config object as the only argument. For example:

eyes.open({
  appName: 'My app',
  batchName: 'My batch',
  ...
  // all other configuration variables apply
})

Method 2: Environment variables

The name of the corresponding environment variable is in uppercase, with the APPLITOOLS_ prefix, and separating underscores instead of camel case:

APPLITOOLS_APP_NAME
APPLITOOLS_SHOW_LOGS
APPLITOOLS_BATCH_NAME
APPLITOOLS_CONCURRENCY
APPLITOOLS_SAVE_DEBUG_DATA
APPLITOOLS_BATCH_ID
APPLITOOLS_BATCH_NAME
APPLITOOLS_BASELINE_ENV_NAME
APPLITOOLS_ENV_NAME
APPLITOOLS_IGNORE_CARET
APPLITOOLS_IS_DISABLED
APPLITOOLS_MATCH_LEVEL
APPLITOOLS_MATCH_TIMEOUT
APPLITOOLS_BRANCH_NAME
APPLITOOLS_BASELINE_BRANCH_NAME
APPLITOOLS_PARENT_BRANCH_NAME
APPLITOOLS_SAVE_FAILED_TESTS
APPLITOOLS_SAVE_NEW_TESTS
APPLITOOLS_COMPARE_WITH_PARENT_BRANCH
APPLITOOLS_IGNORE_BASELINE
APPLITOOLS_SERVER_URL

Method 3: The applitools.config.js file

It's possible to have a file called applitools.config.js at the same folder location as .testcaferc.json. (The directory from which you run TestCafe. This is usually the project's root directory). In this file specify the desired configuration, in a valid JSON format. For example:

module.exports = {
  appName: 'My app',
  showLogs: true,
  batchName: 'My batch'
  ...
  // all other configuration variables apply
}

Configuring the browser

Eyes-Testcafe will take a screenshot of the page in the requested browser, the browser can be set in the applitools.config.js or by passing it to eyes.open.

It's also possible to send an array of browsers, for example:

eyes.open({
  ...
  browser: [
    {width: 800, height: 600, name: 'firefox'},
    {width: 1024, height: 768, name: 'chrome'},
    {width: 1024, height: 768, name: 'ie11'}
  ]
}

Note: that if only a single browser is set, then Eyes-Testcafe changes the testcafe's browser viewport to that size.

Device emulation

To enable chrome's device emulation, it's possible to send a device name and screen orientation, for example:

eyes.open({
  ...
  browser: {
    deviceName: 'iPhone X',
    screenOrientation: 'landscape',
    name: 'chrome' // optional, just to make it explicit this is browser emulation and not a real device. Only chrome is supported for device emulation.
  }
}

Possible values for screen orientation are landscape and portrait, and if no value is specified, the default is portrait.

The list of device names is taken from chrome devtools predefined devices, and can be obtained by running the following command in a unix-based shell (installing jq might be needed):

curl -s https://raw.githubusercontent.com/chromium/chromium/0aee4434a4dba42a42abaea9bfbc0cd196a63bc1/third_party/blink/renderer/devtools/front_end/emulated_devices/module.json | jq '.extensions[].device.title'

In addition, it's possible to use chrome's device emulation with custom viewport sizes, pixel density and mobile mode, by passing deviceScaleFactor and mobile in addition to width and height. For example:

eyes.open({
  ...
  browser: {
    width: 800,
    height: 600,
    deviceScaleFactor: 3,
    mobile: true,
    name: 'chrome' // optional, just to make it explicit this is browser emulation and not a real device. Only chrome is supported for device emulation.
  }
}

Troubleshooting

  • If issues occur, DEBUG_SAVE=1 env variable can be set to save helpful information. The information will be saved under a folder named .debug in the current working directory. This could be then used for getting support on your issue.
  • You can also use DEBUG=eyes* for debugging.
6.2.1

1 day ago

6.2.0

16 days ago

6.1.1

1 month ago

6.0.3

1 month ago

6.0.0

2 months ago

5.0.0

3 months ago

4.1.7

3 months ago

4.1.6

4 months ago

4.1.5

4 months ago

4.1.4

5 months ago

4.1.3

5 months ago

4.1.2

5 months ago

4.1.1

5 months ago

4.0.4

6 months ago

4.1.0

5 months ago

4.0.3

6 months ago

4.0.2

6 months ago

2.0.0

7 months ago

4.0.1

7 months ago

4.0.0

7 months ago

3.0.0

7 months ago

1.17.4

2 years ago

1.16.2

2 years ago

1.17.2

2 years ago

1.17.1

2 years ago

1.17.0

2 years ago

1.17.3

2 years ago

1.16.1

2 years ago

1.16.0

2 years ago

1.15.4

2 years ago

1.15.3

2 years ago

1.15.2

2 years ago

1.15.1

2 years ago

1.15.5

2 years ago

1.15.0

3 years ago

1.14.3

3 years ago

1.14.2

3 years ago

1.14.1

3 years ago

1.14.0

3 years ago

1.13.1

3 years ago

1.13.0

3 years ago

1.12.8

3 years ago

1.12.7

3 years ago

1.12.6

3 years ago

1.12.5

3 years ago

1.12.4

3 years ago

1.12.3

3 years ago

1.12.2

3 years ago

1.12.1

3 years ago

1.12.0

3 years ago

1.11.1

4 years ago

1.11.0

4 years ago

1.10.0

4 years ago

1.9.2

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.4

4 years ago

2.0.0-beta.21

4 years ago

1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

2.0.0-beta.20

4 years ago

2.0.0-beta.19

4 years ago

1.6.0

4 years ago

1.5.14

4 years ago

2.0.0-beta.18

4 years ago

1.5.12

4 years ago

1.5.13

4 years ago

1.5.11

4 years ago

2.0.0-bata.16

4 years ago

1.5.9

4 years ago

2.0.0-beta.17

4 years ago

1.5.8

4 years ago

2.0.0-beta.15

4 years ago

2.0.0-beta.14

4 years ago

1.5.7

4 years ago

2.0.0-beta.13

4 years ago

1.5.6

4 years ago

1.5.5

4 years ago

2.0.0-beta.12

4 years ago

2.0.0-beta.11

4 years ago

1.5.4

4 years ago

2.0.0-beta.10

4 years ago

2.0.0-beta.9

4 years ago

2.0.0-beta.8

4 years ago

2.0.0-beta.7

4 years ago

2.0.0-beta.6

4 years ago

2.0.0-beta.5

4 years ago

2.0.0-beta.4

4 years ago

2.0.0-beta.3

4 years ago

2.0.0-beta.2

4 years ago

2.0.0-beta.1

4 years ago

1.0.0

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.3.1

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.1.29

5 years ago

1.1.28

5 years ago

1.1.27

5 years ago

1.1.26

5 years ago

1.1.25

5 years ago

1.1.24

5 years ago

1.1.23

5 years ago

1.1.22

5 years ago

1.1.21

5 years ago

1.1.20

5 years ago

1.1.19

5 years ago

1.1.18

5 years ago

1.1.17

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago