Javascript unit testing with Jasmine and JSP - javascript

I am working on a project that uses JSP and Javascript. Now I have to write unit tests so I started using Jasmine for it and since the project is built using Maven, My goal was to run unit tests as part of continuous build and integration. So I thought of using jasmine-maven-plugin it uses PhantomJS for headless testing. I tried PhantomJS API to see whether it can load a JSP based webpage. Using the sample api I was able to create a screenshot of my JSP page. So I think PhantomJS can work with JSP.
My unit tests are heavily dependent on the JSP page but I was to not able to configure the Jasmine specRunner to use JSP, so that I can run my Jasmine tests as part of maven build.
Does anybody used Jasmine, Javascript, JSP combination for unit testing? Any help or comments will be highly appreciated. Thanks!

Related

How to test client-side javascript in an ASP.NET MVC application?

Project Context
I've got an ASP.NET MVC 5 and ASP.NET Web API 2.0 combined web application (i.e. both in the same project). There are pages that I have created where I do custom SPA: Handlebars.js for html templating, Routie.js for hash routing, and the rest custom JavaScript.
How do I go about unit testing my client-side javascript?
I could write functions that I manually kick off that does the action and checks if it succeeded. This has worked for me, but is still very manual. Any searches for how to automate testing of this code leads me to things like jest.js and such which seem to require npm, which to my understanding requires a node.js project.
I explored the node.js project route a little bit and got jest working for tests, but still lacked the understanding of how to use this project to test my client-side javascript and/or reference the code from the main project.
Question
How do I go about unit testing (preferably automating such tests; e.g. Azure DevOps) in an ASP.NET MVC 5 / Web Api 2.0 project?
If I need to create a node.js project with a test runner like Jest, how do I reference my client-side code from the main project to be tested?
I conceptually know how to write tests, I'm just running into problems understanding how to get the environment setup to do so. I have my C# unit tests setup to automatically run with my Azure DevOps pipeline, I'm just missing on how to get something setup to test my client-side JavaScript.

How to test a web page from my local machine after it is loaded in my browser

I have a website that is running on some server, which is used by a few thousand people. It's built on Java and soy template. I need to test the fronted rendering/js files. Can I do this from my local machine after accessing the web page?
for example: say I need to run a few Javascript test files on facebook.com. I go to facebook.com in my browser, after it is loaded, I need to run a few js files to test it. Is it possible? if yes, should I use Mocha or jest or any other alternate framework?
Only you can do is E2E testing. E2E testing is an UI automation testing. Here are the e2e testing framework choices:
puppeteer
cypress
casperjs
protractor
You can use jestjs with puppeteer.
There are different test types that can be run against code: Unit, Integration, Functional, etc. Some types have different test methodologies: TDD, BDD, etc. In this instance it seems you are wanting Functional Tests usually using the BDD method. In regard to your post, you state Java but tag the post Javascript and mention the Mocha framework. I'm assuming you meant Javascript instead of Java, with that said Mocha does support Functional Tests:
https://mochajs.org/#retry-tests
You will need the Selenium Driver to be installed along with other specific web browser drivers: Chrome, Edge, Gecko. A quick search shows that there might be an easier path to get up and running:
https://www.npmjs.com/package/mocha-webdriver
I have no experience with that library but leave further investigation up to you.

How to test a front-end JavaScript library and integrate with Travis?

I have already experimented testing NodeJS libraries using Jasmine or Mocha, but I don't know how to test front-end projects. I have found tutorials online, but everything includes a task manager in the workflow and I would like to know how to do this without one.
I found the following question close to what I am asking:
Using Travis-CI for client-side JavaScript libraries?
In my case, I am using Jasmine and have already set up the Jasmine SpecRunner.html, Jasmine library and spec/mylibSpec.js. The tests pass when I run the SpecRunner.html on my browser.
Now, how do I integrate this with Travis, without Grunt/Gulp/Brunch/etc?
I have heard the words "PhantomJS" and "Selenium" and I think this has to do with what I am trying to accomplish. Is there a "hello, world"-like project with tests and Travis integration one can learn from?
The Travis documentation lists three basic ways to accomplish this:
the PhantomJS headless browser
running Firefox with a virtual display or
using the Saucelabs browser VM service
Testing with PhantomJS is the fastest, since it does not simulate a display (it still allows you to create screenshots, though). PhantomJS comes with a run-jasmine example.
The phantom test script can then be executed directly, simply by running
script: phantomjs run-jasmine.js
in your .travis.yml, without the additional overhead of a build system such as Grunt.
If testing your project requires a real browser GUI, that leaves you with options 2 or 3.
Saucelabs browser VMs have the advantage of real cross-browser testing; if your project is open source, they offer a free plan. They also provide an in-depth tutorial for your specific use case: Travis + Jasmine + Saucelabs, which however does require Grunt in order to run.

Test driven JavaScript, Jasmine, and production code

I'm trying to grasp the basics of Jasmine (and BDD/TDD I guess). The examples I have seen does not resemble any realistic scenario of a web application, and have a hard time relating to it.
Are Jasmine tests done aside (separately) from working on the JavaScript that will be deployed? Manually copying tested/validated code.. Or does Jasmine compile to standard JS used for production?
Cheers
Jasmine is a framework for testing JavaScript code. Just like testing Ruby on Rails code, the tests don't become part of the production code. They are in the same repo and are run but they aren't minified into project.js or whatever your build process is. As part of your test run process, you can do headless tests using PhantomJS (headless webkit) and have it run on your CI server and so forth just like any other test.
I have worked on a bunch of projects this way. There has been a trend of seeing JavaScript has an enhancement layer that doesn't really need to be tested but today JavaScript is so much more. It is critical to test it if your application needs to work.

How to Maven JUnit-test HTML pages containing Javascript and calling a servlet?

I am still new to Javascript and Servlets. I have some HTML pages containing javascript code and these pages can call a servlet. I want to develop JUnit-like test for this page to run from Maven.
I googled and found JSUnit to test Javascript and HttpUnit to test Servlets, but how can I test my HTML page by simulating clicks and user actions? Is there a framework to write these tests in a Java/JUnit fashion? Basically, I want to automate these as part of a maven compilation process.
Have you looked at Selenium and Watir? Selenium can capture the clicks and then be put into a jUnit framework to run like it was unit tests. Watir does the same type of thing in Ruby.
I use a combination of JWebUnit as the driver and easyb for writing specifications.
JWebUnit will drive either HtmlUnit or Selenium. This is both good and bad; good because the behavioral tests can run on a headless server via HtmlUnit, bad because I lose some functionality of HtmlUnit in the process... some tests "go native". Pretty good JavaScript support, too.

Categories

Resources