如何打开多个窗口或操作多个实例 [英] How do I open multiple windows or operate multiple instances

查看:43
本文介绍了如何打开多个窗口或操作多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果出于某种疯狂的原因我需要打开 100 个窗口/选项卡并导航到其中的 100 个不同链接,我该怎么做?我可以同时在所有 100 个测试中运行某些测试吗?

If for whatever crazy reason I need to open 100 windows/tabs and navigate to 100 different links in them how do I do that? Can I simultaneously run certain tests in all 100 of them?

假设我有一个数组 ['a','b','c','d','e'],我需要测试某种形式是否适用于所有这些值.如何打开 5 个实例(或窗口或任何可以独立于其余实例控制的实例)并同时测试它们?例如:

let's say I have an array ['a','b','c','d','e'], I need to test if some form works for all these values. How can I open 5 instances (or windows or whatever that can be controlled independently from the rest) and test them simultaneously? e.g.:

  • 找到表单的输入文本框,
  • 将文本值更改为数组的其中之一
  • 点击提交按钮
  • 运行某些断言等等,所有这些都是并行的.同时测试所有数组值而不是一一测试

upd:我想我可以使用

browser.executeScript("window.open('https://angularjs.org/', 'tab" + i + "')")

然而,这并不允许我真正并行运行测试,因为我将不得不从一个选项卡跳到另一个选项卡,假设所有选项卡都打开并加载:

yet that doesn't allow me to truly run tests in parallel, since I'm gonna have to jump from tab to tab, assuming all tabs open and loaded:

1) 从数组中选取值2)修改输入框3)点击提交按钮4)切换到下一个标签5) 重复

1) pick value from array 2) modify input box 3) click submit button 4) switch to next tab 5) repeat

是的,这仍然比在一个选项卡中测试所有内容、循环遍历数组并每次都重置页面要快,但我需要找到更好的方法

Yes, this will still be faster than testing everything in one tab, looping through array and resetting the page every time, but I need to find a better way

推荐答案

启动 100 个不同的浏览器只是为了让它们可以运行非常相似的场景,这听起来效率极低.如果这些不同的值启动相同的流程,只是输出略有不同,您可能希望对这些值使用单元测试,并使用量角器仅运行一个或几个以进行端到端测试.

It sounds terribly inefficient to kick off 100 different browsers just so they can run very similar scenarios. If those different values kick off the same flow, just slightly different output, you might want to use unit tests for those and run just one or few using protractor to test end-to-end.

但是要回答您的问题,有两种方法.

But to answer your question, there are two ways.

1) multiCapabilities:在这里,每个浏览器都会运行一个完全不同的测试.(如果您的测试相似,您可能希望重用公共组件).

1) multiCapabilities: Here, each browser will run a completely different test. (You might want to reuse a common component if your tests are similar).

exports.config = {
  specs: [
    // leave this empty if you have no shared tests. 
  ],

  multiCapabilities: [{
    'browserName': 'chrome',
    'specs': ['test1.js']
  }, {
    'browserName': 'chrome',
    'specs': ['test2.js']
  }, {
    'browserName': 'chrome',
    'specs': ['test3.js']
  }],
};

文档:https://github.com/angular/protractor/blob/master/docs/referenceConf.js

2) browser.forkNewDriverInstance():在这里,您只运行一个测试,但该测试可以产生 n 个单独的浏览器.缺点是因为一切都在 1 个测试中,如果 100 个案例中有 1 个失败,你只会得到一个失败.

2) browser.forkNewDriverInstance(): Here, you only run one test, but the test can spawn off n separate browsers. Disadvantage is that since everything is in just 1 test, if 1 case out of 100 fails, you'll just get a single failure.

var runtest = function(input, output) {
  var newBrowser = browser.forkNewDriverInstance(true); // true means use same url
  // note I used newBrowser.element instead of element, because you are accessing the new browser. 
  newBrowser.element(by.model(...)).sendKeys(input).click();
  expect(newBrowser.element(by.css('blah')).getText()).toEqual(output);
};

describe('...', function() {
  it('spawn browsers', function() {
    browser.get(YOUR_COMMON_URL);

    runtest('input1', 'output1');
    runtest('input2', 'output2');
    runtest('input3', 'output3');
    runtest('input4', 'output4');
  });
});

文档:https://github.com/angular/protractor/blob/master/docs/browser-setup.md#using-multiple-browsers-in-the-same-test

这篇关于如何打开多个窗口或操作多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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