量角器,我什么时候应该在 click() 之后使用 then()

2023-04-21前端开发问题
3

本文介绍了量角器,我什么时候应该在 click() 之后使用 then()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在运行一个 Angular 应用程序,当在量角器上测试 click() 时,我不知道何时应该使用 then() 解决承诺.

I'm running an Angular app and when testing on protractor a click(), I don't know when should I resolve the promise with a then().

我在 Protractor API 上找到了这个:

I found this on Protractor API:

当点击命令完成时将被解决的承诺.

A promise that will be resolved when the click command has completed.

那么,我应该在每次 click 中使用 click().then() 吗?

So, should I use click().then() in every click?

推荐答案

那么,我应该在每次点击中使用 click().then() 吗?

So, should I use click().then() in every click?

绝对不是.

不需要,因为 Protractor/WebDriverJS 有这种称为 " 的机制控制流",它基本上是一个需要解决的承诺队列:

It's not needed because Protractor/WebDriverJS has this mechanism called "Control Flow" which is basically a queue of promises that need to be resolved:

WebDriverJS 维护一个待处理的 Promise 队列,称为控件流,以保持执行有条理.

WebDriverJS maintains a queue of pending promises, called the control flow, to keep execution organized.

并且 Protractor 会自然地、开箱即用地等待 Angular:

and Protractor waits for Angular naturally and out-of-the-box:

您不再需要在测试中添加等待和睡眠.量角器可以在测试的那一刻自动执行下一步网页完成待处理任务,您无需担心等待您的测试和网页同步.

You no longer need to add waits and sleeps to your test. Protractor can automatically execute the next step in your test the moment the webpage finishes pending tasks, so you don’t have to worry about waiting for your test and webpage to sync.

这导致了一个非常直接的测试代码:

Which leads to a quite straight-forward testing code:

var elementToBePresent = element(by.css(".anotherelementclass")).isPresent();

expect(elementToBePresent.isPresent()).toBe(false);
element(by.css("#mybutton")).click();
expect(elementToBePresent.isPresent()).toBe(true);

<小时>

但有时,如果您遇到同步/计时问题,或者您的被测应用不是 Angular,您可以通过使用 then()<显式解析 click() 来解决它/code> 并在点击回调中继续:


Sometimes though, if you experience synchronization/timing issues, or your app under test is non-Angular, you may solve it by resolving the click() explicitly with then() and continue inside the click callback:

expect(elementToBePresent.isPresent()).toBe(false);
element(by.css("#mybutton")).click().then(function () {
    expect(elementToBePresent.isPresent()).toBe(true);
});

还有显式等待在这些情况下的救援,但在这里不相关.

There are also Explicit Waits to the rescue in these cases, but it's not relevant here.

这篇关于量角器,我什么时候应该在 click() 之后使用 then()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13