gpt4 book ai didi

javascript - 如何立即执行 promise

转载 作者:行者123 更新时间:2023-12-03 03:23:25 26 4
gpt4 key购买 nike

需要帮助执行 promise 。代码:

it('[TEST-1] Log in with invalid credentials-wrong email', function() {
//LogIn
browser.logger.info("[ENTRY] - Username: " + browser.params.Adenity.loginInfo[2].username);
element(by.id('js-input-email')).sendKeys(browser.params.Adenity.loginInfo[2].username);
expect(element(by.id('js-input-email')).getAttribute('value')).toEqual(browser.params.Adenity.loginInfo[2].username);
element(by.id('js-input-email')).getAttribute('value').then(function(pom) {
if (pom == browser.params.Adenity.loginInfo[2].username)
browser.logger.info("Username populated succesfully");
else
browser.logger.info("Username not populated succesfully");
});
browser.logger.info("[ENTRY] - Password: " + browser.params.Adenity.loginInfo[2].password);
element(by.id('js-input-password')).sendKeys(browser.params.Adenity.loginInfo[2].password);
expect(element(by.id('js-input-password')).getAttribute('value')).toEqual(browser.params.Adenity.loginInfo[2].password);
element(by.id('js-input-password')).getAttribute('value').then(function(pom) {
if (pom == browser.params.Adenity.loginInfo[2].password)
browser.logger.info("Password populated succesfully");
else
browser.logger.info("Password not populated succesfully");
});
});

实际结果:

[INFO] protractorLog4js - [ENTRY] - Username: oiiuo@iuz.com
[INFO] protractorLog4js - [ENTRY] - Password: Test1234
[INFO] protractorLog4js - Username populated succesfully
[INFO] protractorLog4js - Password populated succesfully

我想要的结果:

[INFO] protractorLog4js - [ENTRY] - Username: oiiuo@iuz.com
[INFO] protractorLog4js - Username populated succesfully
[INFO] protractorLog4js - [ENTRY] - Password: Test1234
[INFO] protractorLog4js - Password populated succesfully

我不知道该如何解决这个问题,所以非常感谢您的帮助。

最佳答案

sendKeys 还返回一个 promise ,以便确保事情按照您期望的顺序发生,您的代码必须看起来更像:

element(by.id('js-input-email')).sendKeys(browser.params.Adenity.loginInfo[2].username).then(function() {
return element(by.id('js-input-email')).getAttribute('value');
}).then(function(pom) {
if (pom == browser.params.Adenity.loginInfo[2].username)
browser.logger.info("Username populated succesfully");
else
browser.logger.info("Username not populated succesfully");

browser.logger.info("[ENTRY] - Password: " + browser.params.Adenity.loginInfo[2].password);
return element(by.id('js-input-password')).sendKeys(browser.params.Adenity.loginInfo[2].password)
}).then(function() {
expect(element(by.id('js-input-password')).getAttribute('value')).toEqual(browser.params.Adenity.loginInfo[2].password);
return element(by.id('js-input-password')).getAttribute('value')
}).then(function(pom){
if (pom == browser.params.Adenity.loginInfo[2].password)
browser.logger.info("Password populated succesfully");
else
browser.logger.info("Password not populated succesfully");
});

参见 - https://javascript.info/promise-chaining

关于javascript - 如何立即执行 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46467167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com