在尝试确定元素是否存在时,我的量角器测试挂起

问题描述 投票:1回答:2

我有一个量角器测试设置,可以做出最基本的事情。它登录到一个Web应用程序。在我尝试通过查找后续页面中元素的存在来评估登录是否成功之前,一切似乎都很顺利。我确定这是一个同步问题,但无法弄清楚如何解决它。

注意事项:

  • 我对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出现的行,那么测试将运行完成并正确评估simple 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
2个回答
0
投票

如您所料,这很可能是异步问题的结果。即使您的登录功能正常,我会亲自使用所有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()可以成为。

我没有看到你的测试本身有什么问题,虽然在isPresent()上检查element.all可能会导致问题,而且我猜是问题所在。 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()会看到还没有什么,等待元素到变得可见,直到它超时如果你能在element.all(by.model('searchQuery'));中找到一个独特的元素并反过来,我希望它会通过。

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


0
投票

以下代码中的两个问题:

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);

请确认如何定位searchBox

© www.soinside.com 2019 - 2024. All rights reserved.