SIGN IN SIGN UP
SeleniumHQ / selenium UNCLAIMED

A browser automation framework and ecosystem.

0 0 252 Java
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
2013-06-26 22:16:09 -07:00
2020-08-03 17:56:31 +03:00
'use strict'
2013-06-26 22:16:09 -07:00
const assert = require('node:assert')
const { URL } = require('node:url')
const proxy = require('selenium-webdriver/proxy')
2020-08-03 17:56:31 +03:00
const test = require('../lib/test')
const { Browser } = require('selenium-webdriver')
2020-08-03 17:56:31 +03:00
const { Server } = require('../lib/test/httpserver')
2017-10-24 17:58:37 -07:00
2020-08-03 17:56:31 +03:00
test.suite(function (env) {
2013-06-26 22:16:09 -07:00
function writeResponse(res, body, encoding, contentType) {
res.writeHead(200, {
'Content-Length': Buffer.byteLength(body, encoding),
2020-08-03 17:56:31 +03:00
'Content-Type': contentType,
})
res.end(body)
2013-06-26 22:16:09 -07:00
}
function writePacFile(res) {
2020-08-03 17:56:31 +03:00
writeResponse(
res,
[
'function FindProxyForURL(url, host) {',
' if (shExpMatch(url, "' + goodbyeServer.url('*') + '")) {',
' return "DIRECT";',
' }',
' return "PROXY ' + proxyServer.host() + '";',
'}',
].join('\n'),
'ascii',
'application/x-javascript-config',
2020-08-03 17:56:31 +03:00
)
2013-06-26 22:16:09 -07:00
}
2020-08-03 17:56:31 +03:00
const proxyServer = new Server(function (req, res) {
const pathname = new URL(req.url).pathname
2013-06-26 22:16:09 -07:00
if (pathname === '/proxy.pac') {
2020-08-03 17:56:31 +03:00
return writePacFile(res)
2013-06-26 22:16:09 -07:00
}
2020-08-03 17:56:31 +03:00
writeResponse(
res,
['<!DOCTYPE html>', '<title>Proxy page</title>', '<h3>This is the proxy landing page</h3>'].join(''),
2020-08-03 17:56:31 +03:00
'utf8',
'text/html; charset=UTF-8',
2020-08-03 17:56:31 +03:00
)
})
const helloServer = new Server(function (_req, res) {
writeResponse(
res,
['<!DOCTYPE html>', '<title>Hello</title>', '<h3>Hello, world!</h3>'].join(''),
2020-08-03 17:56:31 +03:00
'utf8',
'text/html; charset=UTF-8',
2020-08-03 17:56:31 +03:00
)
})
const goodbyeServer = new Server(function (_req, res) {
writeResponse(
res,
['<!DOCTYPE html>', '<title>Goodbye</title>', '<h3>Goodbye, world!</h3>'].join(''),
2020-08-03 17:56:31 +03:00
'utf8',
'text/html; charset=UTF-8',
2020-08-03 17:56:31 +03:00
)
})
2013-06-26 22:16:09 -07:00
// Cannot pass start directly to mocha's before, as mocha will interpret the optional
// port parameter as an async callback parameter.
function mkStartFunc(server) {
2020-08-03 17:56:31 +03:00
return function () {
return server.start()
}
}
2020-08-03 17:56:31 +03:00
before(mkStartFunc(proxyServer))
before(mkStartFunc(helloServer))
before(mkStartFunc(goodbyeServer))
2013-06-26 22:16:09 -07:00
2020-08-03 17:56:31 +03:00
after(proxyServer.stop.bind(proxyServer))
after(helloServer.stop.bind(helloServer))
after(goodbyeServer.stop.bind(goodbyeServer))
2013-06-26 22:16:09 -07:00
2020-08-03 17:56:31 +03:00
let driver
beforeEach(function () {
driver = null
})
afterEach(function () {
return driver && driver.quit()
})
2013-06-26 22:16:09 -07:00
function createDriver(proxy) {
2020-08-03 17:56:31 +03:00
return (driver = env.builder().setProxy(proxy).build())
}
// Proxy support not implemented.
2020-08-03 17:56:31 +03:00
test
.ignore(env.browsers(Browser.CHROME, Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.FIREFOX))
2020-08-03 17:56:31 +03:00
.describe('manual proxy settings', function () {
it('can configure HTTP proxy host', async function () {
await createDriver(
proxy.manual({
http: proxyServer.host(),
bypass: [],
}),
2020-08-03 17:56:31 +03:00
)
await driver.get(helloServer.url())
assert.strictEqual(await driver.getTitle(), 'Proxy page')
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
2020-08-03 17:56:31 +03:00
})
it('can bypass proxy for specific hosts', async function () {
await createDriver(
proxy.manual({
http: proxyServer.host(),
bypass: [helloServer.host()],
}),
2020-08-03 17:56:31 +03:00
)
await driver.get(helloServer.url())
assert.strictEqual(await driver.getTitle(), 'Hello')
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'Hello, world!')
2020-08-03 17:56:31 +03:00
// For firefox the no proxy settings appear to match on hostname only.
let url = goodbyeServer.url().replace(/127\.0\.0\.1/, 'localhost')
await driver.get(url)
assert.strictEqual(await driver.getTitle(), 'Proxy page')
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
2020-08-03 17:56:31 +03:00
})
// TODO: test ftp and https proxies.
})
2013-06-26 22:16:09 -07:00
// PhantomJS does not support PAC file proxy configuration.
2013-11-29 21:16:00 -08:00
// Safari does not support proxies.
2020-08-03 17:56:31 +03:00
test
.ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.CHROME, Browser.FIREFOX))
2020-08-03 17:56:31 +03:00
.describe('pac proxy settings', function () {
it('can configure proxy through PAC file', async function () {
await createDriver(proxy.pac(proxyServer.url('/proxy.pac')))
await driver.get(helloServer.url())
assert.strictEqual(await driver.getTitle(), 'Proxy page')
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'This is the proxy landing page')
2020-08-03 17:56:31 +03:00
await driver.get(goodbyeServer.url())
assert.strictEqual(await driver.getTitle(), 'Goodbye')
assert.strictEqual(await driver.findElement({ tagName: 'h3' }).getText(), 'Goodbye, world!')
2020-08-03 17:56:31 +03:00
})
})
})