Block reference / General

JavaScript code

Run your own JavaScript in the workflow, to work on variables and the table between phone blocks.

Only inWarpX Phone

WarpX Phone's JavaScript code runs a script in the workflow itself, not on the phone and not in a page: a phone has no page. Use it to count, pick, reshape and decide — build the text a Type text block types, choose one of several replies at random, or turn what a Read element block read into a number. It reaches the phone only through the blocks around it. It is WarpX Browser's JavaScript code block fixed to running in the background.

The script picked one review at random into {{variables.review}}; the Type text block after it types that review into the field.
The JavaScript code block's settings, filled in as in the example below.

Settings

Timeout (milliseconds)

How long the code may run before the block fails, saying the code did not finish in time. Code that waits longer can call resetTimeout() to start the count again. The default is 20000.

Example20000
JavaScript code

Click the preview to edit. A new block starts with a comment and nextBlock();. Available: refData(keyword, path) reads variables, table, loopData, prevBlockData or globalData; setVariable(name, value); nextBlock(data, insert); resetTimeout(); fetchData(type, resource). Each also exists with an automa prefix, such as automaNextBlock.

Example
const replies = [
  'Great quality, fast delivery!',
  'Exactly as described, thank you.',
  'Second time buying, still happy.',
];
const reply = replies[Math.floor(Math.random() * replies.length)];
setVariable('review', reply);
nextBlock({ review: reply });
Preload script

In the code window's Preload script tab. Scripts fetched from an http or https address and run before your code, such as a date library. They are taken away again once the block finishes.

Examplehttps://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js
Delay (ms)

A pause after the block finishes, before the next block starts. Write one number such as 1000, or a range such as 1000,3000 to pick a random pause in that range on each run.

Example0

Every block also has the shared block settings — a description, Disable block and what to do on an error. Fields that take text accept data from earlier blocks.

What it hands on

Whatever you pass to nextBlock(data): an object whose keys are table column names adds one row, and a list of such objects adds several. nextBlock(data, false) passes the data on without writing it to the table. Variables set with setVariable are readable afterwards as {{variables.name}}. Leaves by its single output; an error the code throws makes it fail with that message.

Example: Leave a different review on each phone

  1. Start
  2. Start app
  3. Element action
  4. JavaScript code
  5. Type text
  6. Element action
Where JavaScript code sits in the example workflow.
  1. Open the order's review screen with Start app and Element action blocks.
  2. Add a JavaScript code block and click its preview.
  3. Enter code that picks one reply from a list at random, calls setVariable('review', reply) and ends with nextBlock({ review: reply }).
  4. Add a Type text block that types {{variables.review}} into the review field.
  5. Add an Element action block that taps Submit.

Good to know

  • There is no Execution context choice: the code always runs in the workflow, with no document of a page to read. To read what is on the phone's screen, use a block such as Read element before it and read the result with refData('prevBlockData') or a variable.
  • The code runs inside an async function, so it can await directly: const item = await fetchData('json', { url: 'https://api.example.com/item' }); nextBlock(item);. An error thrown before or after an await ends the block at once with its message.
  • Code with a syntax error does not run: the block fails at once, with the error, rather than waiting for its timeout.
  • refData only sees the kinds of data your code names, for example refData('variables', 'review'). Build the keyword from a string literal, not from another variable.
  • Call nextBlock once, as the last thing the code does. If the code never names it, one is added at the end with no data.
  • fetchData takes json or text as its type, and is sent from this computer, not from the phone or through its proxy.

See also

  • JavaScript code — Run your own JavaScript in the page, and pass data back to the workflow.
  • Type text — Type text into a field on the phone, found by its selector or the one that has the focus.
  • Read element — Read an element's text, description, id, position or state, or count how many match.
  • Conditions — Send the run down one of several paths, depending on values, page elements or code.