【问题标题】:My Protractor test is hanging when trying to determine if an element is present尝试确定元素是否存在时,我的量角器测试挂起
【发布时间】:2019-07-21 14:09:43
【问题描述】:

我有一个量角器测试设置,可以完成可以想象的最基本的事情。它登录到一个网络应用程序。一切似乎都很好,直到我尝试通过在后续页面上查找元素的存在来评估登录是否成功。我确定这是一个同步问题,但不知道如何解决。

注意事项:

  • 我对 javascript 还很陌生(尽管对编码不熟悉),因此我们不胜感激代码改进建议。
  • 登录功能就像一个魅力,包括等待。
  • Async / Promises / Awaits 仍然让我有点不知所措。

登录方式

this.login = async function (repo, username, password, domain) {
    if (repo == null){ repo = REPO_UNDER_TEST; }

    browser.driver.manage().window().maximize();

    await this.openLoginPage(domain).
        then( this.selectRepo(repo) ).
        then( this.usernameTextBox.sendKeys(username) ).
        then( this.passwordTextBox.sendKeys(password) ).
        then( this.signInButton.click() ).
        then( async function() {
            console.log(`I'm starting my WAIT at ${Date().toString()}`);
            await browser.wait(
                EC.titleIs(repo), 
                32000
            );
        } )
};

测试代码

describe('Login Tests', function () {
it('should log you in given a good username/password', async function () {
    console.log(`I'm starting my TEST at ${Date().toString()}`);
    await loginPage.login(null, 'edwin', 'e', null);
    console.log(`I'm starting my EVAL at ${Date().toString()}`);

    var searchBox = element.all(by.model('searchQuery'));        
    expect(searchBox.isPresent()).toBe(true);
    expect(true).toBeTruthy();

    });  
});

console.logs 只是为了确保事情以正确的顺序发生。如果我注释掉我期望 searchBox 出现的行,则测试运行完成并正确评估简单的 expect(true).toBeTruthy()。如果我尝试运行 searchBox 的期望,则测试会挂起,直到我手动将其关闭。然后控制台会告诉我期望失败,因为该项目不存在,即使它在 console.log 之前在屏幕上清晰可见,说明 eval 正在启动。

日志输出

[10:22:42] I/hosted - Using the selenium server at http://localhost:4444/wd/hub

Started

I'm starting my TEST at Wed Feb 27 2019 10:22:45 GMT-0800 (Pacific Standard Time)

I'm starting my WAIT at Wed Feb 27 2019 10:22:46 GMT-0800 (Pacific Standard Time)

I'm starting my EVAL at Wed Feb 27 2019 10:23:06 GMT-0800 (Pacific Standard Time)

F

Failures:

1) Login Tests should log you in given a good username/password

  Message:
    Expected false to be true.

  Stack:
    Error: Failed expectation
        at UserContext.<anonymous> 

(C:\GIT\TestFramework_POCs\protract\TESTS\login-spec.js:15:39)
        at process._tickCallback (internal/process/next_tick.js:68:7)


1 spec, 1 failure

Finished in 37.295 seconds


[10:23:22] I/launcher - 0 instance(s) of WebDriver still running

[10:23:22] I/launcher - chrome #01 failed 1 test(s)

[10:23:22] I/launcher - overall: 1 failed spec(s)

[10:23:22] E/launcher - Process exited with error code 1

【问题讨论】:

    标签: protractor ui-automation


    【解决方案1】:

    正如您所怀疑的,这很可能是异步问题的结果。即使您的登录功能运行良好,我个人还是会使用所有 await 而不是 .then() 来编写它,例如

    this.login = async function (repo, username, password, domain) {
    if (repo == null){ repo = REPO_UNDER_TEST; }
    
      await browser.driver.manage().window().maximize();
    
      await this.openLoginPage(domain);
      await this.selectRepo(repo);
      await this.usernameTextBox.sendKeys(username);
      await this.passwordTextBox.sendKeys(password);
      await this.signInButton.click();
      console.log(`I'm starting my WAIT at ${Date().toString()}`);
      await browser.wait(EC.titleIs(repo), 32000);
    };
    

    虽然 .then() 确实有效,但我发现在任何地方使用 await 会更好/更容易。 await 在继续下一行代码之前等待以下操作完成,这与多个 .then() 相同,但您避免了多个 .then() 可能成为的烦人的圣诞树函数。

    我认为您的测试本身没有任何问题,尽管在element.all 上检查isPresent() 可能会导致问题,而我猜这就是问题所在。 elementelement.all 将处理不存在的元素。例如:

    element.all(by.css('NothingHere'); // will return array with length of 0
    element(by.css('NothingHere'); //will return an ElementFinder object
    

    基本上,0 是一个有效的响应,而空的 ElementFinder 会给出错误,所以如果你的第一次检查是在 element.all() 它还没有加载任何东西,它会很高兴地返回 0。但是,element() 会看到什么都没有然而并等待元素变为可见直到它超时

    此外,当使用 async/await 时,您需要在 conf 文件中包含 SELENIUM_PROMISE_MANAGER = false;

    【讨论】:

    • 感谢您的意见。我尝试了您的所有建议,但似乎没有任何效果。在测试中登录完成后,测试似乎什么也找不到。我正在使用 VSCode 进行调试,并在登录后遇到断点后尝试交互/查找任何元素。我根本无法与任何元素进行交互。
    【解决方案2】:

    以下代码中有两个问题:

    1) element.all() 没有isPresent()

    2) 你在下面的代码中错过了await

    var searchBox = element.all(by.model('searchQuery'));        
    expect(await searchBox.get(0).isPresent()).toBe(true);
    
    // or 
    var searchBox = element(by.model('searchQuery'));        
    expect(await searchBox.isPresent()).toBe(true);
    

    请确认如何定位搜索框

    【讨论】:

    • 好收获。不幸的是,它并没有解决问题。登录完成后,我似乎根本无法与页面交互(找不到任何元素)。
    猜你喜欢
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    相关资源
    最近更新 更多