Protractor proxy settings configuration is not passed to Saucelabs - proxy

To enable saucelabs proxy to work in older version of protractor, we were overriding sendRequest method by setting host and port in below index.js:
protractor\node_modules\selenium-webdriver\http\index.js
Now protractor allows you set the proxy through capabilities object (as shown below) which should be passed to index.js sendRequest new parameter called 'opt_proxy'.
capabilities: {
"browserName": "chrome",
'proxy': {
'proxyType': 'manual',
'httpProxy': 'appproxy.web.abc.com:84'
},
"chromeOptions": {
"args": [
"--disable-extensions",
"--test-type"
]
},
"customData": {
"usageBracket" : "1",
"displayName" : "Chrome",
"id" : "CH"
}
}
However, when I am still getting null for opt_proxy. Is there anything I am doing wrong? I even tried passing through CLI using --proxy="" but it still get null.

I have gotten my proxy configuration to work with Sauce Labs using the sauceAgent util provided within Protractor. Here is a code snippet from my protractor config file.
var HttpsProxyAgent = require("https-proxy-agent");
var agent = new HttpsProxyAgent('http://localhost:56193'); //Insert your proxy info here
exports.config = {
sauceUser: process.env.SAUCE_USERNAME,
sauceKey: process.env.SAUCE_ACCESS_KEY,
sauceAgent: agent,

capabilities: {
browserName: 'chrome',
chromeOptions: {
args: [
'--proxy-server=socks5://host:port',
]
},
},

Related

Configuration for log driver logfile

I've implemented nightwatchjs in my project and the start is there. However, something I don't like is that the chrome and gecko driver are placing a log file in my root directory. I'ld much prefer this to move to a logging location.
disable_error_log: true,
desiredCapabilities: {
silent: true,
browserName: 'firefox',
alwaysMatch: {
acceptInsecureCerts: true,
'moz:firefoxOptions': {
args: []
}
}
},
webdriver: {
start_process: true,
server_path: GeckoDriver.path,
cli_args: []
}
},
chrome: {
disable_error_log: true,
desiredCapabilities: {
silent: true,
browserName: 'chrome',
chromeOptions: {
args: []
}
},
webdriver: {
log_path: false,
start_process: true,
server_path: ChromeDriver.path,
cli_args: [
]
}
}
Right now the configuration is as above. Two questions here for the logging are:
wat setting do you use to turn it on or off
what setting do you use to change the location and or file name for the log
I'm running Nightwatch on a Mac machine and in my nightwatch.conf.js file I have a Selenium object that has a log_path property. The path currently is logs/ but I just tried to remove the path and put false as the path and that worked for me. If you want to turn it off, put false as the path for log_path, otherwise put the name of the directory (mine is logs/). As far as changing the name of the selenium-server.log file, I do not think you can change this, unless someone has create an npm module that gives the ability to extend Nightwatch.
selenium: {
"start_process": true,
"server_path": "bin/selenium-server-standalone-3.9.1.jar",
"log_path": false,
"port": 4444,
"cli_args": {
"webdriver.chrome.driver": this.chromePath
}
}
link for nightwatch docs
I also have the same issue. I solve this by setting value of log_path in webdriver object of each driver.
you can make changes as like:
chrome: {
disable_error_log: true,
desiredCapabilities: {
silent: true,
browserName: 'chrome',
chromeOptions: {
args: []
}
},
webdriver: {
start_process: true,
server_path: ChromeDriver.path,
log_path:'log_folder', // add this line to your every webdriver object.
cli_args: [
]
}
}
You can off your driverLog generation by setting log_path to false. If you want to specify some specific folder to store the driverLog file then you have to set the log_path to desired location.

How do I use protractor with firefox and local self-signed https?

I'm using protractor to test an application and webdriver-manager doesn't seem to want to install an appropriate Chrome driver, so I'm trying Firefox instead. However, the test gets stuck at the security warning page of my self-signed https:// test server. How do I configure protractor / selenium webdriver to skip the warning page?
Use this block in your protractor.conf.js to load Firefox, and ignore the certificate error presented by the test server's self-signed certificate. Note that if you're editing a Chrome config it's not enough to change browserName, you must remove any chromeOptions: { to avoid confusing the test runner.
capabilities: {
browserName: 'firefox',
'moz:firefoxOptions': {
// command line options could go here
},
'acceptInsecureCerts': true
},
In order to run protractor tests on multiple browsers, Protractor
offers a multiCapabilities configuration option. These options should
be defined as an array of objects.
1. Capabilities to be passed to the web driver instance.
capabilities: {
'browserName': 'firefox',
},
2. How to execute protractor tests on multiple browsers in parallel using multiCapabilities.
multiCapabilities: [
{'browserName': 'chrome'},
{'browserName': 'firefox'},
],
3. How we can do it using multiCapabilities.
multiCapabilities: [
{
'browserName': 'chrome',
'chromeOptions': {
'args': ['disable-infobars']
}
},
{
'browserName': 'firefox',
'moz:firefoxOptions': {
'args': ['--safe-mode']
}
}
],

