Rao*_*Rao 7 javascript selenium automated-tests async-await protractor
以下代码随机运行,有时它工作正常,有时会抛出这样的错误 Stale Element Reference Exception
我想要的是我想首先执行以下操作
element(by.id('FiltItemTransDocNo')).sendKeys(grno);
Run Code Online (Sandbox Code Playgroud)
在上面我希望这个在下面执行
element.all(by.name('chkGrd')).first().click();
Run Code Online (Sandbox Code Playgroud)
我试过这种方式,但它似乎没有用
element(by.id('FiltItemTransDocNo')).sendKeys(grno).then(function(el){
element.all(by.name('chkGrd')).first().click();
});
Run Code Online (Sandbox Code Playgroud)
帮我解决这个问题
我已经附加了我发送钥匙到购物征收领域的图像,并根据我想出的结果只会显示一个结果,我想点击,如果我将条件设置为visiblity它将永远是true我将导致同样的问题

sendKeys和的快速说明e1快速注释,sendKeys不返回WebElement或ElementFinder.这意味着上面示例中的e1可能未定义.
关于假设的快速说明:答案假定将文本发送到过滤器会改变屏幕上的行数或元素数.如果在发送文本后屏幕上有相同数量的元素,那么这将不起作用.我会在下面看看Florent关于过时参考错误的评论.
过时的元素通常在DOM发生变化时发生.如果您在Angular中使用某些结构指令,那么如果您使用*ngFor或*ngIf,DOM将会更改.我的猜测是,在过滤元素后,您将根据过滤器在DOM实际更改期间或之前获取DOM中的元素.这将导致陈旧的引用Web元素.在我的examplex下面,我使用async/await关闭控制流.
您可以显式设置睡眠,以便在您调用单击第一个元素之前更新DOM.这可能导致潜在的片状测试,因为根据您将运行的环境,超时未知.
it('should do something', async () => {
const filterItem = element(by.id('FiltItemTransDocNo'));
await filterItem.sendKeys(grno);
await browser.sleep(1000); // or some other waits
await element.all(by.name('chkGrd')).first().click();
});
Run Code Online (Sandbox Code Playgroud)
或者,您可以element.all对点击之前和之后的项目数量进行比较检查,并仅在更新内容时继续进行.
it('should do something', async () => {
const filterItem = element(by.id('FiltItemTransDocNo'));
const table = element.all(by.name('chkGrd'));
const length = await table.count();
await filterItem.sendKeys(grno);
// After the filter keys are sent, check to see if the current table
// count is not equal to the `table.count()`.
let updated = false;
await browser.wait(async () => {
updated = length !== await table.count();
return updated;
}, 5000);
// So if we use the entire 5 seconds and the count has not changed,
// we should probably fail before clicking on stuff.
expect(updated).toBeTruthy();
// now we can click on the next element.
await element.all(by.name('chkGrd')).first().click();
});
Run Code Online (Sandbox Code Playgroud)
为什么呼叫length !== await table.count()工作?这是因为该表表示获取Web元素的承诺.调用count方法时,它首先解析Web元素来执行操作.根据DOM的变化,这可能会有所不同.然后我们将当前计数与前一计数进行比较.
在配置文件中,您需要指定您不在控制流中:
exports.config = {
// In this case, I plan to use a selenium standalone server
// at http://127.0.0.1:4444/wd/hub. You could also use other
// methods like direct connect or starting it up with 'local'.
seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
// Required flag to tell Protractor you do not want to use the
// control flow. This means that you will have to either chain
// your promises or async / await your actions. Historically
// jasminewd node module would resolve promises for you. This
// package will no longer be used in future releases since the
// control flow is deprecated.
SELENIUM_PROMISE_MANAGER: false,
// The rest of your config...
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.