Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newbie to Selenium E2E with React

I created a boilerplate React project, packages.json has the usual suspects:

  • prestart
  • start
  • list
  • test
  • etc.

I am using Selenium for my E2E framework. I have the following test:

it('should launch a browser', () => {

    const By = webDriver.By;
    let driver = new webDriver.Builder()
      .forBrowser('chrome')
      .build();

    // verify Continue button exist on page
    driver.navigate().to('http://localhost:3000').then(() => driver.findElement(By.id('submitButton')).getAttribute('value'))
      .then(buttonValue => expect(buttonValue).toEqual('Continue'));

  });

If I do npm start, my site launches and my E2E launches an additional Chrome browser and navigate to my running site: localhost:3000. The test succeeds.

My question is, how do I run my E2E separately, without the need to my site side by side using npm start.

I am newbie to React and Selenium, in case I am missing a lot of information on this post, I apologize in advance.

like image 693
Pacman Avatar asked Jun 13 '17 17:06

Pacman


1 Answers

Well, since you didn't find the time to update the question information with the NPM "scripts" object, then I'll try to give it a shot in the dark.

First of all, due to your wording, I can interpret your question two ways:

  • a.) you want to run your E2E tests separately, w/o your server running (which is started via npm start);
  • b.) you want to run your E2E tests via npm start, without triggering your server from starting;

a.) If you want to run your scripts separately, seeing as you are using Mocha, then you can trigger them via: ./node_modules/.bin/mocha <pathToTests>/<testFile>.

Now, since you stated in your question that you're using npm test script, then that should be the best switch to bind your E2E tests execution to:

package.json (Scripts object):

"scripts": {
    "test": "mocha --reporter spec <pathToTests>/<testFile>",
    "start": "node <yourServerName>.js"
},

Please note that mocha <pathToTests>/<testFile> is equivalent to ./node_modules/.bin/mocha <pathToTests>/<testFile>, because NPM looks for binaries inside node_modules/.bin and when Mocha was installed, it installed it into this directory.

Note: Many packages have a bin, or .bin section, declaring scripts that can be called from NPM similar to Mocha. If you want to find out what other binaries you can run that way, just issue a ls node_modules/.bin.


b.) In this care, I think your issue might be due to NPM defaulting some script values based on package contents. Specifically, if you have a server.js file in the root of your package, then npm will default the start command to server.js.

So if you're starting your E2E tests via npm start, having this ("start": "mocha <pathToTests>/<testFile>") in your package.json and there is a server.js file in the root of your package, then npm will default the start command to node server.js.

In which case, you could either move your server script to another place in the project, or change the switch you're using to trigger the E2E tests (see section b.)).

Hope this solves your problem and if not, looking forward for that package.json "scripts" object so we can really see what's up. :)

Cheers!

like image 56
iamdanchiv Avatar answered Oct 04 '22 21:10

iamdanchiv