【问题标题】:How to wait for the backend in Protractor?如何在量角器中等待后端?
【发布时间】:2015-03-02 11:01:04
【问题描述】:

我正在测试一个网页,用户可以通过文本输入向另一个网页发送消息。然后在服务器上发送一个 POST 请求,并将消息转储到磁盘上的var/mail/new 文件夹中。

在使用 Protractor 自动发送页面中的消息后,我打电话给 browser.waitForAngular()browser.driver.sleep(4000),以便为后端在磁盘上写入邮件留出时间。

在这些调用之后,电子邮件是否存在的检查失败。在 Unix shell 中查看时,我可以确认电子邮件已发送,并且在 Jasmine 中用it 标记的下一个测试确认电子邮件的存在。

为什么browser.driver.sleep(4000) 等待后端继续没有效果?如何更正以下代码?

it("is possible to send a message", function() {
    shared.loginContributor();

    var mailsBeforeMessaging =
        fs.readdirSync(browser.params.mail.queue_path + "/new");
    console.log('mailsBeforeMessaging');
    console.log(mailsBeforeMessaging.length);
    console.log(fs.lstatSync(browser.params.mail.queue_path + "/new"));

    var usersListing = new UserPages.UsersListing().get();
    var annotatorPage = usersListing.getUserPage("annotator");

    annotatorPage.sendMessage("title5", "content64");

    exec("/tmp/check.sh");

    // we expect the message widget to disappear
    var button = element(by.css(".user-profile-info-button"));
    console.log('waiting');
    browser.wait(EC.elementToBeClickable(button), 5000);
    console.log('waiting is finished');
    expect(EC.elementToBeClickable(button)).toBeTruthy();

    // wait for mail to be dumped on the disk?
    browser.waitForAngular();
    browser.driver.sleep(4000);

    exec("/tmp/check.sh");

    var mailsAfterMessaging =
        fs.readdirSync(browser.params.mail.queue_path + "/new");
    console.log('mailsAfterMessaging');
    // ERROR: here the number of emails is NOT incremented
    console.log(mailsAfterMessaging.length);
    console.log(fs.lstatSync(browser.params.mail.queue_path + "/new"));
});

it("xyz", function() {

    console.log(fs.lstatSync(browser.params.mail.queue_path + "/new"));
    // here the number of emails is incremented
    var mailsAfterMessaging =
        fs.readdirSync(browser.params.mail.queue_path + "/new");
    console.log('mailsAfterMessaging');
    console.log(mailsAfterMessaging.length);
});

【问题讨论】:

    标签: javascript angularjs selenium protractor


    【解决方案1】:

    大部分量角器函数不任何事情。他们排队等待稍后完成,并返回承诺去做。在 it 块安排了一堆事情要做之后,它们实际上开始发生(通过它们在 ControlFlow 中注册的承诺)。

    但是,您的检查都是立即执行的。因此,它们发生在任何量角器调用完成任何操作之前。

    使用then 在测试中明确等待和依赖关系。像这样:

    annotatorPage.sendMessage("title5", "content64").then(function() {
        exec("/tmp/check.sh");
    });
    

    或:

    browser.wait(EC.elementToBeClickable(button), 5000).then(function() {
       console.log('wait-for-clickable has completed'); // B
    });
    console.log('wait-for-clickable has been scheduled'); // A
    

    请参阅Protractor Control Flow 文档和Webdriver JS API doc

    不是你。这是一个非常难学的 API,因为它的行为与任何熟悉普通同步编程的人所期望的完全不同。

    【讨论】:

    • 感谢您友好而详细的回答。有什么办法可以在执行测试前清空队列,避免调用then
    • 未来我的注意事项:可以使用browser.controlFlow()flow.execute 控制流量。
    猜你喜欢
    • 1970-01-01
    • 2020-05-06
    • 2016-04-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    相关资源
    最近更新 更多