我试图与Node.js的黄瓜测试设置,可以通过使用iframe测试任何网站。 通常情况下,iframe是一个没有去,因为交叉脚本安全限制。 但是,如果这是可能的(我敢肯定它是。我相信你拿出一个解决方案),以获取该网站是为通过请求的URL测试目标正在请求特定的URL名时,这样的iframe将装有测试目标的副本。 基本上只是获取基础上,req.url类似于一个地址请求路由器的特定页面标准的node.js服务器。
这是我明目张胆地这样做。 通过获取测试页。 该URL的作品。 但我有一个问题,从HTTP服务器的连接对象的切换。 有没有一种方法来“养活”与HTTP服务器响应的连接?
PS。 我还创建具有两个node.js的服务器中的溶液。 节点1中取出的测试目标和黄瓜测试页相混合。 节点2托管黄瓜试验。 该解决方案的工作。 但它创造在哪里JavaScript的命名冲突发生的网站的问题。 这就是为什么iframe解决方案,该解决了封装这个问题更吸引人。
var http = require('http');
var connect = require('connect');
var port = process.env.PORT || 8788;
var server = http.createServer(function(req, webres)
{
var url = req.url;
console.log(url);
if(url == '/myWebsiteToBeTestedWithCucumberJS')
{
// Load the web site to be tested "myWebsiteToBeTestedWithCucumberJS"
// And update the references
// Finaly write the page with the webres
// The page will appear to be hosted locally
console.log('Loading myWebsiteToBeTestedWithCucumberJS');
webres.writeHead(200, {'content-type': 'text/html, level=1'});
var options =
{
host: 'www.myWebsiteToBeTestedWithCucumberJS.com,
port: 80,
path: '/'
};
var page = '';
var req = http.get(options, function(res)
{
console.log("Got response: " + res.statusCode);
res.on('data', function(chunk)
{
page = page + chunk;
});
res.on('end', function()
{
// Change relative paths to absolute (actual web location where images, javascript and stylesheets is placed)
page = page.replace(/ href="\/\//g , ' href="/');
page = page.replace(/ src="\//g , ' src="www.myWebsiteToBeTestedWithCucumberJS.com');
page = page.replace(/ data-src="\//g , ' data-src="www.myWebsiteToBeTestedWithCucumberJS.com');
page = page.replace(/ href="\//g , ' href="www.myWebsiteToBeTestedWithCucumberJS.com');
webres.write(page);
webres.end('');
});
});
}
else
{
// Load any file from localhost:8788
// This is where the cucumber.js project files are hosted
var dirserver = connect.createServer();
var browserify = require('browserify');
var cukeBundle = browserify({
mount: '/cucumber.js',
require: ['cucumber-html', './lib/cucumber', 'gherkin/lib/gherkin/lexer/en'],
ignore: ['./cucumber/cli', 'connect']
});
dirserver.use(connect.static(__dirname));
dirserver.use(cukeBundle);
dirserver.listen(port);
}
}).on('error', function(e)
{
console.log("Got error: " + e.message);
});
server.listen(port);
console.log('Accepting connections on port ' + port + '...');