WebdriverIO调用sharedStore.get返回Promise pending如何解决
问题根因
运行时出现Promise { <pending> }提示,核心是两个代码写法问题:
browser.sharedStore.get()是异步方法,调用后返回Promise对象,没有加await等待Promise执行完成就直接打印输出,拿到的自然是待执行状态的Promise,不是实际存储的业务值。- 存取操作的key大小写不匹配:你注释里存值用的key是
SomeDesc(首字母大写S),实际取值传入的key是someDesc(首字母小写s),就算补了await也读不到对应存储值。
修复后代码
调整代码给get方法加await,同时统一存取的key名称即可,参考如下:
describe('Test', () => { it('Test', async () => { console.log(Generallib.createRandomCharacter(8, "CompanyDesc")) console.log('Setting the store') // set操作是同步的,无需加await browser.sharedStore.set('someDesc', 'See this'); console.log('Store set'); console.log('Accessing the store'); // 无需额外加pause等待,await get本身会等待值读取完成 const storeVal = await browser.sharedStore.get('someDesc'); console.log(storeVal); console.log('Store accessed') }); });
注意事项
- Shared Store用于多并行测试Worker之间的跨进程数据共享,
set方法为同步执行,不需要加await - 所有
get读取操作都是异步的,必须加await等待结果返回,否则永远只能拿到pending状态的Promise - 存储的key严格区分大小写,存取时必须保证传入的字符串完全一致
内容的提问来源于stack exchange,提问作者Prakash P
相关产品推荐
相关产品推荐

