I know that Karma is a test runner for JS Unit Testing frameworks like Jasmine or Mocha. And PhantomJS provides headless browser for running Jasmine or Mocha Tests.
But, what is the difference between Karma and PhantomJS? Are they two competing tools, or do I use PhantomJS on top of Karma to run my unit tests without a browser?
PhantomJS has nothing to do with testing. In the unit testing scope it would become one of the target browsers.
PhantomJS allows you to run unit tests in a browser when a desktop environment doesn't exist.
Karma is a runner that provides the finished reports on how successful the tests where.
Jasmine is the library used to write unit tests.
So to clarify
Jasmine unit tests are run by Karma inside the browser PhantomJS.
It seems like you have somewhat already answered your own question, but I'll expand what you have mentioned.
Karma is a test running framework that is largely test framework language agnostic. It has a rich plugin ecosystem that allows you to heavily customize how, when, and why your tests run.
In order to test Javascript, we often need to test against an incarnation of the DOM. There are numerous plugins that allow you to wire into different browsers such as karma-chrome. These plugins bootstrap the required browser and execute your tests against the browser.
However, there are times when you want to run without a physical browser being installed on the target test box. this is where PhantomJS comes in. It is a headless browser that can be run without being installed on a target machine. It cannot replace Karma. If you want to describe it as a "competitor", it would be a competitor to IE, Firefox, Chrome, and Safari.
Related
I have written some tests for my website using selenium and javascript. I want to know the standard way of using this script in production. Locally I'm running chrome driver and testing my script. What I have tried is in start of my package.json I run my test node test.js && react-scripts start.What is the standard way of doing the same in production?
In case there is no any difference between running the tests on your local machine and the product environment there is no reason to use your tests differently on the production.
However, it is common to run the tests on production with Jenkins or other CI/CD tools on Unix server in headless mode or with Selenium Grid etc.
In this case you will have to adjust your tests for running with Selenium Grid or in headless mode respectively, to adjust them running on Unix etc.
All this depends on YOUR actual configuration, how YOU will use it.
There are multiple different ways of handling Selenium in production. For example, if you have an open source project, you may consider using GitHub Actions. Here's an example of a JavaScript Workflow from the Selenium Project: https://github.com/SeleniumHQ/selenium/actions/workflows/javascript.yml
That's probably a good place to start, since it is open source and you can see how they run tests. Once you've learned that, you can try out some of the other popular solutions out there if you want (Eg: Jenkins, Azure Pipelines, AWS, Google Cloud, CircleCI, GitLab, TravisCI, etc.)
There is no way for you to use selenium in production, considering that you need selenium in production, such as a crawler, what may be different are your dependencies, in development you will use the dependencies for testing, selenium will use the available driver, either in production or in development/testing.
Perhaps the arguments you use for the chosen browser may be different for the environments used (production, development, etc.), which doesn't make sense to me, because in the test you should reproduce the same scenario as in production.
From the online definition:
Karma: is a tool which spawns a web server that executes source code against test code for each of the browsers connected. The results of each test against each browser are examined and displayed via the command line to the developer.
Jasmine: is a development framework for testing js code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.
My question is, does Karma require Jasmine to run, does Karma depend on Jasmine since Jasmine is a framework and Karma is a tool which runs on that framework and runs the written tests?
I'm using both of them with my angular2 project.
Karma is client-side test runner and doesn't depend on Jasmine. It can run without any testing framework at all.
It has plugins for major testing frameworks, including Jasmine and Mocha.
Yes, Jasmine and Karma can co-exist.
Jasmine is a javascript based framework that we use to write unit
test cases. Alternative to jasmine is Mocha.
Karma is a test runner, which runs unit test cases on browser. And It can be used with all type of unit test frameworks. And It's easy to integrate with all CI tools like Bamboo and Jenkins.
I'm new to javascript unit testing. To learn karma + jasmine, I've been writing unit tests for a Chrome Extension project of mine. That has worked pretty well for the modules and functions which don't use the chrome object, but I have no idea how to test the extension parts of it.
For instance, I might want to set the proxy with chrome.proxy.settings.set(...), and then verify that it was successfully set with chrome.proxy.settings.get(...).
Is there a way to run tests from the extensions global scope? Or any other way to accomplish these tests.
For JavaScript projects, what asynchronous system and unit test framework would be most conducive to use in both node.js and web browsers.
Ideally, the testing system would be able to execute some tests specifically for node.js and some in web browsers, while also having general tests which run in all environments.
Also, does anyone make automatable browser tests?
There are a bunch of unittest frameworks for JavaScript:
Jasmine
BDD style
for both node and browser testing
can run via maven, so you can used with CI (Jenkins)
BusterJS
its in beta state
BDD style
for both node and browser testing
Generates JUnit/Ant compatible XML output for use in continuous integration servers (Jenkins)
JSTestDriver
Plugins for eclipse and IntelliJ/Webstorm
runs in the browser , result saved on the server
calc code coverage
We have a rich web client. Our controllers and service facades are written in coffeescript (JavaScript) and jquery. In the past they would have been java.
To run our JavaScript jasmine tests from Jenkins/Hudson, we use java's junit and htmlunit to load a test oriented jsp page which includes the jasmine specs.
When the Htmlunit tries to run, it blows up trying to getPage() probably because of an XML parser class path which is extremely challenging to track down in our world.
We just want to be able to run our JavaScript tests from Jenkins and have it report failure if a JavaScript test does not pass. We are just using jsp and htmlunit in order to run JavaScript tests. Can we load the JavaScript tests and javascript code into a JavaScript engine with Jenkins as the thing that kicks it off? If so, how?
Sounds like you're in a Java environment. My jasmine-maven-plugin might be a good fit.
Jasmine Reporters would also be a solution. It has instructions for running headlessly via PhantomJS for example, and it can generate JUnit XML so Jenkins can understand the test results natively, graphing test count, duration, and failure over time.
Also, the "xvfb-run" wrapper often provided with xvfb is a great help here, so you can do "xvfb-run phantomjs.runner.sh ..." in a truly headless environment.
I've previously solved this problem by running the tests with a node.js plugin called jasmine-node
This solution of course requires node.js and a few node modules to properly run the jasmine tests. There is no real browser running the tests, but an emulated one using a module called jsdom, which basically creates a headless browser, and more specifically, a DOM, which the tests can interact with.
There's node modules for jQuery, underscore and propably other too, so these can be tested too. You can even skip the whole browser emulation if you'd rather run the tests in a browser, though I find it too cumbersome compared to automated Jenkins testing.
jasmine-node generates jUnit test reports, which Jenkins can interpret just fine.
I just realized there is some jenkins-jasmine-node plugin that might ease this process.
Grunt is your friend
use grunt http://gruntjs.com/
with grunt jasimine https://github.com/gruntjs/grunt-contrib-jasmine
with nodejs http://nodejs.org/
on jenkins using https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin
got this setup and it's really nice, plus this gives you a place to start making your build server do other nice things such as deployment, unit testing, etc you know, other nice things
Can you use selenium? That would actually use a real browser then and get as close to the real environment as possible.