How to test code that uses browsers APIs

249
June 17, 2017, at 2:32 PM

I'm using Node for running my unit-tests.

I have a JavaScript module run in the browser I'd like to test.

My code is "isomorphic", i.e. it avoids language features not available in Node, like exports.

But it uses pure browsers APIs: XMLHttpRequest, FormData and File.
I have found Node's implementations for each of them.
But the one of XMLHttpRequest does not support upload.

So I'm looking for the simplest way to unit-test this code in an environment with these APIs.
The code does not need DOM or other browsers APIs, "only" these three.

I've already used PhantomJS for other needs but:

  • this will create another test workflow (minor issue),
  • it supports an older JavaScript version and it would force a complete rewrite of the code to test (major issue),
  • the code has a lot of NPM dependencies that probably won't be compatible (blocking issue).

As the code is Browserified all these issues may disappear but before going along this way I'd like to be sure.

Is there any chance to get it work with PhantomJS, CasperJS or the like?

Which other alternatives are available?

Answer 1

This is not how you test code that runs in a browser. If it runs in a browser, it needs to be tested in a browser.

You need to look into solutions based on the webdriver spec. The big hairy monster in this ecosystem is Selenium. I'm currently researching this topic because of some issues we've had with using selenium-server. You should also look into Nightwatch and Leadfoot. Webdriver.io is the first recommendation a lot of people recommend, as its a node-based client that wraps (poorly) around Selenium. But the documentation is all over the place and we've run into frequent bugs using it.

Rent Charter Buses Company
READ ALSO
How to get 2 textboxes input value result into the 3rd textbox without hit submit button on the same page in PHP+MySQL by using JavaScript or JQuery?

How to get 2 textboxes input value result into the 3rd textbox without hit submit button on the same page in PHP+MySQL by using JavaScript or JQuery?

How I can get the result in net amount text box without a click on the submit button? (JQuery and JS solutions are both fine to me)

260
inserting date in to mysql but current date is stored

inserting date in to mysql but current date is stored

I have an Android app and I write a web services using PHP and my database is MYSQL databaseThere is a form in android to collect the information about uses and save it into the database using my php service

301
Implementing Tensor-Flow + python on other project?

Implementing Tensor-Flow + python on other project?

I've been using tensor-flow for the past several months learning how to classify a basic flower using scikit-learn and adding in new data labels for flowers such as tulip, sun-flower, rose, and features for example it's color to define it's species with the data that I've input for it's features...

307
“checkout as maven” option missing in java eclipse sts

“checkout as maven” option missing in java eclipse sts

Added all the connectors needed in the eclipse marketplace like m2ealso added maven to STS

264