2017-04-14 51 views
0

有没有办法确保it在位于其下面的if条件之前先执行?量角器IF条件在IT之前首先执行

示例代码:

it('should click ' + strName + ' from the list', function() { 
    exports.waitForElementDisp(10, global.elmGravityPanel, false); 
    browser.sleep(2000).then(function() { 
     element(by.css('[cellvalue="' + strName + '"]')).click(); 
     element(by.id('uniqueid')).getText().then(function(tmpText) { 
      global.tempObject.isPresent = false; 
      if (tmpText == strName) { 
       global.tempObject.isPresent = true; 
      } 
     }); 
    }); 
}); 

if (global.tempObject.isPresent == true) { 
    it('should click the settings icon', function() { 
     global.elmSettingBtn.click(); 
    }); 

    it... 
} 

目前,global.tempObject.isPresent被设置为null,量角器IF内没有去,即使它会被设置在第一IT内部为真。

+1

包住如果条件的browser.call内()... HTTP://www.protractortest.org/#/api查看= webdriver.WebDriver.prototype.call – Grasshopper

+0

那控制流问题 – Gunderson

+0

@Grasshopper你的意思是,bro​​wser.call(if(condition){...})? –

回答

0

因为这个'if'是在文件解析时执行的。 我建议尝试这样的:

it(`should click ${strName} from the list`, function() { 
    exports.waitForElementDisp(10, global.elmGravityPanel, false); 
    browser.sleep(2000) 
    $(`[cellvalue="${strName}"]`).click(); 
    $('#uniqueid').getText().then(function (tmpText) { 
     global.tempObject.isPresent = false; 
     if (tmpText == strName) { 
      global.tempObject.isPresent = true; 
     } 
    }); 

}); 

/** 
* 
* @param condition Any boolean condition to check 
* @param suiteOrTest function, that contains scheduling of tests. 
*/ 
function runIf(condition, suiteOrTest) { 
    if (condition) { 
     return suiteOrTest 
    } else { 
     console.log('Skipping execution') 
    } 
} 

describe('Conditional execution',() => { 
    it('1', function() { 
     global.elmSettingBtn.click(); 
    }); 

    it('This test is with condition', runIf(process.env.IS_LINUX,()=> { 
     // Some other test 
     global.elmSettingBtn.click(); 
    })) 
})) 
+0

嗨,我得到一个错误,指出“ReferenceError:描述没有定义”我需要更新任何描述行吗? 我粘贴了行,并得到了错误。 –

+0

哦拼写错误。更新后出现不同的错误 - TypeError:无法读取未定义的属性“长度” –

+0

对不起,试过这个。现在应该没问题。再次检查代码或这里是例子:https://github.com/Xotabu4/Protractor-Experiments/blob/master/experiments/conditionalJasmine/experiment.ts – Xotabu4

0

我不是包装在有条件的it块的粉丝。所以下面是我的想法,你将如何改变你的代码,以避免使用it块的条件。

  • 如果你期待出现的元素以下it块独特的文字,你可以增加浏览器等待预期的条件元素出现,而不是具有任意的睡眠时间。元素可用后,我们可以对文本进行一些检查。
  • 如果您有混合的工作流程,有时会测试以查看是否存在唯一文本,以及其他时间是否存在唯一测试,那么我会将其分成两个不同的工作流程。
  • 如果该元素不存在,那么基于uniqueText不存在,我们可能会通过测试失败。这可能是矫枉过正。

let EC = ExpectedConditions; 
    let button = element(by.css('[cellvalue="' + strName + '"]')); 
    let uniqueText = element(by.id('uniqueid')); 

    // your locator strategy here. 
    let elemSettingBtn = element(by.css('something')); 

    it('should click ' + strName + ' from the list', function() { 
    // Leaving this line alone. Not sure what this is doing. 
    exports.waitForElementDisp(10, global.elmGravityPanel, false);  

    browser.wait(EC.elementToBeClickable(button), 3000); 
    button.click(); 

    browser.wait(EC.presenceOf(uniqueText), 3000); 
    expect(uniqueText.getText()).toEqual(strName); 
    }); 

    it('should click the settings icon', function() { 
    uniqueText.isPresent().then(present => { 
     if (present) { 
     // click the settings button because the unique text is there. 
     return elmSettingBtn.click(); 
     } else { 
     // Maybe fail the test when the button is not present? 
     fail('unique text is not present.'); 
     }   
    }); 
    });