How to run tests programmatically in Nightwatch.js?

I'm using Nightwatch.js and trying to run E2E tests using the programmatic API as described here.
Here is my nightwatch.json file:
{
"src_folders": ["tests"],
"webdriver": {
"start_process": true,
"server_path": "node_modules/.bin/chromedriver",
"port": 9515
},
"test_settings": {
"default": {
"desiredCapabilities": {
"browserName": "chrome"
}
}
}
}
and index.js script:
const Nightwatch = require('nightwatch');
Nightwatch.runTests(require('./nightwatch.json')).then(() => {
console.log('All tests has been passed!');
}).catch(err => {
console.log(err);
});
When I run the script I get the error:
Error: An error occurred while retrieving a new session: "Connection refused to 127.0.0.1:9515". If the Webdriver/Selenium service is managed by Nightwatch, check if "start_process" is set to "true".
I feel it needs some configuration but the documentation isn't very helpful here.

Change to desiredCapabilities in beforeEach not reflecting

I am trying to record all browser request and responses. This can be done via browsermob-proxy api's.
For this, I have to change desired capabilities and change httpProxy for browser.
In beforeEach at global or file level, I am trying to change this. Though it reflects in browser object, actual browser is not initiated with those settings.
Simple example:
globalhook file
module.exports = {
before : function (done) {
},
beforeEach: function(browser, done){
browser.options.desiredCapabilities = {
"browserName": "chrome",
"proxy": {
"proxyType": "manual",
"httpProxy": "127.0.0.1:" + someport,
"sslProxy": "127.0.0.1:" + someport
},
"javascriptEnabled": true,
"acceptSslCerts": true,
}
done()
},
afterEach: function(browser, done){
//some code
}
after : function (done) {
//some code
},
}
If i change desired capabilities in before hook, chrome browser is taking those changes. Problem is with beforeEach [global, file level].
Further debugging, I've found setCapabilities function is run just before beforeEach Hook.
Could anyone please have a look or suggest if I am doing something wrong.
Thanks for moving the issue here.
The issue is that the desiredCapabilities are applied to a browser session.
beforeEach and afterEach run on the same browser session therefore any change in the desiredCapabilities won't be applied unless the session is restarted.
If you need to dynamically change the desiredCapabilities you have to structure your tests in the different way, e.g. split your tests in separate test classes
First of all, you probably want to use before() hook instead of beforeEach(), since beforeEach() will run before each test case and you won't be able to modify already started browser session. before() will run before browser session is initiated.
To solve your issue you need to define desiredCapabilities property on your test run module and in case you need to set some dynamic values, use before() hook to modify that object:
module.exports = {
desiredCapabilities: {
"browserName": "chrome",
"proxy": {
"proxyType": "manual",
"httpProxy": "127.0.0.1",
"sslProxy": "127.0.0.1"
},
"javascriptEnabled": true,
"acceptSslCerts": true,
},
before: function(client, done) {
this.desiredCapabilities.proxy.sslProxy = '127.0.0.1' + someport;
done();
}
}
I've searched official docs on this feature but couldn't find anything, though we used this in our tests and it worked well.
Update:
Since you only need to change proxy on desiredCapabilities object, there is a little hack:
// global hooks file:
module.exports = {
beforeEach: function(client, done) {
client.options.desiredCapabilities.proxy = {
"proxyType": "manual",
"httpProxy": "127.0.0.1:" + someport,
"sslProxy": "127.0.0.1:" + someport
};
done(client);
}
};
Changing desiredCapabilities object won't work, since reference to original object is already stored somewhere under the hood of Nightwatch. But nothing stops you from overriding proxy property on that object.
After checking sources and tests in Nightwatch I believe this is the only solution for your case.

How do I use CloudFormation resources in a Lambda function?

