简体   繁体   English

如何测试使用量角器进行 api 调用的多个点击事件

[英]How to test multiple click events that make api calls with protractor

I have some buttons that when clicked make an ajax request, and when the request completes it enables/disables buttons (for pagination).我有一些按钮,当点击它们时会发出 ajax 请求,当请求完成时,它会启用/禁用按钮(用于分页)。 I am not 100% sure on how to handle this since the request takes upto 1000ms, I would like to know if this is the correct way to handle the click by waiting for the next button to become enabled:我不是 100% 确定如何处理这个,因为请求需要长达 1000 毫秒,我想知道这是否是通过等待下一个按钮启用来处理点击的正确方法:

When('I click {string} to navigate pagination', async (item: ButtonType) => {
  const first = element(by.css('#first'));
  const last = element(by.css('#last'));
  switch (item) {
    case 'first':
      await last.click();
      await browser.wait(until.elementIsEnabled(first.getWebElement()), 1500);
      await first.click();
      break;
  }
});

Do I even need to tell the browser to wait or does protractor know that it needs to wait automatically?我什至需要告诉浏览器等待还是量角器知道它需要自动等待?

Here is one of my assertions:这是我的断言之一:

Then('I should see the {string} set of items', async (item: ButtonType) => {
  const firstEnabled = await element(by.css('#first')).isEnabled();
  const prevEnabled = await element(by.css('#prev')).isEnabled();
  const nextEnabled = await element(by.css('#next')).isEnabled();
  const lastEnabled = await element(by.css('#last')).isEnabled();
  switch (item) {
    case 'first':
      expect(firstEnabled).to.eq(false);
      expect(prevEnabled).to.eq(false);
      expect(nextEnabled).to.eq(true);
      expect(lastEnabled).to.eq(true);
      break;
  }
});

When it runs I am getting the following error:当它运行时,我收到以下错误:

[chrome #01-6]    ✖ Then I should see the "first" set of items
[chrome #01-6]        AssertionError
[chrome #01-6]            + expected - actual
[chrome #01-6] 
[chrome #01-6]            -true
[chrome #01-6]            +false

The first button should be disabled because there are no more pages before it since it is the first page.第一个按钮应该被禁用,因为它是第一页,因为它之前没有更多的页面。

What is it that I need to do to get this to pass?我需要做什么才能让这件事通过?

Normally, whenever an application is waiting for request to populate results in the UI, there should be a loading animation, to indicate the page hasn't been loading.通常,每当应用程序等待请求在 UI 中填充结果时,都应该有一个加载动画,以指示页面尚未加载。 If this is something developers can add, this would be ideal.如果这是开发人员可以添加的内容,这将是理想的。 In this case you will just wait until the loading animation goes away在这种情况下,您只需等到加载动画消失

If this is not an option then look for any UI changes, lets say if the button is disabled until the results are received and it clearly indicates the app awaits for the results, then it would be the best strategy.如果这不是一个选项,那么寻找任何 UI 更改,假设按钮在收到结果之前被禁用,并且它清楚地表明应用程序正在等待结果,那么这将是最好的策略。 You can basically add waiting until the button is clickable os make a suctom function.您基本上可以添加等待,直到按钮可点击 os make a suctom 功能。 If you're having problems with this let me know I'll help如果您在这方面遇到问题,请告诉我,我会提供帮助

You can use ExpectedConditions to wait for a button to be clickable.您可以使用ExpectedConditions等待按钮可点击。 One thing to be aware of, it does not check if the button you want to click is covered by another element or not.需要注意的一件事是,它不会检查您要单击的按钮是否被另一个元素覆盖。 It only checks that the element is enabled and visible in the UI such that it can be clicked.它只检查元素是否在 UI 中启用和可见,以便可以单击它。 If your element is covered by an overlay or loading animation or something like that, then you will need to wait for that element to go away or at least not be covering the button before you click on it.如果您的元素被覆盖或加载动画或类似的东西覆盖,那么您将需要等待该元素消失或至少在单击它之前没有覆盖按钮。 Here is how I use it:这是我如何使用它:

import { ExpectedConditions } from 'protractor';

export default class MyClass {
  public EC = ExpectedConditions;
  public wait = 5000;

  public async waitForElementToBeClickable(elem: ElementFinder): Promise<void> {
    await browser.wait(this.EC.elementToBeClickable(elem), this.wait);
  }
}


// usage
const myElement = element(by.css('my-button'));
await myClass.waitForElementToBeClickable(myElement);
await myElement.click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM