How to Configure Mochawesome HTML Reporter in an Appium JavaScript Framework

How to Configure Mochawesome HTML Reporter in an Appium JavaScript Framework

When you are writing automation tests, it is important after the test execution to generate detailed and easy-to-understand reports. Mochawesome is a reporter for Mocha and can help you by providing an HTML report that can help you analyze your test results efficiently. In this post, I’ll guide you through configuring Mochawesome Reporter in an Appium JavaScript framework.

Before we install and configure the Mochawesome reporter, ensure you have installed and configured Node.js, npm, and a working Appium JavaScript framework set up using Mocha as the test runner.

Install Mochawesome in your project

  • npm install --save-dev mochawesome – This command is used to install Mochawesome in your project.

After running this command, you can check if Mochawesome is installed.

You can do it by checking the package.json file. In the devDependencies block you should see something like this:

Or you can run npm list mochawesome in the terminal. If the Mochawesome reporter is successfully installed you should see its version. If not, this will be empty.

Run the tests and generate the report

Now when the Mochawesome reporter is installed, you can run the tests and create a Mochawesome report by using the following command:

npx mocha --reporter mochawesome ./tests"

I am writing “./tests” at the end because my test files are in my project’s “tests” folder.

or you can define this command in the package.json file like this:

"scripts": {
  "test": "mocha --reporter mochawesome ./tests"
}

And then you can run the tests simply by calling the npm test command in your terminal.

When the test execution is complete, a mochawesome-report folder will be automatically created in your project root folder, where you can find the HTML and JSON files of your report.

Open the HTML report file in your browser and you should see a report similar to this:

Custom Reporter Configuration

If you want to customize your report, you can do it directly in the package.json file.

For example, if you want the reports to be stored in your custom folder, and each report to have a different name than the default one, you can do it by adding the --reporter-options, and the following parameters to the test command in the package.json file:

  • reportDir: Specifies the directory where the report will be generated.
  • reportFilename: Specifies the name of the report file.

So the command will now look like this:

"scripts": {
  "test": "mocha --reporter mochawesome --reporter-options reportDir=reports, reportFilename=report ./tests"
}

Now, when you run the npm test command in the terminal, there should be a new reports folder automatically created in your project, where the new report is stored. If you open the folder, you should see that the reports have the names report.html and report.json.

To differentiate the reports generated from different test runs, adding a timestamp to the report filename is a good practice.

You can do it by defining the timestamp format in the test command in the package.json file, and adding that timestamp to the filename, like this:

"scripts": {
  "test": "timestamp=$(date +%d-%m-%Y-%H-%M-%S) && mocha --reporter mochawesome --reporter-options reportDir=reports, reportFilename=report-$timestamp ./tests"
}

Now, when the test execution is completed, you should see that the new report has the defined timestamp.

Other Reporter Options

In the following table, you can see all the possible Mochawesome reporter-options, including their descriptions, default values, and example usage. You can customize your report to fit best to your needs.

OptionDescriptionDefault ValueExample Usage
reportDirDirectory where the reports will be generatedmochawesome-reportreportDir=reports
reportFilenameName of the report filemochawesomereportFilename=TestReport
overwriteOverwrites existing reports if set to truetrueoverwrite=false
htmlEnables HTML report generationtruehtml=true
jsonEnables JSON report generationtruejson=true
quietSuppresses log messages in the consolefalsequiet=true
codeIncludes code snippets for failed tests in the reporttruecode=false
timestampAdds a timestamp to the report filenamefalsetimestamp=true
reportPageTitleSets a custom title for the HTML report pageMochawesome ReportreportPageTitle="My Custom Report"
reportTitleSets the title displayed within the reportMochawesomereportTitle="My Test Suite Results"
autoOpenAutomatically opens the report in the browser after testsfalseautoOpen=true
inlineAssetsInlines CSS and JS assets into the HTML filefalseinlineAssets=true
chartsIncludes charts in the reporttruecharts=true
saveJsonSaves the JSON output of the report to a filefalsesaveJson=true
devOutputs reporter options to the console for debuggingfalsedev=true

With the steps that are described above, you should be able to setup a well-organized reporting system that improves visibility and helps in analyzing test results.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *