1.0.48 • Published 3 months ago

sefiutils v1.0.48

Weekly downloads
-
License
EPL-2.0
Repository
gitlab
Last release
3 months ago

npm pipeline status

logo sefiutils

Utilities (and Jest tests) for Selenium Webdriver Tests and some React tools as well

This package provides some utility functions for Selenium Webdriver. It also adds some utilities for React Fiber in order to make it easier to test React components -- however it has no dependency to React itself. Additionally setup and teardown functions for Jest are provided.

If you want to test your web app, you probably want to use other frameworks. This package is intended for use in teaching, in particular for grading student's projects. Thus it provides some utilities which are usually not available in other frameworks (as they are usually not required at all).

Install

Install with npm:

npm install --save-dev sefiutils

Usage

In your jest.config.js file add the following lines:

globalSetup: "<rootDir>/node_modules/sefiutils/lib/globalSetup.js",
globalTeardown: "<rootDir>/node_modules/sefiutils/lib/globalTeardown.js"
globalSetup: "<rootDir>/../node_modules/sefiutils/lib/globalSetup.js",
globalTeardown: "<rootDir>/../node_modules/sefiutils/lib/globalTeardown.js"

would not work. In this case, create your own global files and reexport the sefituils ones, e.g. Create a file 'globalSetup.js' in your project with the following content:

module.exports = require("sefiutils/lib/globalSetup.js");

Depending on the environment variable SEFI_BROWSER either a browser is started or a selenium server is started. The following values are supported:

  • remote: A selenium server is started. This requires Java as well! By default, selenium server 4.1.16 is used, but this can be changed via environment variable SEFI_SELENIUM_SERVER_VERSION. The jar file is automatically downloaded to ~/.cache/selenium-server if not already present.
  • chrome: A Chrome browser is started. This requires chromedriver (and @types/chromediver) to be installed.
  • firefox: A Firefox browser is started.

In your tests, add the following lines (or similar) to setup and teardown the driver and load the page:

import { LOCAL_SETUP_TIMEOUT, localSetup} from "sefiutils/lib/localSetup"
import { localTeardown} from "sefiutils/lib/localTeardown"

const page = "http://localhost:3000/" // or any other page
let driver: WebDriver;

beforeAll(async () => {
    try {
        driver = await localSetup(page, 600, 700);
    } catch (err) {
        throw new Error(`Cannot open page ${page}`);
    }
}, LOCAL_SETUP_TIMEOUT);

afterAll(async () => {
    await localTeardown(driver);
});

Utils for accessing information from the DOM via Selenium Webdriver injected JavaScript

Most utility functions are provided via module 'seUtils', e.g.

  • waitFor -- similar to React Testing Library's waitFor
  • saveScreenshot -- saves a screenshot to a file
  • getElementById, getElementByClass, getElementByTag -- returns the (first) element with the given id, class or tag
  • getElementContainingText -- returns the (first) element containing the given text
  • getBoundingClientRect, getBoundingClientRects -- returns the bounding rectangle(s) of the given element(s)
  • getComputedStyle -- returns the computed style of the given element
  • getInheritedBackgroundColorsByJS, getInheritedColorsByJS -- returns the inherited (background) color of the given element
  • getInnerSizeOfWindow, getViewPortSize -- returns the inner size of the window or the viewport size
  • setBrowserWindowSize -- sets the size of the browser window (this is directly supported by newer Selenium webdriver versions and only required for older versions)
  • waitForSetRect -- waits until the given element has the given bounding rectangle
  • setViewPortSize -- sets the size of the viewport
  • getButtonsAndHRefs -- returns all buttons and anchors with hrefs of the given element

Most of these functions are scripts executed in the browser via Selenium Webdriver's executeScript function.

Utils for React Fiber

If you want to test the React components hierarchy, you can use the following functions:

import { ComponentNode, dumpComponentTree, findAllInComponentTree, 
         findInComponentTree, getReactComponentTree} from "sefiutils/lib/fiberUtils"

