Sunday 28 May 2017

Why https proxy does not work for nodejs selenium-webdriver?

I make selenium tests for website.

For some tests I need to replace backend responses to check website behavior in different conditions. I decided to use proxy for replacing responses and made simple server on Node.js/Express.

When I run my tests all http traffic is proxies successfully, but https. Http requests from tests generates log messages with requested urls.

When I try to make requests to localhost with port of http and https proxies manually from browser, I see urls in console too. But requests to https from test does not generate messages to console and does not download any data.

My proxy implementation:

const app = require('express')();
const proxy = require('express-http-proxy');
const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');

const httpsOptions = {
    key: fs.readFileSync(path.resolve(__dirname, '../../ssl/site.key')),
    cert: fs.readFileSync(path.resolve(__dirname, '../../ssl/site.crt'))
};

app.use((req, res, next) => {
    console.log('req.url', req.url);
    next();
});

app.use((req, res) => {
    proxy(req.url)(req, res);
});

const httpServer = http.createServer(app);
const httpsServer = https.createServer(httpsOptions, app);

httpServer.listen(0); // listen random free port
httpsServer.listen(0); // listen random free port

module.exports = {
    http: httpServer,
    https: httpsServer
};

And how I use it for selenium tests:

const webdriver = require('selenium-webdriver');
const proxy = require('selenium-webdriver/proxy');
const { http: proxyHttp, https: proxyHttps } = require('./helpers/proxy');

const driverBuilder = new webdriver.Builder().forBrowser('chrome');
driverBuilder.getCapabilities().set(webdriver.Capability.SECURE_SSL, true); // tried with this option and without it
driverBuilder.setProxy(proxy.manual({
    http: `http://127.0.0.1:${proxyHttp.address().port}`,
    https: `https://127.0.0.1:${proxyHttps.address().port}`
}));

const driver = driverBuilder.build();

If I remove https option in driverBuilder.setProxy, test works, but https traffic is not going via proxy.



via Pasha

No comments:

Post a Comment