iframeを使用して任意のWebサイトをテストできるNode.jsでキュウリのテストセットアップを作成しようとしています。通常、クロス スクリプトのセキュリティ制限のため、iframe は使用できません。ただし、特定の url 名が要求されているときに、要求された url を介してテストのターゲットである web サイトを取得することが可能である場合 (私はそう確信しています。そして、私はあなたが解決策を考え出すことを信じています)、iframeテストターゲットのコピーがロードされます。基本的には、req.url に基づいて特定のページをフェッチする標準的な node.js サーバーにすぎません。アドレス リクエスト ルーターに似ています。
これがまさにそれを行うための私の露骨な試みです。経由でテストページを取得しています。URLは機能します。しかし、http サーバーから接続オブジェクトへの切り替えに問題があります。http サーバーの応答で接続を「フィード」する方法はありますか?
PS。また、2 つの node.js サーバーを使用してソリューションを作成しました。ノード 1 はテスト ターゲットをフェッチし、キュウリ テスト ページと混合します。キュウリ テストをホストするノード 2。このソリューションは機能しています。ただし、javascript の名前の競合が発生する Web サイトでは問題が発生します。そのため、カプセル化によってこの問題を解決する 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 + '...');