How can I replace the server in Web Component Test

2019-07-10 16:05发布

问题:

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.