var createTime=10:00;
var createdPatient="vijay";
/* In for loop after comparing the time with createdTime 'i' value should be the compare index but 'i' value becoming last index each time*/
element(by.xpath("//div[@id='patients']")).all(by.xpath("div[@class='ng-scope']")).then(
function(divtags){
for(i=0;i<divtags.length;i++){
divtags[i].element(by.xpath("div[1]/div[2]")).getText().then(function(time){
if(time==createdTime){
/*Here i am getting 'i' value every time last divtag length */ divtags[i].element(by.xpath("div[@class='patient']")).getText().then(function(patientName){
if(patientName==createdPatient){
divtags[i].click();
}})}});}});
请告诉我如何在将时间与 Protractor 循环代码中创建的时间进行比较后获得正确的索引“i”值。
最佳答案
不,这不是您在 Protractor 中解决此类问题的方式。你需要一个 filter()
按时间和姓名过滤所需的“患者”。
这是经过一些额外改进的代码:
$$("#patients > div").filter(function (patient) {
return patient.element(by.xpath("div[1]/div[2]")).getText().then(function (time) {
return patient.$(".patient").getText().then(function (patientName) {
return time === createdTime && patientName == createdPatient;
});
});
}).first().click();
不能保证它会按原样工作,因为我没有办法重现和测试它,但我希望你能理解并能适本地调整建议的解决方案。
关于javascript - 带有 if 条件 Protractor 的异步 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38819756/