Running AngularJS Protractor with proxy to https - angularjs

I get the following error in the command line when trying to run Protractor:
>
Fatal error: protractor exited with code: 1
I need to proxy to an https test server. How do I accomplish this? I followed the advice from this Github issue, but I am still getting the above error. Here is my config file:
// A reference configuration file.
exports.config = {
// ----- How to setup Selenium -----
//
// There are three ways to specify how to use Selenium. Specify one of the
// following:
//
// 1. seleniumServerJar - to start Selenium Standalone locally.
// 2. seleniumAddress - to connect to a Selenium server which is already
// running.
// 3. sauceUser/sauceKey - to use remote Selenium servers via SauceLabs.
// The location of the selenium standalone server .jar file.
seleniumServerJar: './selenium/selenium-server-standalone-2.35.0.jar',
// The port to start the selenium server on, or null if the server should
// find its own unused port.
seleniumPort: null,
// Chromedriver location is used to help the selenium standalone server
// find chromedriver. This will be passed to the selenium jar as
// the system property webdriver.chrome.driver. If null, selenium will
// attempt to find chromedriver using PATH.
chromeDriver: './selenium/chromedriver',
// Additional command line options to pass to selenium. For example,
// if you need to change the browser timeout, use
// seleniumArgs: ['-browserTimeout=60'],
seleniumArgs: [],
// If sauceUser and sauceKey are specified, seleniumServerJar will be ignored.
// The tests will be run remotely using SauceLabs.
sauceUser: null,
sauceKey: null,
// ----- What tests to run -----
//
// Spec patterns are relative to the location of this config.
specs: [
'./e2e/*-spec.js'
],
// ----- Capabilities to be passed to the webdriver instance ----
//
// For a full list of available capabilities, see
// https://code.google.com/p/selenium/wiki/DesiredCapabilities
// and
// https://code.google.com/p/selenium/source/browse/javascript/webdriver/capabilities.js
capabilities: {
'browserName': 'chrome',
'proxy': {
'proxyType': 'manual',
'httpProxy': 'https://localhost.com:8443/'
}
},
// A base URL for your application under test. Calls to protractor.get()
// with relative paths will be prepended with this.
baseUrl: 'http://localhost:9999',
// Selector for the element housing the angular app - this defaults to
// body, but is necessary if ng-app is on a descendant of <body>
rootElement: 'body',
// ----- Options to be passed to minijasminenode -----
jasmineNodeOpts: {
// onComplete will be called just before the driver quits.
onComplete: null,
// If true, display spec names.
isVerbose: true,
// If true, print colors to the terminal.
showColors: true,
// If true, include stack traces in failures.
includeStackTrace: true,
// Default time to wait in ms before a test fails.
defaultTimeoutInterval: 10000
}
};

According to the WebDriver capabilities documentation, you should use 'hostname:port' as the format for the httpProxy. E.g.:
capabilities: {
browserName: 'firefox',
proxy: {
proxyType: 'manual',
httpProxy: 'localhost:8443',
sslProxy: 'localhost:8888'
}
}
Check your proxy software for the correct port.
This works in Firefox and Chrome.

Related

Target browser must be a string... (selenium, protractor)

i have upgraded my environment to have support for my tests for firefox, chrome, etc.
I have installed:
Node LTS (6.10.0)
Selenium Server Standalone 3.1.0
protractor#5.1.1
npm#4.1.2
When i now want to run my test i receive:
[17:31:32] I/launcher - Running 1 instances of WebDriver
[17:31:32] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
[17:31:32] E/launcher - Error: TypeError: Target browser must be a string, but is <undefined>; did you forget to call forBrowser()?
[...]
[17:31:32] E/launcher - Process exited with error code 100
Process finished with exit code 100
What does that mean?
The config.js is:
var TIMEOUT = 10000;
exports.config = {
...
capabilities: [
{
'browserName': 'firefox', //tried as 'firefox', firefox and "firefox"
//'marionette': true //tried true and false
},
],
...
};
That was tricky and funny :) You know what .. There is no issue with browserName. Problem was you are providing the capabilities object incorrectly. You are providing an array whereas you need to send 1 capabilities object
Remove '[' & ']'
capabilities: [{'browserName': 'firefox'},] - This is incorrect . This means an Array of size 1 with index 0 holding your firefox config
It should be this - capabilities: {'browserName': 'firefox'}

Taking screenshots in BrowserStack running Protractor

