首页 > 解决方案 > 如何使用量角器定位和填充模态?

问题描述

我有一个使用 JHipster 制作的网站:http ://www.jhipsterpress.com/#/post/15/view 我正在尝试使用 Protractor 进行测试(我是新手)。所以我想测试的第一件事是登录网站。当您进入时,您必须在模态中登录。

  describe('JhipsterPress Demo App', function() {
it('Should login', function() {
    browser.get('http://www.jhipsterpress.com/#/post/15/view');

        element(by.id('username')).sendKeys('admin');
        element(by.id('password')).sendKeys('admin');
        var username = element(by.binding('username'));
        var password = element(by.binding('password'));
        element(by.css("button[class='btn btn-primary']")).click().then(function(){
        const EC = protractor.ExpectedConditions;
        // Waits max. 5 seconds for the input field to become clickable
        browser.wait(EC.elementToBeClickable(by.css("button[class='btn btn-primary']")), 5000);
    });
});

});

量角器说...

  Message:
    Failed: by.id(...).click is not a function
  Stack:
    TypeError: by.id(...).click is not a function

编辑:修复括号错误后:

Failures:
1) JhipsterPress Demo App Should login
  Message:
    Failed: element not interactable
      (Session info: chrome=71.0.3578.98)
      (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17134 x86_64)
  Stack:
    ElementNotVisibleError: element not interactable
      (Session info: chrome=71.0.3578.98)
      (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17134 x86_64)

标签: protractorjhipster

解决方案


您当前正在发生,因为 Protractor 尝试与出现的模式对话框的输入字段进行交互。要解决此问题,您可以使用来自 的预期条件Protractor

有了它们,您可以等到输入字段可以交互:

element(by.id('login').click()).then(function(){
    const EC = protractor.ExpectedConditions;
    // Waits max. 5 seconds for the input field to become clickable
    browser.wait(EC.elementToBeClickable(element(by.id('username')), 5000);
    element(by.id('username')).sendKeys('admin');
    ...
});

推荐阅读