我编写了一些代码,旨在弄清楚用户是否登录了任何网站。我不明白代码的执行顺序,但我看不到如何解决它。非常感谢任何见解。
代码:
var login_promises = [];
var logout_promises = [];
var toggle_login_promises = [];
var toggle_logout_promises = []; var loginStringsExist, logoutStringsExist;
var string_tally = {loginStrings: 0, logoutStrings: 0, toggle_in: 0, toggle_out: 0}
var promises = {loginStrings: [], logoutStrings: [], toggle_in: [], toggle_out: []};
var loginStrings = [ "login",
"Login",
"LOGIN",
"Log in",
"Log In",
"LOG IN",
"log in",
"signin",
"Signin",
"SIGNIN",
"sign in",
"Sign in",
"Sign In",
"SIGN IN",
"log on",
"Log on",
"Log On",
"LOG ON",
"logon",
"Logon",
"LOGON",
"logon id",
"Logon id",
"Logon Id",
"LOGON ID"
];
var logoutStrings = [ "sign out",
"Sign out",
"Sign Out",
"SIGN OUT",
"signout",
"Signout",
"SIGNOUT",
"log out",
"Log out",
"Log Out",
"LOG OUT",
"logout",
"Logout",
"LOGOUT"
];
var toggleInOutStrings = [ "signin/out",
"signin / out",
"sign in/out",
"Sign in/out",
"Sign in/Out",
"Sign In/Out",
"SIGN IN/OUT",
"sign in / out",
"Sign in / out",
"Sign in / Out",
"Sign In / Out",
"SIGN IN / OUT",
"login/out",
"login / out",
"log in/out",
"Log in/out",
"Log in/Out",
"Log In/Out",
"LOG IN/OUT",
"log in / out",
"Log in / out",
"Log in / Out",
"Log In / Out",
"LOG IN / OUT",
];
function login_check() {
}
var getStringExistsCount = function(string, type){
return new Promise(
function(reject, resolve){
element(by.linkText(string)).isPresent()
.then(function(el){
console.log("ELEMENTS FOUND: " + el)
if(el){
string_tally[type] ++;
}else{
console.log(string + " NOT FOUND");
}
resolve(el);
});
}
)
}
var load_promises = function(arr, type){
return new Promise(function(resolve, reject){
for (var i = 0; i < arr.length; i++) {
console.log(i + ") ")// + type + " " + string + "\n" + arr.length)
promises[type].push(getStringExistsCount(arr[i], type));
if(i === arr.length - 1){
console.log(promises[type]);
resolve();
}
}
});
}
login_check.prototype.is_account_logged_in = function(pw) {
return new Promise(
function(resolve, reject){
if(pw){
resolve(false)
}else{
load_promises(loginStrings, "loginStrings")
.then(function(){
load_promises(logoutStrings, "logoutStrings");
}).then(function(){
Promise.all(promises.logoutStrings);
})
.then(function(){
Promise.all(promises.logoutStrings)
console.log("LOGIN PROMISES: " + promises.loginStrings.length);
console.log("LOGOUT PROMISES: " + promises.logoutStrings.length);
console.log("TOGGLE LOGIN PROMISES: " + promises.toggle_in.length);
console.log("TOGGLE LOGOUT PROMISES: " + promises.toggle_out.length);
})
.then(function(){
console.log("STRING_TALLY:")
console.log(string_tally);
resolve(string_tally.loginStrings < string_tally.logoutStrings)
});
}
}
)
}
输出:
.pw_is_present: false
0)
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20)
21)
22)
23)
24)
[ Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> } ]
0)
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
[ Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> } ]
LOGIN PROMISES: 25
LOGOUT PROMISES: 14
TOGGLE LOGIN PROMISES: 0
TOGGLE LOGOUT PROMISES: 0
STRING_TALLY:
{ loginStrings: 0, logoutStrings: 0, toggle_in: 0, toggle_out: 0 }
ELEMENTS FOUND: false
login NOT FOUND
ELEMENTS FOUND: false
sign out NOT FOUND
ELEMENTS FOUND: false
Login NOT FOUND
ELEMENTS FOUND: false
Sign out NOT FOUND
ELEMENTS FOUND: false
LOGIN NOT FOUND
ELEMENTS FOUND: false
Sign Out NOT FOUND
ELEMENTS FOUND: false
Log in NOT FOUND
ELEMENTS FOUND: false
SIGN OUT NOT FOUND
ELEMENTS FOUND: false
Log In NOT FOUND
ELEMENTS FOUND: false
signout NOT FOUND
ELEMENTS FOUND: false
LOG IN NOT FOUND
ELEMENTS FOUND: false
Signout NOT FOUND
ELEMENTS FOUND: false
log in NOT FOUND
ELEMENTS FOUND: false
SIGNOUT NOT FOUND
ELEMENTS FOUND: false
signin NOT FOUND
ELEMENTS FOUND: false
log out NOT FOUND
ELEMENTS FOUND: false
Signin NOT FOUND
ELEMENTS FOUND: false
Log out NOT FOUND
ELEMENTS FOUND: false
SIGNIN NOT FOUND
ELEMENTS FOUND: false
Log Out NOT FOUND
ELEMENTS FOUND: false
sign in NOT FOUND
ELEMENTS FOUND: false
LOG OUT NOT FOUND
ELEMENTS FOUND: false
Sign in NOT FOUND
ELEMENTS FOUND: false
logout NOT FOUND
ELEMENTS FOUND: false
Sign In NOT FOUND
ELEMENTS FOUND: false
Logout NOT FOUND
ELEMENTS FOUND: false
SIGN IN NOT FOUND
ELEMENTS FOUND: false
LOGOUT NOT FOUND
ELEMENTS FOUND: false
log on NOT FOUND
ELEMENTS FOUND: false
Log on NOT FOUND
ELEMENTS FOUND: false
Log On NOT FOUND
ELEMENTS FOUND: false
LOG ON NOT FOUND
ELEMENTS FOUND: false
logon NOT FOUND
ELEMENTS FOUND: false
Logon NOT FOUND
ELEMENTS FOUND: false
LOGON NOT FOUND
ELEMENTS FOUND: false
logon id NOT FOUND
ELEMENTS FOUND: false
Logon id NOT FOUND
ELEMENTS FOUND: false
Logon Id NOT FOUND
ELEMENTS FOUND: false
LOGON ID NOT FOUND
我在应该至少有一个匹配的网站上测试了这个(即ELEMENTS FOUND:true
不知道这笔交易是什么。
我不太确定如何问这个,所以如果有人能够在这里看到任何东西,我可以回答有关问题的进一步问题。
答案 0 :(得分:1)
Promise.all(promises.logoutStrings)
和Promise.all(promises.logoutStrings)
也不会对您有太大用处。如果找到所有字符串的linkText,那么两个聚合的promises将只跟随它们各自的成功路径 - 这不是你想要的,并且实际上永远不会发生。
你想要的是一个承诺,它将响应于找到任何一个字符串的链接而解决,因此你需要一个raceToResolve(...)
函数(两次)。只有这样,使用Promise.all()
来聚合两场比赛。
此外,load_promises()
是同步的,因此不需要返回promise,并且通过从load_promises()
返回映射的数组,对外部变量login_promises
等的需求消失。
你可以写:
var loginStrings = [.....]; // big array of "login" variants
var logoutStrings = [.....]; // big array of "logout" variants
function raceToResolve(arr) {
return new Promise(function(resolve, reject) {
var errCount = 0;
arr.forEach(function(p) {
p.then(
function(val) { resolve(val); },
function(err) {
errCount++;
if (errCount === arr.length) {
reject(0);
}
}
);
});
});
}
function getStringExistsCount(string) {
return element(by.linkText(string)).isPresent();
}
function load_promises(arr, successCode) {
return arr.map(getStringExistsCount).then(function(el) {
if(el) { return successCode; } // found
else { throw new Error(); } // not found
});
}
function login_check() {}
login_check.prototype.is_account_logged_in = function(pw) {
if(pw) {
return Promise.resolve(false);
} else {
var loggedInPromise = firstToResolve(load_promises(loginStrings, 1));
var loggedOutPromise = firstToResolve(load_promises(logoutStrings, 2));
return Promise.all([loggedInPromise, loggedOutPromise])
.then(function(results) {
var compositeCode = results[0] | results[1]; // bitwise `|` will give 0(indeterminate), 1(loggedIn), 2(loggedOut) or 3(ambiguous).
return compositeCode === 1; // will return `true` if unambiguously logged in, otherwise false.
// alternatively, expose the compositeCode itself
// return compositeCode;
});
}
}
我省略了与toggleInOutStrings
有关的一切。也许我错过了一些东西,但我看不出“登录/退出”形式链接的存在/不存在将有助于确定登录/退出状态。