await waitFor(async () => {
    cmpTree = await getReactComponentTree(driver);
    const component = findInComponentTree(cmpTree, (node) => equalsIgnoreCase(node.name, "App"));
    expect(component).toBeTruthy();
}, 5000, 1000);

For debugging, you can also dump the tree;

console.log("Component Tree:\n" + dumpComponentTree(cmpTree));

Note that in production build, webpack (i.e, terser) replaces all class and function names. Since the component names are simply the function names, they are replaced as well.

In order to keep the names, you must use a trick described by François Zaninotto in his blog. The trick there is a bit outdated, so this is how it works now:

  1. Install rewire: npm install rewire
  2. Write a small script, e.g. build.js as follows:
    const rewire = require('rewire');
    const defaults = rewire('react-scripts/scripts/build.js');
    const config = defaults.__get__('config');
    // console.log(JSON.stringify(config, null, 2));
    config.optimization.minimizer[0].options.minimizer.options.keep_classnames = true;
    config.optimization.minimizer[0].options.minimizer.options.keep_fnames = true;
  3. Instead of calling npm run build, just call node build.js. Of course you can also use that as your new build script.

If you get an error or the trick does not work, uncomment the log line to see the actual structure of your config.

Logging etc.

In order to see what's going on, set environment variable SEFI_VERBOSE to true.

If you have problems starting the driver or something like that, set the environment variable SEFI_DEBUG to true. This will output information about local start up process.

Docker Hints

If you want to run your tests in a docker container on macos with M1 or M2 processor, you can use the following Dockerfile:

FROM seleniarm/standalone-firefox:latest

# install Node 18 and NPM
RUN sudo apt update
RUN sudo apt install nodejs npm -y

CMD ["/bin/bash"]

The default user ist "seluser", i.e. home folder is found in "/home/seluser".

Troubleshooting and configuration

Several environment variables can be used to configure SEFIUTILS.

  • SEFI_BROWSER, at the moment only remvote is supported, default remote
  • SEFI_SELENIUM_SERVER_VERSION, the selenium server version, default 4.16.1
  • SEFI_LOG_LEVEL, the log level, can be log, verbose or debug, default log
  • SEFI_SELENIUM_REMOTE_URL, the URL, only the port is really configurable, default http://localhost:4444/wd/hub
  • SEFI_SELENIUM_SERVER_START_TIMEOUT, timeout when starting the remote selenium server, default 5000
  • SEFI_SELENIUM_SERVER_DOWNLOAD_URL, URL (prefix) from where the selenium server jar will be downloaded, default https://github.com/SeleniumHQ/selenium/releases/download

The environment is also passed to Selenium, so specific Selenium configurations can be set as well.

If the selenium server cannot be started, you might check whether any server is listing at the port via

lsof -i :4444 -t

(and you might kill it via kill -15 $(lsof -i :4444 -t))

License

This program and the accompanying materials are made available under the terms of the Eclipse Public License v. 2.0 which is available at https://www.eclipse.org/legal/epl-2.0.

1.0.48

3 months ago

1.0.47

3 months ago

1.0.46

3 months ago

1.0.45

3 months ago

1.0.44

3 months ago

1.0.43

3 months ago

1.0.42

4 months ago

1.0.41

4 months ago

1.0.40

4 months ago

1.0.39

4 months ago

1.0.38

4 months ago

1.0.37

4 months ago

1.0.36

4 months ago

1.0.35

4 months ago

1.0.34

5 months ago

1.0.29

5 months ago

1.0.33

5 months ago

1.0.32

5 months ago

1.0.31

5 months ago

1.0.30

5 months ago

1.0.28

8 months ago

1.0.27

8 months ago

1.0.26

8 months ago

1.0.25

8 months ago

1.0.24

8 months ago

1.0.23

9 months ago

1.0.22

9 months ago

1.0.21

9 months ago

1.0.20

9 months ago

1.0.19

9 months ago

1.0.18

9 months ago

1.0.17

9 months ago

1.0.16

9 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.13

9 months ago

1.0.12

9 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago