【问题标题】:Get wdio instance thread number获取 wdio 实例线程号
【发布时间】:2019-10-17 15:08:26
【问题描述】:

我有许多使用 wdio maxInstances 并行运行的自动化 ui 测试。 在每次测试开始时,我通过执行以下操作生成一个随机/唯一的手机号码:

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

不幸的是,我有时会遇到时间戳完全相同的问题。这是由于测试在完全相同的时间生成手机号码。 我尝试的第二种方法是:

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);`.

这种方法减少了生成的重复手机号码,但是我仍然偶尔会生成相同的号码。

我想尝试的另一种方法是获取 wdio 实例线程/运行器编号并将其附加到手机号码的末尾。这样,如果一个数字是同时生成的,线程号将意味着它有一个唯一的数字。 有没有人能解释一下如何做到这一点。

【问题讨论】:

    标签: selenium-webdriver webdriver cucumber webdriver-io cucumberjs


    【解决方案1】:

    我不确定是否要获取线程号,但我们会做不同的事情。我们尝试为每个规范文件分配唯一编号。

    类似这样的:

    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));
    

    这段代码放在onPrepare钩子里。这个变量fileMappedWithNumber 可以全局赋值,并且可以在整个代码中使用。

    beforeSession钩子中的specs可以用来匹配文件。

    【讨论】:

    • 我的 onPrepare 钩子在 wdio.conf 中。如何从那里调用 fileMappedWithNumber?
    • 嗨 .. 在 export.config 上方定义 fileMappedWithNumber 并在需要时将此变量分配给全局变量之一。像这样:let fMWN; export.config = { ... onPrepareHook{ the above code } ... beforeSession{ global.fMWN = fMWN} .... }
    • 嗨.. 为您的其他问题添加了我们的解决方案:stackoverflow.com/questions/58463916/… 尝试让我知道它有效
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多