nitro-automated-testing
v2.0.0
Published
Nitro Automated Testing Framework
Downloads
19
Maintainers
Readme
Nitro Automated Testing Framework
Nitro Automated Testing Framework is build on top of the Selenium JS, which is simplified by creating tests using a JSON
object file and report templates to define the test workflow. Also can be split a workflow into different small modules.
Steps to create a Test
- Add elements and actions
tests/data/signup.data.js
import { By, until } from 'selenium-webdriver';
export const signupTest = {
url: "signup",
delay: {
// To Add a delay of 3 seconds before start the test
beforeLoad: 3,
// To Add a delay of 3 seconds after the form submission
afterSubmission: 3
},
tests: {
elements: {
controls: [
{
// Type of Element
type: "input",
// CSS selector to identify the HTML element
selector: "[name=email]",
// Action to be performed
action: "sendKeys",
// Value to fill
value: "[email protected]"
},
{
type: "textarea",
selector: "#message",
action: "sendKeys",
value: `Lorem ipsum dolor sit amet`
},
{
type: "checkbox",
selector: "#agree",
action: "click",
value: true
},
{
type: "radio",
selector: "[name=gender][value=male]",
action: "click",
value: true
},
{
type: "select",
selector: "#country",
action: "selectByValue",
value: "US"
}
],
// Define the Submit Button
submit: "[name=submit]",
wait: async (driver) => {
// Optional Custom Wait Function, (Only if required)
await driver.wait(async () => {
const bodyClass = await driver.findElement(By.css('body')).getAttribute('class');
return !bodyClass.includes('onloading');
}, 3000, 'The body still has the onloading class');
}
}
},
validator: async (driver) => {
// Wait for the success or error message to be displayed and be visible
let currentUrl = await driver.getCurrentUrl();
let messageElement = await driver.wait(until.elementLocated(By.css('#output')), 3000);
await driver.wait(until.elementIsVisible(messageElement), 3000);
// let title = await driver.getTitle();
// Verify the message content
return await messageElement.getText().then((text) => {
let message = text;
console.log('Message Text:', message); // Debugging output
let result = (message.includes('Success')) ? 'Signup successful' : 'Signup failed';
// Data that are required to show in Report
return {
result,
message,
currentUrl,
timestamp: new Date().toISOString()
};
});
}
};
- Create a template for the Report
tests/template/signup.report.js
import { nitroTest } from "nitro-automated-testing";
export const getSignupReport = async (ob) => {
return `
# Signup Test Report
**Browser:** ${(nitroTest.Browser[process.env.BROWSER] || nitroTest.Browser.CHROME).toUpperCase()}
**Current URL:** ${ob.currentUrl}
**Result:** ${ob.result}
**Message:** ${ob.message}
**Timestamp:** ${ob.timestamp}
`;
}
- Create Signup Function, if needed we can add custom Selenium JS scripts
tests/signup.js
// tests/signup.js
import { signupTest } from './data/signup.data.js';
import { nitroTest } from "nitro-automated-testing";
export const signup = async (driver) => {
await driver.get(`${process.env.protocol}://${process.env.domain}${process.env.port ? `:${process.env.port}` : ''}/${signupTest.url}`);
// Wait for 3 seconds before proceeding
await nitroTest.sleep(signupTest.delay.beforeLoad * 1000);
// Interact with the form
await nitroTest.automateData(driver, signupTest);
const output = await signupTest.validator(driver);
return output;
};
- Create the main
test.js
file which will be entry point js file which can be used for import various test cases and workflows:
tests/test.js
// main test script
import path from 'path';
import { fileURLToPath } from 'url';
import { signup } from './signup.js';
import { nitroTest } from "nitro-automated-testing";
import { getSignupReport } from './template/signup.report.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
(async function globalTests() {
// Get the desired browser from environment variable or use default
let driver = await nitroTest.getDriver();
try {
console.log(`👉️ ${process.env.protocol}://${process.env.domain}${process.env.port ? `:${process.env.port}` : ''}`);
await driver.get(`${process.env.protocol}://${process.env.domain}${process.env.port ? `:${process.env.port}` : ''}`);
// Perform the signup process
const signupResult = await signup(driver);
// Generate the report content
const reportContent = await getSignupReport(signupResult);
await nitroTest.saveReport(path.join(__dirname, './', 'test', 'reports'), reportContent);
} finally {
await driver.quit();
}
})();