我在第三天和Protractor一起工作时,我经常会在等待页面加载和元素出现方面碰到金砖墙.特别是这个测试案例变得丑陋,我想解决问题而不必依赖睡眠.
我目前“在AngularJS之外”
it('it should reflect in both the field and the title when the personnel name is changed',function() { var inputField,personnelHeader,personnelName; personnelName = element(By.css(".overlay.editnameoverlay")).click(); personnelHeader = element(By.id("personnel_name_header")); inputField = element(By.css("input[name='newvalue']")); inputField.clear(); inputField.sendKeys("Test 123"); element(By.css("input[name='ok_button']")).click(); // browser.driver.sleep(2000); This test only works with this sleep added browser.wait(function() { console.log("Waiting for header to change..."); return personnelHeader.getText().then(function(text) { return text === "Test 123"; }); },5000); return expect(personnelHeader.getText()).toBe(personnelName.getText()); });
因此,此处的测试会更改输入字段中的名称.提交它并等待更改反映在模态的标题中.问题是如果没有browser.driver.sleep(2000),我会收到错误消息
Stacktrace: StaleElementReferenceError: stale element reference: element is not attached to the page document
在这种特殊情况下,我该如何解决这个问题?
解决方法
从
Expect Conditions的文档:
var EC = protractor.ExpectedConditions; // Waits for the element with id 'abc' to contain the text 'foo'. browser.wait(EC.textToBePresentInElement($('#abc'),'foo'),5000);