简体   繁体   English

告诉量角器阻塞异步调用

[英]tell protractor to block on asynchronous call

I'm adding a fresh, angular client-tier to a legacy app. 我正在向旧版应用程序添加一个新的,有角度的客户端层。 Upon login the legacy up redirects to a 'home' url. 登录后,旧版本将重定向到“主” URL。 The url contains a session id which I need to grab and use (in the url) for any subsequent gets/posts. 该网址包含一个会话ID,我需要该会话ID才能在随后的任何获取/发布中使用(在网址中)。 After login I call: 登录后,我致电:

browser.getCurrentUrl()

and then use a regex to extract the session id. 然后使用正则表达式提取会话ID。 I store the session id away and use it for later gets/posts. 我将会话ID存储起来,并用于以后的获取/发布。

The problem is though that browser.getCurrentUrl() returns a promise and all my tests run before I can get the session id back. 问题在于,尽管browser.getCurrentUrl()返回了一个promise并且我所有的测试都在我可以获取会话ID之前运行。 How can I make protractor wait for the browser.getCurrentUrl() to resolve. 如何使量角器等待browser.getCurrentUrl()解析。

Specifically below where I have the code: 具体在下面我有代码的地方:

var sessionId = loginPage.login('testuser@example.com', 'testuser');
homePage = new HomePage(sessionId);

I really need all code to block on loginPage.login() so I'll have a defined session id. 我真的需要所有代码来阻止loginPage.login(),所以我将具有定义的会话ID。 My home page tests and any other page tests will need the session id to run properly. 我的首页测试和其他任何页面测试都需要会话ID才能正常运行。

How can I achieve this in protractor? 我该如何在量角器中实现这一目标?

Thanks! 谢谢!

The relevant parts of my code looks like this... 我的代码的相关部分看起来像这样...

home.spec.js: home.spec.js:

describe('home page tests', function() {
  var loginPage = new LoginPage();
  var homePage;

  // get sessionId from login and create a new HomePage object from it
  beforeEach(function() {
    var sessionId = loginPage.login('testuser@example.com', 'testuser');
    homePage = new HomePage(sessionId);
    homePage.get();
  });

  describe('main elements of home page test', function() {    
    it('page has correct username as part of user menu', function() {
      expect(homePage.getUsername()).toEqual('testuser@example.com');
    });
  });
});

login.po.js: login.po.js:

function LoginPage {

  // ...snip...

  this.login = function(username, password) {
    return this.get()
      .then(function() {
        this.username.sendKeys(username);
        this.password.sendKeys(password);

        this.loginButton.click();
      })
      .then(function() {
        return browser.getCurrentUrl().then(function(url) {
          var groups = sessionIdRegex.exec(url);

          // return the extracted session id or null if there is none
          if (groups !== null) {
            return sessionIdRegex.exec(url)[2];
          } else {
            return null;
          }
        });
      });
  };
}

home.po.js: home.po.js:

function HomePage(sessionId) {
  this.username = element(by.binding('username'));

  this.getUsername = function() {
    return this.username.getText();
  }

  this.get = function() {
    return browser.get(browser.baseUrl + sessionId + '#/home');
  };
};

module.exports = HomePage;

The simplest could be to use expect : 最简单的方法是使用expect

Jasmine expectations are also adapted to understand promises. 茉莉花的期望也可以用来理解诺言。 That's why the line 这就是为什么线

`expect(name.getText()).toEqual('Jane Doe'); `expect(name.getText())。toEqual('Jane Doe');

works - this code actually adds an expectation task to the control flow, which will run after the other tasks. 有效-此代码实际上将期望任务添加到控制流中,该任务将在其他任务之后运行。

login.po.js: login.po.js:

function LoginPage {

  this.login = function(username, password) {
    return this.get()
      .then(function() {
        this.username.sendKeys(username);
        this.password.sendKeys(password);

        this.loginButton.click();
      })
      .then(function() {
        return browser.getCurrentUrl().then(function(url) {
          var groups = sessionIdRegex.exec(url);

          // return the extracted session id or null if there is none
          if (groups !== null) {
            return sessionIdRegex.exec(url)[2];
          } else {
            return null;
          }
        });
      });
  };
  expect(this.login).not.toBeUndefined();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM