Skip to content

Commit

Permalink
Integrating mongodb with playwright testing
Browse files Browse the repository at this point in the history
  • Loading branch information
BakkappaN committed Sep 11, 2024
1 parent e1affdd commit 48d3d8b
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 4 deletions.
8 changes: 7 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ MYSQL_HOST=
MYSQL_PORT=
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_DATABASE=
MYSQL_DATABASE=

# MongoDB
MONGODB_DATABASE=
MONGODB_COLLECTION=
MONGODB_URL=mongodb://127.0.0.1:27017

136 changes: 136 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
"description": "",
"main": "index.js",
"scripts": {
"tests": "npx playwright test --config=playwright.config.js --project=chromium"
"tests": "npx playwright test",
"ParaBank:Tests":"npx playwright test --grep ParaBankTest",
"SQLDB:Tests":"npx playwright test --grep SQLDataBaseTest",
"MongoDB:Tests":"npx playwright test --grep MongoDBTest",
"UI:Tests":"npx playwright test --grep UITest",
"API:Tests":"npx playwright test --grep APITest",
"Mobile:Tests":"npx playwright test --grep MobileTest"
},
"keywords": [],
"author": "",
Expand All @@ -20,6 +26,7 @@
"csv-parse": "^5.5.3",
"dotenv": "^16.4.1",
"luxon": "^3.4.4",
"mongodb": "^6.8.1",
"mysql2": "^3.11.0",
"otplib": "^12.0.1",
"xlsx": "^0.18.5"
Expand Down
4 changes: 2 additions & 2 deletions tests-e2e/databasetest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test.afterAll('Running after all tests', async () => {
/**
* Bakkappa N
*/
test('Data Base Validation Test 1', { tag: '@DataBaseTest' }, async ({ }) => {
test('Data Base Validation Test 1', { tag: '@SQLDataBaseTest' }, async ({ }) => {
await test.step('Validate name from data base', async () => {

// Playwright actions
Expand All @@ -41,7 +41,7 @@ test('Data Base Validation Test 1', { tag: '@DataBaseTest' }, async ({ }) => {
/**
* Bakkappa N
*/
test('Data Base Validation Test 2', { tag: '@DataBaseTest' }, async ({ }) => {
test('Data Base Validation Test 2', { tag: '@SQLDataBaseTest' }, async ({ }) => {
await test.step('Validate email from data base', async () => {

// Playwright actions
Expand Down
109 changes: 109 additions & 0 deletions tests-e2e/mongodbtest.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Include playwright module
const { test, expect } = require('@playwright/test');

const { connect, close } = require('../utils/mongodb');

let db;
let collection;

test.beforeAll('Running before all tests', async () => {
db = await connect(); // Connect to MongoDB

// Read collection
collection = db.collection(process.env.MONGODB_COLLECTION);

// Insert data into MongoDB
await collection.insertOne({ name: 'Testers Talk', age: 30, email: 'testerstalk@example.com' });

// Insert multiple documents
await collection.insertMany([
{ name: "Testers Talk 2", age: 25, email: "testerstalk2@example.com" },
{ name: "Testers Talk 3", age: 35, email: "testerstalk3@example.com" }
])
});

test.afterAll('Running after all tests', async () => {
await collection.deleteMany({}); // Delete document
await close(); // Close MongoDB connection
});

/**
* Bakkappa N
*/
test('MongoDB Test 1 - Validate email', { tag: '@MongoDBTest' }, async ({ }) => {
await test.step('Validate email from MongoDB', async () => {

// Playwright actions

// Retrieve and log data from MongoDB
const documents = await collection.find({}).toArray();
console.log(documents);

// Read a particular record
const query = { name: 'Testers Talk' };
const record = await collection.findOne(query);

console.log('Record:', record);

// Example assertion (if relevant)
expect(record).not.toBeNull();
expect(record).toHaveProperty('email', 'testerstalk@example.com');

// Playwright actions

})
})

/**
* Bakkappa N
*/
test('MongoDB Test 2 - Validate name', { tag: '@MongoDBTest' }, async ({ }) => {
await test.step('Validate name from MongoDB', async () => {

// Playwright actions

// Retrieve and log data from MongoDB
const documents = await collection.find({}).toArray();
console.log(documents);

// Read a particular record
const query = { email: 'testerstalk@example.com' };
const record = await collection.findOne(query);

console.log('Record:', record);

// Example assertion (if relevant)
expect(record).not.toBeNull();
expect(record).toHaveProperty('name', 'Testers Talk');

// Playwright actions

})
})

/**
* Bakkappa N
*/
test('MongoDB Test 3 - Validate age', { tag: '@MongoDBTest' }, async ({ }) => {
await test.step('Validate age from MongoDB', async () => {

// Playwright actions

// Retrieve and log data from MongoDB
const documents = await collection.find({}).toArray();
console.log(documents);

// Read a particular record
const query = { name: 'Testers Talk' };
const record = await collection.findOne(query);

console.log('Record:', record);

// Example assertion (if relevant)
expect(record).not.toBeNull();
expect(record).toHaveProperty('age', 30);

// Playwright actions

})
})
20 changes: 20 additions & 0 deletions utils/mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { MongoClient } = require('mongodb');

let client;

async function connect() {
if (!client) {
client = new MongoClient(process.env.MONGODB_URL, { useNewUrlParser: true, useUnifiedTopology: true });
await client.connect();
}
return client.db(process.env.MONGODB_DATABASE);
}

async function close() {
if (client) {
await client.close();
client = null;
}
}

module.exports = { connect, close };

0 comments on commit 48d3d8b

Please sign in to comment.