2012-08-01 139 views
5

我正在尝试使用可以使用iframe测试任何网站的Node.js进行黄瓜测试设置。 由于跨脚本安全限制,通常iframe是不合格的。 但是,如果可能的话(我敢肯定它是这样的,而且我相信你会想出一个解决方案) 在请求特定的url名称时通过请求的url获取测试的目标网站,这样iframe将加载测试目标的副本。 基本上只是一个标准的node.js服务器,它基于类似于地址请求路由器的req.url 来获取特定的页面。通过Node.js路由http请求

这是我公然的尝试做到这一点。 通过提取测试页面。该网址的作品。 但我有一个问题,从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 + '...'); 

回答

3

好吧,毕竟它不是那么难。
作为node.js的新手,我必须意识到使用多个监听器的可能性。
阅读nodejitsu的功能帮助我解决了这个问题。 http://localhost:9788/myWebsiteToBeTestedWithCucumberJS 其中作为cucumber.js网站要求所有其他请求被处理:

下面的例子指定URL时,如下加载www.myWebsiteToBeTestedWithCucumberJS.com 。
希望这对其他node.js newcucumbers有意义。

var http = require('http'); 

var connect = require('connect'); 
var port = process.env.PORT || 9788; 

var server = http.createServer(function(req, webres) 
{ 
    var url = req.url; 
    console.log(url); 
    if(url == '/myWebsiteToBeTestedWithCucumberJS') 
    { 
     loadMyWebsiteToBeTestedWithCucumberJS(req, webres); 
    } 
    else 
    { 
     loadLocal(req, webres, url); 
    } 
}).on('error', function(e) 
{ 
     console.log("Got error: " + e.message); 
}); 
server.listen(port); 
console.log('Accepting connections on port ' + port + '...'); 

function loadMyWebsiteToBeTestedWithCucumberJS(req, webres) 
{ 
    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() 
     { 
       page = page.replace(/ href="\/\//g  , ' href="/'); 
       page = page.replace(/ src="\//g   , ' src="http://www.myWebsiteToBeTestedWithCucumberJS.com/'); 
       page = page.replace(/ data-src="\//g  , ' data-src="http://www.myWebsiteToBeTestedWithCucumberJS.com/'); 
       page = page.replace(/ href="\//g   , ' href="http://www.myWebsiteToBeTestedWithCucumberJS.com/'); 

       webres.write(page); 
       webres.end(''); 
     }); 
    }); 

} 

function loadLocal(req, webres, path) 
{ 
    console.log('Loading localhost'); 
    webres.writeHead(200, {'content-type': 'text/html, level=1'}); 
    var options = 
    { 
       host: 'localhost', 
       port: 9787, 
       path: 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() 
     { 
       webres.write(page); 
       webres.end(''); 
     }); 
    }); 
} 


// Cucumber site listening on port 9787 
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(9787); 
+0

作为一个方面说明,我可以推荐你看看Cukestall(https://github.com/jbpros/cukestall)。这是Cucumber.js潜在的官方“iframe亚军”。它旨在测试一个* local * Node.js应用程序。但是,在* remote *应用程序上运行和加载功能套件应该相当容易。 – jbpros 2012-08-02 07:31:53

+0

对“loadMyWebsiteToBeTestedWithCucumberJS”函数名称有一个赞成 – 2016-03-10 20:17:36