如何等待在量角器后端? [英] How to wait for the backend in Protractor?

查看:158
本文介绍了如何等待在量角器后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试一个网页,用户可以通过一个TextInput发送消息到另一个。 POST请求,然后在服务器和邮件上发送的被倾倒在无功/邮件/新文件夹在磁盘上。

用量角器我打电话 browser.waitForAngular() browser.driver.sleep(automatising消息的发送页面后4000)来留出时间,后端写邮件在磁盘上。

这些电话后,电子邮件的presence的检查失败。当在Unix外壳看,我可以证实,电子邮件发送,也试了下在茉莉花标记与确认电子邮件的presence。

为什么 browser.driver.sleep(4000)无效等待后端进行?我怎样才能更正如下code?

 它(可发送邮件功能(){
    shared.loginContributor();    VAR mailsBeforeMessaging =
        fs.readdirSync(browser.params.mail.queue_path +/新);
    的console.log('mailsBeforeMessaging');
    的console.log(mailsBeforeMessaging.length);
    的console.log(fs.lstatSync(browser.params.mail.queue_path +/新));    VAR usersListing =新UserPages.UsersListing()获得()。
    变种annotatorPage = usersListing.getUserPage(注释);    annotatorPage.sendMessage(TITLE5,content64);    执行exec(/ tmp目录/ check.sh);    //我们预计消息控件消失
    VAR键=元素(by.css()用户配置文件的信息按钮。);
    的console.log('等待');
    browser.wait(EC.elementToBeClickable(按钮),5000);
    的console.log(等待完成');
    。期待(EC.elementToBeClickable(按钮))toBeTruthy();    //等待磁盘上被倾倒的邮件?
    browser.waitForAngular();
    browser.driver.sleep(4000);    执行exec(/ tmp目录/ check.sh);    VAR mailsAfterMessaging =
        fs.readdirSync(browser.params.mail.queue_path +/新);
    的console.log('mailsAfterMessaging');
    //错误:此电子邮​​件的数量没有增加
    的console.log(mailsAfterMessaging.length);
    的console.log(fs.lstatSync(browser.params.mail.queue_path +/新));
});它(XYZ,函数(){    的console.log(fs.lstatSync(browser.params.mail.queue_path +/新));
    //这里的电子邮件的数目被递增
    VAR mailsAfterMessaging =
        fs.readdirSync(browser.params.mail.queue_path +/新);
    的console.log('mailsAfterMessaging');
    的console.log(mailsAfterMessaging.length);
});


解决方案

大多数量角器功能不的的东西。他们排队的东西了要做的之后的,返回的承诺去做。一个块时间表一堆事情要做之后,他们居然开始发生(通过他们在ControlFlow注册的承诺)。

您检查,但都被立即执行。因此,他们正在发生前的任何量角器调用完成任何事情。

使用然后来使等待和依赖在测试明确。像这样的:

  annotatorPage.sendMessage(TITLE5,content64)。然后(函数(){
    执行exec(/ tmp目录/ check.sh);
});

  browser.wait(EC.elementToBeClickable(按钮),5000)。然后(函数(){
   的console.log('等待换点击已完成'); // B
});
的console.log('等待换点击已定'); // 一个

查看量角器控制流文件和 webdriver的JS API文档

它不是你的。这是一个疯狂的API来学习,因为它不采取行动都不像任何人都熟悉正常的同步编程所期望的。

I'm testing a web page where the user can send a message to another via a textinput. A POST request is then send on the server and the message is dumped on the disk in the var/mail/new folder.

After automatising the sending of the message in the page with Protractor I'm calling browser.waitForAngular() and browser.driver.sleep(4000) to leave time for the backend to write the mail on the disk.

After these calls the check of the email's presence fails. When looking in the Unix shell, I can confirm that the email was sent and also the next test marked with in Jasmine with it confirms the presence of the email.

Why is browser.driver.sleep(4000) not effective to wait for the backend to proceed? How can I correct the following code?

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

解决方案

Most of the Protractor functions do not do anything. They queue something up to be done later, and return promise to do it. After an it block schedules a bunch of things to do, they actually start happening (via the promises they registered in the ControlFlow).

Your checks, however, are all executing immediately. So, they are happening before any of the protractor calls accomplish anything.

Use then to make the waiting and dependencies explicit in your test. Like this:

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

or:

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

See the Protractor Control Flow documentation and the Webdriver JS API doc.

Its not you. This is a crazy API to learn because it does not act at all like anyone familiar with normal synchronous programming would expect.

这篇关于如何等待在量角器后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