量角器和角度:如何在一个应用中测试两个页面,一个接着一个?

时间:2021-08-02 01:23:23

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

我想在我的倾斜应用程序:/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 describe sections are kicking off in parallel. How can I force the following sequence of events, while still structuring the code in a sensible way?

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

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

4 个解决方案

#1


7  

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

#2


9  

You can move the login to another file.

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

Then, in your protractor configuration file do this:

然后,在您的pro拖拉机配置文件中这样做:

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

Login will run before the other tests

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

#3


7  

The Protractor documentation recommends

量角器文档建议

put your log-in code into an onPrepare function, 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);
}

#4


-1  

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.

两个description块被并行执行,导致我的测试失败。解决方法是将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

链接到pro拖拉机github上的一个类似问题:https://github.com/angular/protractor/issues/592

#1


7  

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

#2


9  

You can move the login to another file.

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

Then, in your protractor configuration file do this:

然后,在您的pro拖拉机配置文件中这样做:

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

Login will run before the other tests

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

#3


7  

The Protractor documentation recommends

量角器文档建议

put your log-in code into an onPrepare function, 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);
}

#4


-1  

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.

两个description块被并行执行,导致我的测试失败。解决方法是将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

链接到pro拖拉机github上的一个类似问题:https://github.com/angular/protractor/issues/592