自动化端对端测试框架-Protractor Reference FAQ

本文档详细解答了使用Protractor和Karma进行前端自动化测试时遇到的常见问题,包括如何解决超时问题、区别Karma和Protractor的应用场景、处理登录页面测试等,并提供了丰富的示例代码。

FAQ

My tests time out in Protractor, but everything's working fine when running manually. What's up?

There are several ways that Protractor can time out - see the Timeouts reference for full documentation.

What's the difference between Karma and Protractor? When do I use which?

Karma is a great tool for unit testing, and Protractor is intended for end to end or integration testing. This means that small tests for the logic of your individual controllers, directives, and services should be run using Karma. Big tests in which you have a running instance of your entire application should be run using Protractor. Protractor is intended to run tests from a user's point of view - if your test could be written down as instructions for a human interacting with your application, it should be an end to end test written with Protractor.

Here's a great blog post with more info.

Angular can't be found on my page

Protractor supports angular 1.0.6/1.1.4 and higher - check that your version of Angular is upgraded.

The angular variable is expected to be available in the global context. Try opening chrome devtools or firefox and see if angular is defined.

How do I deal with my log-in page?

If your app needs log-in, there are a couple ways to deal with it. If your login page is not written with Angular, you'll need to interact with it via unwrapped webdriver, which can be accessed like browser.driver.get()

You can put your log-in code into an onPrepare function, which will be run once before any of your tests. See this example (withLoginConf.js)

Which browsers are supported?

The last two major versions of Chrome, Firefox, IE, and Safari. See details at Setting Up the Browser and Browser Support.

The result of getText from an input element is always empty

This is a webdriver quirk<input> and <textarea> elements always have empty getText values. Instead, try element.getAttribute('value').

How can I interact directly with the JavaScript running in my app?

In general, the design of WebDriver tests is to interact with the page as a user would, so it gets a little tricky if you want to interact with the JavaScript directly. You should avoid it unless you have a good reason. However, there are ways of doing it.

You can use the evaluate function on a WebElement to get the value of an Angular expression in the scope of that element. e.g.

element(by.css('.foo')).evaluate('bar')

would return whatever {{bar}} is in the scope of the element with class 'foo'.

You can also execute arbitrary JavaScript in the browser with:

browser.executeScript('your script as a string');

You can also pass a regular JavaScript function into executeScript(), for example:

function getAngularVersion() {
  return window.angular.version.full;}browser.executeScript(getAngularVersion).then(function (version) {
  console.log(version);});

How can I get hold of the browser's console?

In your test:

browser.manage().logs().get('browser').then(function(browserLog) {
  console.log('log: ' + require('util').inspect(browserLog));});

This will output logs from the browser console. Note that logs below the set logging level will be ignored. The default level is warnings and above. To change, add a loggingPrefs object to your capabilities, as described in the DesiredCapabilities docs.

See an example (spec.js) of using this API to fail tests if the console has errors.

How can I get screenshots of failures?

First, this is how you can take a screenshot:

browser.takeScreenshot().then(function(png) {
  var stream = fs.createWriteStream("/tmp/screenshot.png");
  stream.write(new Buffer(png, 'base64'));
  stream.end();});

The method to take a screenshot automatically on failure would depend on the type of failure.

  • For failures of entire specs (such as timeout or an expectation within the spec failed), you can add a reporter as below:

// Note: this is using Jasmine 2 reporter syntax.jasmine.getEnv().addReporter(new function() {
  this.specDone = function(result) {
    if (result.failedExpectations.length >0) {
      // take screenshot
    }
  };});

Note, you can also choose to take a screenshot in afterEach. However, because Jasmine does not execute afterEach for timeouts, those would not produce screenshots

  • For failures of individual expectations, you can override jasmine's addMatcherResult/addExpectationResult function as such:

// Jasmine 2var originalAddExpectationResult = jasmine.Spec.prototype.addExpectationResult;jasmine.Spec.prototype.addExpectationResult = function() {
  if (!arguments[0]) {
    // take screenshot
    // this.description and arguments[1].message can be useful to constructing the filename.
  }
  return originalAddExpectationResult.apply(this, arguments);};

See an example of taking screenshot on spec failures.

How do I produce an XML report of my test results?

You can use the npm package jasmine-reporters@^2.0.0 and add a JUnit XML Reporter in the onPrepare block. This would look something like:

var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
    consolidateAll: true,
    savePath: 'testresults',
    filePrefix: 'reportXMLoutput'
}));

How can I catch errors such as ElementNotFound?

WebDriver throws errors when commands cannot be completed - e.g. not being able to click on an element which is obscured by another element. If you need to retry these actions, try using webdriverjs-retry. If you would just like to catch the error, do so like this

elm.click().then(function() { /* passing case */}, function(err) { /* error handling here */})

How can I test file uploads?

Via Webdriver, you can just send the absolute file path to the input with type=file. See this example.

If you need to test file upload on a remote server (such as Sauce Labs), you need to set a remote file detector. You can do this via browser.setFileDetector(), and you'll need access to the selenium-webdriver node module.

var remote = require('selenium-webdriver/remote');browser.setFileDetector(new remote.FileDetector());

Why is browser.debugger(); not pausing the test?

The most likely reason is that you are not running the test in debug mode. To do this you run: protractor debug followed by the path to your protractor configuration file.

I get an error: Page reload detected during async script. What does this mean?

This means that there was a navigation or reload event while a command was pending on the browser. Usually, this is because a click action or navigation resulted in a page load. Protractor is trying to wait for Angular to become stable, but it's interrupted by the reload.

You may need to insert a browser.wait condition to make sure the load is complete before continuing.

How do I switch off an option in the CLI?

i.e. webdriver-manager update --chrome=false does not work. This has to do with the way optimist parses command line args. In order to pass a false value, do one of the following:

1) webdriver-manager update --chrome=0

2) webdriver-manager update --no-chrome (see https://github.com/substack/node-optimist#negate-fields)

Why does Protractor fail when I decorate $timeout?

Protractor tracks outstanding $timeouts by default, and reports them in the error message if Protractor fails to synchronize with Angular in time.

However, in order to do this Protractor needs to decorate $timeout. This means if your app decorates $timeout, you must turn off this behavior for Protractor. To do so pass in the 'untrackOutstandingTimeouts' flag. 

I still have a question

Please see our Contributing Guidelines for questions and issues.


转载于:https://my.oschina.net/u/658505/blog/665178

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值