Integration of Maven and Mocha Test Suite
Ganesha Swaroop B
|17+ yrs exp Software Testing|Author| Mentor|Staff SDET|Technical Writer|Technology Reasearcher|Java|Pytest|Python|Allure|ExtentReports|BDD|Jenkins|SME|Self Taught Data Science and ML Engineer
Install the required dependencies:
npm install mocha mocha-awesome mocha-junit-reporter selenium-webdriver
Create a directory structure for your project:
markdown
- project
?- src
??- test
???- java
????- MyTestSuite.java
???- resources
????- chromedriver.exe
?- pom.xml
Create a Mocha test suite file MyTestSuite.js:
const assert = require('assert');
const { Builder, By, Key, until } = require('selenium-webdriver');
describe('My Test Suite', function() {
?let driver;
?before(async function() {
??driver = await new Builder().forBrowser('chrome').build();
?});
?after(async function() {
??await driver.quit();
?});
?it('Test 1: Verify Title', async function() {
??await driver.get('https://www.example.com');
??const title = await driver.getTitle();
??assert.strictEqual(title, 'Example Domain');
?});
?it('Test 2: Verify Login', async function() {
??await driver.get('https://www.example.com/login');
??await driver.findElement(By.id('username')).sendKeys('testuser');
??await driver.findElement(By.id('password')).sendKeys('password');
??await driver.findElement(By.id('loginButton')).click();
??const welcomeMessage = await driver.findElement(By.id('welcomeMessage')).getText();
??assert.strictEqual(welcomeMessage, 'Welcome, testuser!');
?});
?it('Test 3: Verify Search', async function() {
??await driver.get('https://www.example.com');
??await driver.findElement(By.id('searchInput')).sendKeys('example');
??await driver.findElement(By.id('searchButton')).click();
??const searchResults = await driver.findElement(By.id('searchResults')).getText();
??assert.strictEqual(searchResults, 'Showing results for: example');
?});
});
How to execute the above from pom.xml file
To execute the above Mocha test suite from the pom.xml file using Maven, you can use the maven-surefire-plugin and configure it to run the Mocha tests. Here's an updated version of the pom.xml file:
xml
<project>
?<groupId>com.example</groupId>
?<artifactId>selenium-tests</artifactId>
?<version>1.0</version>
??
?<properties>
??<mocha.reporter>mocha-awesome</mocha.reporter>
?</properties>
??
?<dependencies>
领英推荐
??<!-- Add Selenium and Mocha dependencies -->
??<dependency>
???<groupId>org.seleniumhq.selenium</groupId>
???<artifactId>selenium-java</artifactId>
???<version>3.141.59</version>
??</dependency>
??<dependency>
???<groupId>mochajs</groupId>
???<artifactId>mocha</artifactId>
???<version>8.4.0</version>
??</dependency>
??<dependency>
???<groupId>mocha</groupId>
???<artifactId>mocha-junit-reporter</artifactId>
???<version>2.0.0</version>
??</dependency>
?</dependencies>
??
?<build>
??<plugins>
???<!-- Configure the Maven Surefire Plugin to run Mocha tests -->
???<plugin>
????<groupId>org.apache.maven.plugins</groupId>
????<artifactId>maven-surefire-plugin</artifactId>
????<version>3.0.0-M5</version>
????<executions>
?????<execution>
??????<id>mocha-tests</id>
??????<phase>test</phase>
??????<goals>
???????<goal>test</goal>
??????</goals>
??????<configuration>
???????<testSourceDirectory>src/test/java</testSourceDirectory>
???????<includes>
????????<include>**/*TestSuite.js</include>
???????</includes>
???????<systemProperties>
????????<property>
?????????<name>mocha.reporter</name>
?????????<value>${mocha.reporter}</value>
????????</property>
???????</systemProperties>
??????</configuration>
?????</execution>
????</executions>
???</plugin>
??</plugins>
?</build>
</project>
With this configuration, Maven will look for JavaScript test files with the pattern *TestSuite.js in the src/test/java directory. It will execute these Mocha tests during the test phase.
To run the tests using Maven, you can execute the following command:
mvn test
Maven will then execute the Mocha tests defined in the MyTestSuite.js file using the specified mocha-awesome reporter, and generate the test report in the desired format.
Try this and let me know if this works!!!!
Thanks,
Swaroop