2012-08-01 11 views
5

के माध्यम से HTTP अनुरोधों को रूट करना मैं नोड.जेएस के साथ एक ककड़ी परीक्षण सेटअप करने की कोशिश कर रहा हूं जो किसी भी वेबसाइट का उपयोग करके किसी भी वेबसाइट का परीक्षण कर सकता है। आम तौर पर क्रॉस स्क्रिप्ट सुरक्षा सीमाओं के कारण iframe कोई नहीं है। हालांकि यह संभव था (मुझे यकीन है कि यह है। और मुझे विश्वास है कि आप समाधान के साथ आने के लिए) वेबसाइट को अनुरोधित यूआरएल के माध्यम से परीक्षण के लिए लक्षित करने के लिए प्राप्त करें ताकि एक विशिष्ट यूआरएल नाम का अनुरोध किया जा सके, ताकि आईफ्रेम परीक्षण लक्ष्य की एक प्रति के साथ लोड किया जाएगा। मूल रूप से केवल एक मानक node.js सर्वर जो req.url Akin पर एक पता अनुरोध राउटर के आधार पर विशिष्ट पृष्ठों को प्राप्त करता है।Node.js

यहां बिल्कुल ऐसा करने का मेरा प्रयास है। के माध्यम से परीक्षण पृष्ठ ला रहा है। यूआरएल काम करता है। लेकिन मुझे http सर्वर से कनेक्शन ऑब्जेक्ट में स्विच करने में समस्या आ रही है। क्या http सर्वर प्रतिक्रिया के साथ कनेक्शन को "फ़ीड" करने का कोई तरीका है?

पीएस। मैंने दो नोड.जेएस सर्वरों के साथ एक समाधान भी बनाया। नोड 1 ने परीक्षण लक्ष्य प्राप्त किया और इसे ककड़ी परीक्षण पृष्ठ के साथ मिलाकर। ककड़ी परीक्षण की मेजबानी नोड 2। यह समाधान काम कर रहा है। लेकिन यह उन वेबसाइटों पर समस्याएं पैदा करता है जहां जावास्क्रिप्ट नामकरण विवाद होते हैं। यही कारण है कि आईफ्रेम समाधान, जो इस समस्या को encapsulation द्वारा हल करता है, अधिक आकर्षक है।

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 धावक" है। इसका उद्देश्य * स्थानीय * नोड.जेएस अनुप्रयोग का परीक्षण करना है। हालांकि, इसे * रिमोट * एप पर फीचर सूट चलाने और लोड करने के लिए काफी आसान होना चाहिए। – jbpros

+0

के पास 'loadMyWebsiteToBeTestedWithCucumberJS' फ़ंक्शन नाम के लिए अपवर्त है –