JavaScript code
Run your own JavaScript in the workflow, to work on variables and the table between phone blocks.
Only in
WarpX 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.
{{variables.review}}; the Type text block after it types that review into the field.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 is20000.Example20000- JavaScript code
Click the preview to edit. A new block starts with a comment and
nextBlock();. Available:refData(keyword, path)readsvariables,table,loopData,prevBlockDataorglobalData;setVariable(name, value);nextBlock(data, insert);resetTimeout();fetchData(type, resource). Each also exists with anautomaprefix, such asautomaNextBlock.Exampleconst 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
httporhttpsaddress 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 as1000,3000to 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
- Start
- Start app
- Element action
- JavaScript code
- Type text
- Element action
- Open the order's review screen with Start app and Element action blocks.
- Add a JavaScript code block and click its preview.
- Enter code that picks one reply from a list at random, calls
setVariable('review', reply)and ends withnextBlock({ review: reply }). - Add a Type text block that types
{{variables.review}}into the review field. - 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
documentof 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 withrefData('prevBlockData')or a variable. - The code runs inside an async function, so it can
awaitdirectly:const item = await fetchData('json', { url: 'https://api.example.com/item' }); nextBlock(item);. An error thrown before or after anawaitends 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.
refDataonly sees the kinds of data your code names, for examplerefData('variables', 'review'). Build the keyword from a string literal, not from another variable.- Call
nextBlockonce, as the last thing the code does. If the code never names it, one is added at the end with no data. fetchDatatakesjsonortextas 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.