I have added a Redis ElastiCache section to my s-resource-cf.json (a CloudFormation template), and selected its hostname as an output.
"Resources": {
...snip...
"Redis": {
"Type": "AWS::ElastiCache::CacheCluster",
"Properties": {
"AutoMinorVersionUpgrade": "true",
"AZMode": "single-az",
"CacheNodeType": "cache.t2.micro",
"Engine": "redis",
"EngineVersion": "2.8.24",
"NumCacheNodes": "1",
"PreferredAvailabilityZone": "eu-west-1a",
"PreferredMaintenanceWindow": "tue:00:30-tue:01:30",
"CacheSubnetGroupName": {
"Ref": "cachesubnetdefault"
},
"VpcSecurityGroupIds": [
{
"Fn::GetAtt": [
"sgdefault",
"GroupId"
]
}
]
}
}
},
"Outputs": {
"IamRoleArnLambda": {
"Description": "ARN of the lambda IAM role",
"Value": {
"Fn::GetAtt": [
"IamRoleLambda",
"Arn"
]
}
},
"RedisEndpointAddress": {
"Description": "Redis server host",
"Value": {
"Fn::GetAtt": [
"Redis",
"Address"
]
}
}
}
I can get CloudFormation to output the Redis server host when running sls resources deploy, but how can I access that output from within a Lambda function?
There is nothing in this starter project template that refers to that IamRoleArnLambda, which came with the example project. According to the docs, templates are only usable for project configuration, they are not accessible from Lambda functions:
Templates & Variables are for Configuration Only
Templates and variables are used for configuration of the project only. This information is not usable in your lambda functions. To set variables which can be used by your lambda functions, use environment variables.
So, then how do I set an environment variable to the hostname of the ElastiCache server after it has been created?
You can set environment variables in the environment section of a function's s-function.json file. Furthermore, if you want to prevent those variables from being put into version control (for example, if your code will be posted to a public GitHub repo), you can put them in the appropriate files in your _meta/variables directory and then reference those from your s-function.json files. Just make sure you add a _meta line to your .gitignore file.
For example, in my latest project I needed to connect to a Redis Cloud server, but didn't want to commit the connection details to version control. I put variables into my _meta/variables/s-variables-[stage]-[region].json file, like so:
{
"redisUrl": "...",
"redisPort": "...",
"redisPass": "..."
}
…and referenced the connection settings variables in that function's s-function.json file:
"environment": {
"REDIS_URL": "${redisUrl}",
"REDIS_PORT": "${redisPort}",
"REDIS_PASS": "${redisPass}"
}
I then put this redis.js file in my functions/lib directory:
module.exports = () => {
const redis = require('redis')
const jsonify = require('redis-jsonify')
const redisOptions = {
host: process.env.REDIS_URL,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASS
}
return jsonify(redis.createClient(redisOptions))
}
Then, in any function that needed to connect to that Redis database, I imported redis.js:
redis = require('../lib/redis')()
(For more details on my Serverless/Redis setup and some of the challenges I faced in getting it to work, see this question I posted yesterday.)
update
CloudFormation usage has been streamlined somewhat since that comment was posted in the issue tracker. I have submitted a documentation update to http://docs.serverless.com/docs/templates-variables, and posted a shortened version of my configuration in a gist.
It is possible to refer to a CloudFormation output in a s-function.json Lambda configuration file, in order to make those outputs available as environment variables.
s-resource-cf.json output section:
"Outputs": {
"redisHost": {
"Description": "Redis host URI",
"Value": {
"Fn::GetAtt": [
"RedisCluster",
"RedisEndpoint.Address"
]
}
}
}
s-function.json environment section:
"environment": {
"REDIS_HOST": "${redisHost}"
},
Usage in a Lambda function:
exports.handler = function(event, context) {
console.log("Redis host: ", process.env.REDIS_HOST);
};
old answer
Looks like a solution was found / implemented in the Serverless issue tracker (link). To quote HyperBrain:
CF Output variables
To have your lambda access the CF output variables you have to give it the cloudformation:describeStacks access rights in the lambda IAM role.
The CF.loadVars() promise will add all CF output variables to the process'
environment as SERVERLESS_CF_OutVar name. It will add a few ms to the
startup time of your lambda.
Change your lambda handler as follows:
// Require Serverless ENV vars
var ServerlessHelpers = require('serverless-helpers-js');
ServerlessHelpers.loadEnv();
// Require Logic
var lib = require('../lib');
// Lambda Handler
module.exports.handler = function(event, context) {
ServerlessHelpers.CF.loadVars()
.then(function() {
lib.respond(event, function(error, response) {
return context.done(error, response);
});
})
};

Resources