Skip to content

Commit

Permalink
Merge pull request #2 from nabim777/yogest-feature-file
Browse files Browse the repository at this point in the history
login feature and createResource feature
  • Loading branch information
Grisha596 authored Dec 29, 2023
2 parents c46742d + 4636c95 commit ade9b06
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 6 deletions.
22 changes: 16 additions & 6 deletions testHelper/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,29 @@ async function deleteFile(filename) {

}

async function createFile(filename) {
try {
const res= await axios.post(`http://localhost:8080/api/resources/${filename}`,{}, {
headers: {
"X-Auth": await getXauthToken()
},

});
} catch (error) {
console.error("Error creating file:", error);
}

}

async function cleanUpTempFiles() {
for (let i = 0; i < filesToDelete.length; i++) {
await deleteFile(filesToDelete[i])
}
}
function swapFileOnRename(oldfileName, newfileName) {
const fileToSwapIndex = filesToDelete.findIndex((file) => file == oldfileName);
filesToDelete[fileToSwapIndex] = newfileName;
}

module.exports ={
module.exports = {
deleteFile,
cleanUpTempFiles,
filesToDelete,
swapFileOnRename
createFile
}
50 changes: 50 additions & 0 deletions yogesh/tests/acceptance/PageObject/HomePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const util = require('util');
const { filesToDelete } = require('../../../../testHelper/helper.js');

class HomePage{

constructor(){
this.dialogInputSelector = '//input[@class="input input--block"]';
this.lastNavaigatedFolderSelector = '//div[@class="breadcrumbs"]/span[last()]/a'
this.contentEditorSelector = '//textarea[@class="ace_text-input"]'
this.editorContent = '//div[@class="ace_line"]'
this.buttonSelector =`//button[@title="%s"]`
this.fileSelector = `//div[@aria-label="%s"]`
this.cardActionSelector = '//div[@class="card-action"]/button[@title="%s"]'
}

async createNewFolder(folderName){
await page.click( util.format(this.buttonSelector,'New folder'))
await page.fill(this.dialogInputSelector, folderName)
await page.click(util.format(this.cardActionSelector,'Create'))
}

async createFileWithContent(filename, content){
await page.click(util.format(this.buttonSelector,'New file'))
await page.fill(this.dialogInputSelector, filename)
await page.click(util.format(this.cardActionSelector,'Create'))
await page.fill(this.contentEditorSelector, content)
await page.click( util.format(this.buttonSelector,'Save'))
await page.click(util.format(this.buttonSelector,'Close'))

//saving the file info into global array to delete later
filesToDelete.push(filename)
}

async renameFile(oldfileName,newfileName){
await page.click(util.format(this.fileSelector,oldfileName))
await page.click(util.format(this.buttonSelector,'Rename'))
await page.fill(this.dialogInputSelector, newfileName)
await page.click(util.format(this.cardActionSelector,'Rename'))
const fileToSwapIndex = filesToDelete.findIndex((file) => file == oldfileName);
filesToDelete[fileToSwapIndex] = newfileName;
}

async deleteFile(filename){
await page.click(util.format(this.fileSelector,filename))
await page.click(util.format(this.buttonSelector,'Delete'))
await page.click(util.format(this.cardActionSelector,'Delete'))
}
}

exports.HomePage = HomePage
35 changes: 35 additions & 0 deletions yogesh/tests/acceptance/PageObject/LoginPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class LoginPage {
constructor() {
this.usernameSelector = '//input[@type="text"]'
this.passwordSelector = '//input[@type="password"]'
this.loginButton = '//input[@type="submit"]'
this.wrongCredentialsDivSelector = '//div[@class="wrong"]'
this.baseURL = "http://localhost:8080/"
}

async navigateToLoginPage() {
await page.goto(this.baseURL + 'login')
}

async loginWithUsernameAndPassword(username, password) {
await page.fill(this.usernameSelector, username);
await page.fill(this.passwordSelector, password);
await page.click(this.loginButton);
}

async loginBasedOnRole(role) {
this.navigateToLoginPage()
switch (role) {
case 'admin':
await this.loginWithUsernameAndPassword('admin', 'admin')
break;
case 'user':
await this.loginWithUsernameAndPassword('user', 'user')
break;
default:
throw new Error(`Invalid role ${role} passed`)
}
}
}

exports.LoginPage=LoginPage
30 changes: 30 additions & 0 deletions yogesh/tests/acceptance/features/createResource.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Feature: Create a new resource
As a admin
I want to be able to create a new files and folders
So that I can organize my files

Background:
Given "admin" has logged in
And admin has navigated to the homepage


Scenario: Create a new folder
When admin creates a new folder named "myFolder"
Then admin should be able to see a folder named "myFolder"


Scenario: Create a new file with content
When admin creates a new file named "myFile.txt" with content "Hello World"
Then admin should be able to see a file named "myFile.txt" with content "Hello World"


Scenario: Rename a file
Given admin has created a file named "oldfile.txt" with content "Hello World"
When admin renames a file "oldfile.txt" to "newfile.txt"
Then admin should be able to see file with "newfile.txt" name


Scenario: Delete a file
Given admin creates a new file named "delMyFile.txt" using API
When admin deletes a file named "delMyFile.txt"
Then admin shouln't see "delMyFile" in the UI
24 changes: 24 additions & 0 deletions yogesh/tests/acceptance/features/login.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: Login
As an admin
I want to login into the application
So I can manage my files

Background:
Given admin has browsed to the login page


Scenario: admin logs in with correct credentials
When admin logs in with username as 'admin' and password as 'admin'
Then admin should be navigated to homescreen


Scenario Outline: admin logs in with incorrect credentials
When admin logs in with username as "<username>" and password as "<password>"
Then admin should see "Wrong credentials" message
Examples:
| username | password |
| jankari | admin |
| admin | jankari |
| | admin |
| admin | |
| | |
67 changes: 67 additions & 0 deletions yogesh/tests/acceptance/stepDefinitions/homeContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { Given, When, Then } = require('@cucumber/cucumber')
const assert = require('assert');
const util = require('util')
const {expect} = require('@playwright/test')

const { HomePage } = require("../PageObject/HomePage");
const { LoginPage } = require('../PageObject/LoginPage');
const { createFile } = require('../../../../testHelper/helper');

const login = new LoginPage()
const homepage = new HomePage()

Given('{string} has logged in', async function (role) {
await login.loginBasedOnRole(role)
});

Given('admin has navigated to the homepage', async function () {
await expect(page).toHaveURL(login.baseURL + 'files/')
});

When('admin creates a new folder named {string}', async function (folderName) {
await homepage.createNewFolder(folderName)
});

Then('admin should be able to see a folder named {string}', async function (folderName) {
const userCreatedFolderName = await page.innerHTML(homepage.lastNavaigatedFolderSelector)
assert.equal(userCreatedFolderName, folderName, `Expected "${folderName}" but recieved message "${userCreatedFolderName}" from UI`)
});

Given('admin has created a file named {string} with content {string}',async function (filename,content){
await homepage.createFileWithContent(filename, content)
await expect(page.locator(util.format(homepage.fileSelector,filename))).toBeVisible()
})

When('admin creates a new file named {string} with content {string}', async function (filename, content) {
await homepage.createFileWithContent(filename, content)
});

Given('admin creates a new file named {string} using API', async function (filename) {
await createFile(filename)
await page.reload()
await expect(page.locator(util.format(homepage.fileSelector,filename))).toBeVisible()
});


Then('admin should be able to see a file named {string} with content {string}', async function (filename, content) {
await expect(page.locator(util.format(homepage.fileSelector,filename))).toBeVisible()
await page.dblclick(util.format(homepage.fileSelector,filename));
const fileContent = await page.innerHTML(homepage.editorContent)
assert.equal(fileContent, content, `Expected content as "${content}" but recieved "${fileContent}"`)
});

When('admin renames a file {string} to {string}',async function(oldfileName,newfileName){
await homepage.renameFile(oldfileName,newfileName)
})

Then('admin should be able to see file with {string} name',async function(newfileName){
await expect(page.locator(util.format(homepage.fileSelector,newfileName))).toBeVisible()
})

When('admin deletes a file named {string}',async function(filename){
await homepage.deleteFile(filename)
})

Then('admin shouln\'t see {string} in the UI',async function(filename){
await expect(page.locator(util.format(homepage.fileSelector,filename))).toBeHidden()
})
25 changes: 25 additions & 0 deletions yogesh/tests/acceptance/stepDefinitions/loginContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { Given, When, Then } = require('@cucumber/cucumber')
const { expect } = require('@playwright/test');
const assert = require('assert');

const { LoginPage } = require('../PageObject/LoginPage');

const login = new LoginPage()

Given('admin has browsed to the login page', async () => {
await login.navigateToLoginPage()
await expect(page).toHaveURL(login.baseURL + 'login')
});

When('admin logs in with username as {string} and password as {string}', async (username, password) => {
await login.loginWithUsernameAndPassword(username,password)
});

Then('admin should be navigated to homescreen', async function () {
await expect(page).toHaveURL(login.baseURL + "files/");
});

Then('admin should see {string} message', async function (expectedMessage) {
const errorMessage = await page.innerHTML(login.wrongCredentialsDivSelector)
assert.equal(errorMessage, expectedMessage, `Expected message string "${expectedMessage}" but received message "${errorMessage}" from UI`)
});

0 comments on commit ade9b06

Please sign in to comment.