javascript 量角器和 Angular:如何在一个应用程序中一个接一个地测试两个页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20199563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:00:25  来源:igfitidea点击:

Protractor and Angular: How to test two pages in an app, one after the other?

javascriptangularjsprotractor

提问by Richard

I would like to run Protractor tests on two separate pages in my Angular app: /dashboardand /articles.

我想在我的 Angular 应用程序中的两个单独页面上运行 Protractor 测试:/dashboard/articles.

The complication is that I have to log in to the app manually.

复杂的是我必须手动登录应用程序。

Currently I have this setup:

目前我有这个设置:

var LoginPage = function() {
  ptor = protractor.getInstance();
  this.login = function(url) {
    ptor.get(url);
    ptor.findElement(protractor.By.model('email')).sendKeys(config.LOGIN_EMAIL);
    ptor.findElement(protractor.By.model('password')).sendKeys(config.LOGIN_PASS);
    ptor.findElement(protractor.By.tagName('button')).click();
  };
};

describe('The dashboard', function() {
  console.log('logging in');
  var loginPage = new LoginPage();
  loginPage.login(config.DASHBOARD_URL);
  console.log('logged in');
  it('has a heading', function() {
    console.log('testing dashboard 1');
    heading = ptor.findElement(protractor.By.tagName('h1'));
    expect(heading.getText()).toEqual(config.DASHBOARD_HEADING);
  });
});

describe('The article widget', function() {
  console.log('logging in');
  var loginPage = new LoginPage();
  loginPage.login(config.ARTICLE_URL);
  console.log('logged in');
  it('has a heading', function() {
    console.log('testing article 1');
    heading = ptor.findElement(protractor.By.tagName('h1'));
    expect(heading.getText()).toEqual(config.ARTICLES_HEADING);
  });
});

This gives me the following output:

这给了我以下输出:

Selenium standalone server started at http://192.168.2.9:56791/wd/hub
logging in
LoginPage
logged in
logging in
LoginPage
logged in
testing dashboard 1
Ftesting article 1

It looks as though both the describesections are kicking off in parallel. How can I force the following sequence of events, while still structuring the code in a sensible way?

看起来这两个describe部分都在并行开始。如何强制执行以下事件序列,同时仍以合理的方式构建代码?

  1. Load dashboard page
  2. Log in
  3. Run dashboard tests
  4. Load article page (Assume we are already logged in)
  5. Run article tests
  1. 加载仪表板页面
  2. 登录
  3. 运行仪表板测试
  4. 加载文章页面(假设我们已经登录)
  5. 运行文章测试

采纳答案by user2167582

describe('my app', function(){
    beforeEach(function(){
        login()...
    })
    describe('dashboard');
    describe('the article widget')
});

回答by Andres D

You can move the login to another file.

您可以将登录名移动到另一个文件。

Then, in your protractor configuration file do this:

然后,在您的量角器配置文件中执行以下操作:

exports.config = {
  specs: [
    'spec/login.js',
    'spec/dashboard_test.js',
    'spec/article_test.js'
  ],
  ...
};

Login will run before the other tests

登录将在其他测试之前运行

回答by Tamlyn

The Protractor documentationrecommends

量角器文档建议

put your log-in code into an onPreparefunction, which will be run once before any of your tests.

将您的登录代码放入一个onPrepare函数中,该函数将在您的任何测试之前运行一次。

For example in your protractor.conf

例如在你的 protractor.conf

onPrepare: function() {
    browser.driver.get('http://localhost/login.html');

    browser.driver.findElement(by.id('username')).sendKeys('Jane');
    browser.driver.findElement(by.id('password')).sendKeys('1234');
    browser.driver.findElement(by.id('clickme')).click();

    // Login takes some time, so wait until it's done.
    // For the test app's login, we know it's done when it redirects to
    // index.html.
    return browser.driver.wait(function() {
       return browser.driver.getCurrentUrl().then(function(url) {
          return /index/.test(url);
       });
    }, 10000);
}

回答by user3512851

I had a similar issue with my e2e protractor tests. Describe blocks were being executed in parallel, causing my tests to fail.

我的 e2e 量角器测试也有类似的问题。描述块被并行执行,导致我的测试失败。

My code before the fix was something like:

我在修复之前的代码是这样的:

describe('test1', function() {
  it('do foo1', function() {..});
  describe('do test1', function() {..});
});
describe('test2', function() {
  it('do foo2', function() {..});
  describe('do test2', function() {..});
});

Both the describe blocks were being executed in parallel causing my tests to fail. The fix was to enclose the it blocks in describe blocks.

两个描述块都在并行执行,导致我的测试失败。解决方法是将 it 块包含在描述块中。

Code after the fix:

修复后的代码:

describe('test1', function() {
  describe('foo1', function() {
    it('do foo1', function() {..});  
  });
  describe('do test1', function() {..});
});
describe('test2', function() {
  describe('foo2', function() {
    it('do foo2', function() {..});  
  });
  describe('do test2', function() {..});
});

Link to a similar issue on protractor github: https://github.com/angular/protractor/issues/592

链接到量角器 github 上的类似问题:https: //github.com/angular/protractor/issues/592