如何在量角器中等待后端?

How to wait for the backend in Protractor?

我正在测试一个网页,用户可以在其中通过文本输入向另一个人发送消息。然后在服务器上发送 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);
});

大多数量角器函数不任何事情。他们排队稍后 完成一些事情,return 承诺会完成。在 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 documentation and the Webdriver JS API doc

不是你。这是一个疯狂的 API 学习,因为它根本不像任何熟悉普通同步编程的人所期望的那样。