Hello!
I'm trying to take screenshots in protractor and browserstack, I've the following conf.js file:
var HtmlReporter = require('protractor-html-screenshot-reporter');
var reporter=new HtmlReporter({
baseDirectory: './protractor-result', // a location to store screen shots.
docTitle: 'Report Test Summary',
docName: 'protractor-tests-report.html'
});
// An example configuration file.
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome',
'version': '22.0',
'browserstack.user' : 'user_name',
'browserstack.key' : 'user_key',
'browserstack.debug' : 'true'
},
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['./specs/home_page_spec.js'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
},
onPrepare: function() {
jasmine.getEnv().addReporter(reporter);
}
};
And the browserstack help says that I need to add the following lines:
var fs = require('fs');
webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
return driver.takeScreenshot().then(function(data) {
fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
if(err) throw err;
});
})
};
driver.saveScreenshot('snapshot1.png');
Does any one could point me to where to add these lines? and how? (I'm also using PageObject pattern)
I think you are talking about two things, one is the plugin for making screenshots and the second is a creating screeshots manually within your test code regardless a plugin.
In theory, the plugin should make screenshots for you. If not you can create them manually with the code provided from browserstack, just put in anywhere in your test code after some expects.
Anyway, to make screenshots from protractor I recommend use protractor-screenshoter-plugin
(disclaimer: I am the author), also you can have a look at Protractor-Screenshots-Alernatives
Now in one of the branches I have a new support for parallelization. Have a look at https://github.com/azachar/protractor-screenshoter-plugin/tree/feat-parallel-support.
To install the unstable version that contains the parallelization support use
npm install azachar/protractor-screenshoter-plugin#feat-parallel-support
to install stable version without parallel support, just type as usual:
npm install protractor-screenshoter-plugin
Let me know if it works as expected!
Cheers,
Andrej

Can I run a functional test using theintern js framework without setting up a standalone selenium server?

I have a working webdriver javascript test script for my html page that runs using ChromeDriver without needing to start up a selenium standalone server:
test.js
'use strict';
var path = require('path');
var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var options = new chrome.Options();
var logging_prefs = new webdriver.logging.Preferences();
logging_prefs.setLevel(webdriver.logging.Type.BROWSER, webdriver.logging.Level.ALL);
options.setLoggingPrefs(logging_prefs);
var driver = new webdriver.Builder().withCapabilities(options.toCapabilities()).build();
driver.get('file://' + path.resolve('./index.html'));
// Do some testing
driver.quit();
I want to port this test to use theintern.io, but I'd prefer not to have to run a standalone selenium server. Is this possible?
[Edit: Add info on the error and theintern config]
I see the error [POST http://localhost:4444/wd/hub/session] connect ECONNREFUSED which I guess is because I don't have the standalone server running.
My theintern config looks like this:
define({
environments: [
{ browserName: 'chrome' }
],
// Name of the tunnel class to use for WebDriver tests
tunnel: 'NullTunnel',
// Non-functional test suite(s) to run in each browser
suites: [ /* 'myPackage/tests/foo', 'myPackage/tests/bar' */ ],
// Functional test suite(s) to run in each browser once non-functional tests are completed
functionalSuites: [ 'tests/functional/index' ],
// A regular expression matching URLs to files that should not be included in code coverage analysis
excludeInstrumentation: /^(?:tests|node_modules)\//
});
My theintern test looks like this:
define([
'intern!object',
'intern/chai!assert',
'require'
], function (registerSuite, assert, require) {
registerSuite({
name: 'index',
'first test': function () {
return this.remote
.get(require.toUrl('index.html'))
... //more test logic
}
});
});
Intern speaks the standard WebDriver protocol so can be used with any server that implements the specification, not just Selenium. In this case if you are trying to connect to ChromeDriver just make sure that it is running first (chromedriver --port=4444 --url-base=wd/hub) and then run intern-runner config=mid/of/config and you should be good to go with the configuration that you currently have.

when running protractor with phantomjs browser, only able to run tests once

test code:
describe('mysite', function(){
var init_url = 'http://localhost/mySite/#/home';
beforeEach(function(){
// driver = new webdriver.Builder().
// withCapabilities(webdriver.Capabilities.phantomjs()).build();
})
it('should click on toolbox and do stuff', function(){
browser.get(init_url);
browser.waitForAngular();
browser.getCurrentUrl().then(function(url){
console.log('current_url', url);
expect(init_url).toEqual(init_url);
})
expect(true).toBe(true);
browser.sleep(2000);
})
result 1st time run,
Using the selenium server at http://localhost:9515
data Zoom Pad
class active
mysite
should click on toolbox and do stuff
Finished in 3.94 seconds
1 test, 4 assertions, 0 failures
2nd time run, without any interruption, just up arrow and enter:
Stacktrace:
Error: Error while running testForAngular: Error Message => 'Detected a pag
e unload event; asynchronous script execution does not work across page loads.'
caused by Request => {"headers":{"Accept":"application/json; charset=utf-8","Co
nnection":"keep-alive","Content-Length":"689","Content-Type":"application/json;c
harset=UTF-8","Host":"localhost:9515"},"httpVersion":"1.1","method":"POST","post
":"{\"script\":\"return (function () {\\n var attempts = arguments[0];\\n var
callback = arguments[arguments.length - 1];\\n var check = function(n) {\\n
try {\\n if (window.angular && window.angular.resumeBootstrap) {\\n
callback([true, null]);\\n } else if (n < 1) {\\n if (window.angular
) {\\n callback([false, 'angular never provided resumeBootstrap']);\\n
} else {\\n callback([false, 'retries looking for angular exceed
third time
1) mysite should click on toolbox and do stuff
Message:
Error: ECONNREFUSED connect ECONNREFUSED
Stacktrace:
Error: ECONNREFUSED connect ECONNREFUSED
at ClientRequest.<anonymous> (K:\Users\Congwen\AppData\Roaming\npm\node_modu
les\protractor\node_modules\selenium-webdriver\http\index.js:127:16)
at ClientRequest.EventEmitter.emit (events.js:95:17)
at Socket.socketErrorListener (http.js:1547:9)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
and on third time the phantomjs webserver is down, and needs to be reconnected, and afterwards it goes back to result 1:
any clues?
config file used:
exports.config = {
seleniumAddress: 'http://localhost:9515',
specs: [
'./ptor-tests/mysite-test.js'
],
capabilities: {
browserName: 'phantomjs',
version: '',
platform: 'ANY'
},
//baseUrl: 'http://testapp.example.com/index.html',
rootElement: 'body',
allScriptsTimeout: 11000,
onPrepare: function () {},
jasmineNodeOpts: {
onComplete: function () {},
isVerbose: true,
showColors: true,
includeStackTrace: true,
defaultTimeoutInterval: 30000
}
};
also I noticed that sometimes there's no step 2 needed and it will go directly to ECONNECT error, and sometimes it gets stuck in step 2 for a number of tests and eventually will terminate phantomjs server.
This is an issue with Protractor that was resolved in version 0.17 and made better in 0.18.
It's a bug with a long tail, but the TL;DR is that Protractor's .get(url) function actually uses client-side JavaScript to make the location change; this is to ensure it properly bootstraps. An unfortunate side effect of that design is that for some reason, PhantomJS takes a few seconds to navigate over properly.
The bug was resolved by adding a longer timeout to the .get function.
Github Issue: https://github.com/angular/protractor/issues/85
Relevant changelog entries:
v0.18
(10aec0f) fix(pageload): increase wait timeout
The 300 ms wait caused problems when testing IE on Sauce Labs. It seems way too short. "browser.get()" invariably timed out. Increasing it solved our problem.
v0.17
(a0bd84b) fix(pageload): add a wait during protractor.get() to solve unload issues
Some systems would not wait for the browser unload event to finish before beginning the asynchronous script execution.
Closes #406. Closes #85.
I've run your test locally (with a different page, but otherwise the same code):
Happy testing!

Karma coverage always shows 100%

My karma.conf.js files looks like this :
// Karma configuration
// Generated on Tue Jun 11 2013 14:14:12 GMT+0100 (GMT Daylight Time)
// base path, that will be used to resolve files and exclude
basePath = '';
// list of files / patterns to load in the browser
files = [
JASMINE,
JASMINE_ADAPTER,
'../Scripts/angular/angular.js',
'../Scripts/angular/restangular/underscore-min.js',
'../Scripts/angular/restangular/restangular-min.js',
'../Scripts/angular/angular-*.js',
'../Scripts/angular/angular-test/angular-*.js',
'../Scripts/angular/angular-ui/*.js',
'../Scripts/angular/angular-strap/*.js',
'../Scripts/angular/angular-http-auth/*.js',
'../uifw/scripts/ui-framework-angular.js',
'../app/app.js',
'../app/**/*.js',
'unit/**/*.js'
];
// list of files to exclude
exclude = [
'../Scripts/angular/angular-test/angular-scenario.js'
];
preprocessors = {
'**/../app/**/*.js': 'coverage'
};
coverageReporter = {
type: 'html',
dir: 'coverage/'
};
// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters = ['progress', 'coverage'];
// web server port
port = 9876;
// cli runner port
runnerPort = 9100;
// enable / disable colors in the output (reporters and logs)
colors = true;
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_DEBUG;
// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = ['Chrome'];
// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;
My folder structure looks like this :
Root
|__ App
|__ Scripts
|__ Tests
|__ .... other folders
Karma.conf.js is located inside the tests folder. Karma start karma.conf.j is run from within the tests folder.
My tests run and a coverage folder is created, but coverage always shows 100%.
What am I doing wrong?
EDIT:
In fact it turned out to be a simple answer. the preprocessors = {
'**/../app/**/*.js': 'coverage'
};
no longer needs to be prefixed with **
See this for more details
Set up webpack with istanbul-instrumenter-loader got me on the right track.
{
test: /\.ts/,
include: helpers.root('src', 'app'),
loader: 'istanbul-instrumenter-loader',
enforce: 'post'
}
You need to set ANGULAR_SCENARIO_ADAPTER. Karma doesn't use JASMINE_ADAPTER.
To me it seems like your problem is that you are unable to find any of the files you are trying to generate coverage for. Your report does say that 0/0 lines are ran, 0/0 statements and so on.
I had a similar problem that was caused by me using lower case letters instead of upper case ones and it seems you might have the same problem as you have a folder named "App" but you reference it with "app" in the config. See if you can get it to work by writing **/../App/**/*.js': 'coverage'
Also I am not sure but I think you should write ../App/**/*.js': 'coverage' instead of **/../App/**/*.js': 'coverage'

Resources