代码之家  ›  专栏  ›  技术社区  ›  stian

如何用量角器正确编写菜单选择测试?

  •  0
  • stian  · 技术社区  · 7 年前

    可能是因为承诺是在单击之后的某一时刻解决的,所以在单击事件之后但在触发的操作完成之前解决的吗(显示带有延迟的下拉菜单)

    如果是这种情况,我应该如何创建一个测试,等待直到选择菜单可见,然后再继续从菜单中选择一个选项?

    相关问题: Selecting a element in a drop-down menu in protractor

    import {browser, by, element} from 'protractor';
    
    describe('workspace-project App', () => {
      
        it('should select', () => {
          browser.get('http://localhost:4200/test').then(() => {
              // browser.sleep(4000);
              element(by.tagName('mat-form-field')).click().then(() => {
                // browser.sleep(3000);
                element.all(by.css('span.mat-option-text')).getText().then((values) => {
                  element.all(by.css('span.mat-option-text')).filter((elem, index) => {
                    return elem.getText().then((text) => {
                      return values[1] === text;
                    });
                  }).first().click();
                  browser.sleep(2000);
                });
              });
            });
          });
    });
    <p>
      <mat-form-field>
        <mat-select [(value)]="selected">
          <mat-option>None</mat-option>
          <mat-option value="option1">Option 1</mat-option>
          <mat-option value="option2">Option 2</mat-option>
          <mat-option value="option3">Option 3</mat-option>
        </mat-select>
      </mat-form-field>
    </p>
    2 回复  |  直到 7 年前
        1
  •  0
  •   Kacper    7 年前

    如果要在执行操作之前等待元素,则应使用: http://www.protractortest.org/#/api?view=ProtractorExpectedConditions.prototype.visibilityOf

    element.all(by.css('span.mat-option-text')).getText().then((values) => {
      element.all(by.css('span.mat-option-text')).filter((elem, index) => {
        return elem.getText().then((text) => {
          return values[1] === text;
        });
      }).first().click();
    });
    

    现在你正在努力表演 getText() ElementArrayFinder ElementFinder 此外,你正在循环这些 两次。 在 filter() 你真是太棒了 index 但不要在任何地方使用它。索引是可选参数-如果您不打算使用它-ommit。

    values[1] === text; -如果你想选择 option2

    describe('workspace-project App', () => {
      it('should select', () => {
        return browser.get('http://localhost:4200/test').then(() => {
          return element(by.tagName('mat-form-field')).click().then(() => {
            //add Expected Condition here
            return element(by.css('mat-option[value="option2"]')).click();
          });
        });
      });
    });
    
        2
  •  0
  •   Sudhir Sonu Kumar    7 年前