How can I replace the server in Web Component Tester

↘锁芯ラ 提交于 2019-12-10 22:39:08

问题


I have a project set up based around the Polymer Starter Kit, which includes Web-Component-Tester

This project includes php server code which I would also like to test by writing tests to run in the browser which will utilise the PHP server code through Ajax Calls.

This implies replacing the server that Web Component Tester is using ONLY when testing server side code. I hope to make a separate gulp task for this.

Unfortunately, I don't understand the relationship between WCT, Selenium and what ever server is run currently. I can see that WCT command starts Selenium, but I can't find out what the web server is and how that is started. I suspect it is WCT, because there is configuration of the mapping of directories to urls, but other than that I haven't a clue, despite trying to read the code.

Can someone explain how I go about making it run its own server when testing the client, but relying on an already set up web server (nginx) when running the server. I can set nginx to run from local host, or an other domain if that is a way to choose a different configuration.

EDIT: I have now found that runner/webserver.js starts an express server, and that urls get mapped so the base directory for the test runner and the bower_components directory both get mapped to the /components url.

What is currently confusing me is in what circumstances this gets run. It appears that loading plugins somehow does it, but my understanding from reading the code for this is tenuous.


回答1:


The answer is that web component tester itself has a comment in the runner/config.js file.

In wct-conf.js, you can use registerHooks key into the Object that gets returned to add a function that does

  registerHooks: function(wct) {
    wct.hook('prepare:webserver', function(app, done) {
      var proxy = require('express-http-proxy');
      app.use('/api',
        proxy('pas.dev', {
          forwardPath: function(req, res) {
            return require('url').parse(req.url).path;
          }
        })
      );
      done();
    });

This register hook function allows you to provide a route (/api in my case) which this proxies to a server which can run the php scripts.



来源:https://stackoverflow.com/questions/33506850/how-can-i-replace-the-server-in-web-component-tester

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!