簡體   English   中英

WebdriverIO - 如何創建可在所有測試實例之間更新和訪問的映射變量?

[英]WebdriverIO - How can I create a map variable that can be updated and accessed between all test instances?

是否可以創建一個全局映射變量,該變量可以在 WebdriverIO 項目中的所有測試實例之間更新和訪問?

我正在執行 API 調用,為每個功能配置返回一個 ID,但這些 ID 需要存儲在所有測試實例都可以訪問的鍵/值變量中,這樣就不會創建重復的 ID,並且相同的 ID 可以由使用相同功能的測試使用。

這就是我試圖實現我進行 API 調用的想法的方式:

let axios = require('axios');
global.IDs = new Map();

let API = Object.create(null, {

    getID: {
        value: async function (cid, json) {

            let key = cid.split("-")[0];

            if(global.IDs.has(key)){
                return global.IDs.get(key);
            } else {
                let config = {
                    method: 'post',
                    url: /*redacted*/,
                    headers: { 
                        'Content-Type': 'application/json',
                        'Access-Control-Allow-Origin': '*'
                    },
                    data: json
                };
                await axios(config).then(function (response) {
                    global.IDs.set(key, response.data.id);
                    return response.data.id;
                }).catch(function (error) {
                    return null;
                });
            }
        }
    },

});

module.exports = API;

請注意應該存儲 ID 的global.IDs映射變量。 執行多個測試時,盡管按下鍵/值對, global.IDs變量始終顯示為空。 這是我打印過程時執行的樣子:

// Bad results :(
[0-0] global.IDs = []
[0-0] cid = 0
map does not have cid = 0
[0-0] ID returned = 495
[0-0] new global.IDs = [ [ '0', 495 ] ]
[0-1] global.IDs = []
[0-1] cid = 0
map does not have cid = 0
[0-1] ID returned = 496
[0-1] new global.IDs = [ [ '0', 496 ] ]
[1-0] global.IDs = []
[1-0] cid = 1
map does not have cid = 1
[1-0] ID returned = 497
[1-0] new global.IDs = [ [ '1', 497 ] ]
[1-1] global.IDs = []
[1-1] cid = 1
map does not have cid = 1
[1-1] ID returned = 498
[1-1] new global.IDs = [ [ '1', 498 ] ]

最終,執行應該是這樣的:

//Good results :)
[0-0] global.IDs = []
[0-0] cid = 0
map does not have cid = 0
[0-0] ID returned = 495
[0-0] new global.IDs = [ [ '0', 495 ] ]
[0-1] global.IDs = [ [ '0', 495 ] ]
[0-1] cid = 0
map has cid = 0
returning value = 495
[1-0] global.IDs = [ [ '0', 495 ] ]
[1-0] cid = 1
map does not have cid = 1
[1-0] ID returned = 496
[1-0] new global.IDs = [ [ '0', 495 ], [ '1', 496 ] ]
[1-1] global.IDs = [ [ '0', 495 ], [ '1', 496 ] ]
[1-1] cid = 1
map has cid = 1
returning value = 496

先感謝您!

在我的測試中,我還需要一個全局變量。 我在wdio.conf.js文件的beforeSuite鈎子中聲明了這個變量,它在所有測試中都可用。

配置文件

beforeSuite: function (suite) {
  var test = {a: 1}
  global.test = test
}

我的測試文件

describe('temp test ', () ->
  it('temp test', () ->
    console.log('test = ', test)
  )
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM