有没有办法确保it
在if
条件位于其下方之前先执行?
示例代码:
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中设置为true。
答案 0 :(得分: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();
}))
}))
答案 1 :(得分: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.');
}
});
});