0

I have a number of automated ui tests which run in parallel using the wdio maxInstances. At the start of each test I generate a random / unique mobile number by doing the following:

07 - All numbers start with this number. 
Followed by a 9 digit number based on time & date - `new Date().getTime().toString().substring(4, 13)`

Unfortunately I'm facing issues where occasionally the timestamp is exactly the same. This is due to the test generating the mobile number at exactly the same time. The second approach I tried was:

07 - All numbers start with this number.
Followed by a 6 digit number based on time & date - `new Date().getTime().toString().substring(4, 10)`.
Followed by a 3 digit random number - `Math.floor(Math.random() * 900 + 100);`.

This approach has resulted in less duplicated mobile numbers being generated, however I still occasionally get the same number generated.

Another approach I would like to try is to get the wdio instance thread/runner number and append it to the end of the mobile number. That way if a number is generated at the exact same time, the thread number will mean it will have a unique number. Is anyone able to shed some light on how to do this please.

Daredevi1
  • 103
  • 3
  • 13

1 Answers1

1

I am not sure about getting the thread number but we do a different thing. We try to assign unique number for each spec file.

Something like this:

const fs = require('fs');
    //list of features files that we have
    let listOfFiles = fs.readdirSync(process.cwd() + '/features');
    //Mapping one file with a unique number
    let fileMappedWithNumber = listOfFiles.map((file, index) => {
        const item = {
            file: process.cwd() + '/features/' + file,
            number: ++index
        }
        return item;
    });
    console.log(JSON.stringify(fileMappedWithNumber));

This code is placed in onPrepare hook. This variable fileMappedWithNumber can be assigned global and can be used throughout the code.

The specs in the beforeSession hook can be used to match with the file.

  • My onPrepare hook is in the wdio.conf. How can I call fileMappedWithNumber from there? – Daredevi1 Oct 18 '19 at 08:19
  • Hi .. define the `fileMappedWithNumber ` above the `export.config` and assign this variable to one of the global variable if needed. Like this: `let fMWN; export.config = { ... onPrepareHook{ the above code } ... beforeSession{ global.fMWN = fMWN} .... }` – Naveen Thiyagarajan Oct 18 '19 at 16:56
  • Hi.. added our solution to your other question: https://stackoverflow.com/questions/58463916/is-it-possible-to-access-a-variable-declared-in-wdio-conf/58466351#58466351 try and let me know it works – Naveen Thiyagarajan Oct 19 '19 at 17:43