Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file modified cypress/e2e/.DS_Store
Binary file not shown.
5 changes: 2 additions & 3 deletions cypress/e2e/carAndExpenses.cy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import GaragePage from '../pages/GaragePage';
import ExpensesPage from '../pages/ExpensesPage';
import LoginPage from '../pages/LoginPage';
import { after } from 'mocha';

describe('Car and Expenses Tests', () => {
beforeEach(function() {
Expand All @@ -22,7 +21,7 @@ describe('Car and Expenses Tests', () => {
});

it('Add a car to garage', function() {
GaragePage.addCar(this.data.carData);
GaragePage.addCarViaUI(this.data.carData);
GaragePage.getGarageItem().should('exist');

});
Expand All @@ -33,6 +32,6 @@ describe('Car and Expenses Tests', () => {
});

after(() => {
GaragePage.removeCar();
GaragePage.deleteAllCarsviaApi();
})
});
72 changes: 72 additions & 0 deletions cypress/e2e/carAndExpensesApi.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import ExpensesPage from '../pages/ExpensesPage';
import GaragePage from '../pages/GaragePage';
import LoginPage from '../pages/LoginPage';
import { beforeEach } from 'mocha';

describe('Car and Expenses Tests', () => {
beforeEach(function() {
const url = Cypress.config('baseUrl');
const email = Cypress.config('users').email;
const password = Cypress.config('users').password;

cy.fixture('carAndExpenses').as('data');
cy.fixture('savedData').as('id');

cy.visit(url, {
auth: {
username: "guest",
password: "welcome2qauto"
}
});

LoginPage.login(email, password);
});

it('Add a car to garage', function() {
GaragePage.addCarViaUI(this.data.carData);
GaragePage.getGarageItem().should('exist');

});

it('Create expense via API', function(){
const carIdData = this.id.createdCarId + 1;
const todayData = new Date().toISOString().split('T')[0];
const mileageData = parseInt(this.data.expenseData.mileage);
const litersData = parseInt(this.data.expenseData.liters);
const totalCostData = parseInt(this.data.expenseData.totalCost)
const [year, month, day] = todayData.split('-');
const formattedDate = `${day}.${month}.${year}`;


const testBody = {
carId: carIdData,
reportedAt: todayData,
mileage: mileageData,
liters: litersData,
totalCost: totalCostData,
forceMileage: false
};

console.log('Request Body:', JSON.stringify(testBody, null, 2)); // Debug request payload

cy.createExpense(testBody).then((response) => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property('status', 'ok');
expect(response.body).to.have.property('data');
expect(response.body.data).to.have.property('id');
expect(response.body.data).to.have.property('carId', carIdData);
expect(response.body.data).to.have.property('reportedAt');
expect(response.body.data).to.have.property('mileage', mileageData);
expect(response.body.data).to.have.property('liters', litersData);
expect(response.body.data).to.have.property('totalCost', totalCostData);
});
ExpensesPage.checkExpenseData(testBody, formattedDate);
})


after(()=>{

GaragePage.deleteAllCars();
})

});
2 changes: 1 addition & 1 deletion cypress/fixtures/carAndExpenses.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"expenseData": {
"car": "Porsche Panamera",
"date": "27.03.2025",
"mileage": "1100",
"mileage": "1333",
"liters": "50",
"totalCost": "100"
}
Expand Down
3 changes: 3 additions & 0 deletions cypress/fixtures/savedData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"createdCarId": 321716
}
39 changes: 39 additions & 0 deletions cypress/pages/ExpensesPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
class ExpensesPage {

getCar(){
return cy.get('#carSelectDropdown');
}

getExpenceDate(){
return cy.get('.expenses_table tbody tr td.font-weight-bold').first();
}

getExpenceMileage(){
return cy.get('.expenses_table tbody tr td').eq(1);
}

getExpenceLiters(){
return cy.get('.expenses_table tbody tr td').eq(2);
}

getExpenceTotalCost(){
return cy.get('.expenses_table tbody tr td').eq(3);
}

selectCar(car){
this.getCar().select(car);
}

addExpense(data){
cy.get('#addExpenseCar').select(data.car);
Expand All @@ -9,6 +33,21 @@ class ExpensesPage {
cy.get('.modal-footer > .btn-primary').click();
}

checkExpenseData(data, formattedDate){
cy.visit('/panel/expenses', {
auth: {
username: "guest",
password: "welcome2qauto"
}
});
this.getExpenceDate().should('contain', formattedDate);
this.getExpenceMileage().should('contain', data.mileage);
this.getExpenceLiters().should('contain', data.liters);
this.getExpenceTotalCost().should('contain', data.totalCost);
};



}

export default new ExpensesPage();
72 changes: 71 additions & 1 deletion cypress/pages/GaragePage.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,69 @@
class GaragePage {
constructor() {
this.createdCarId = null;
}

getCreatedCarId() {
return this.createdCarId;
}

clickAddCarButton() {
cy.contains('button', 'Add car').click();
return this;
}

selectBrand(optionBrand) {
cy.get('#addCarBrand').select(optionBrand);
return this;
}

selectModel(optionModel) {
cy.get('#addCarModel').select(optionModel);
return this;
}

addMileage(value) {
cy.get('#addCarMileage').type(value);
return this;
}

clickAddButton() {
cy.get('.modal-footer > .btn-primary').click();
return this;
}

addCar(data) {
addCarViaUI(data) {
cy.intercept('POST', '/api/cars').as('createCar');

this.clickAddCarButton();
this.selectBrand(data.brand);
this.selectModel(data.model);
this.addMileage(data.mileage);
this.clickAddButton();

cy.wait('@createCar').then((req) => {
expect(req.response.statusCode).to.eq(201);
this.createdCarId = req.response.body.data.id;

cy.fixture('savedData').then((fixture) => {
fixture.createdCarId = this.createdCarId;
cy.log(`Car ID from fixture: ${this.createdCarId}`);
cy.writeFile('cypress/fixtures/savedData.json', fixture);
});
});

this.verifyCreatedCarViaApi(data);
}

getCarsViaApi() {
return cy.request({
method: 'GET',
url: '/api/cars',
auth: {
username: "guest",
password: "welcome2qauto"
}
});
}

getGarageItem() {
Expand All @@ -48,6 +87,37 @@ class GaragePage {
cy.get('.car_add-expense').first().click();
}


verifyCreatedCarViaApi(data){
this.getCarsViaApi().then((response) => {
const createdCar = response.body.data.find(car => car.id === this.createdCarId);
expect(createdCar).to.exist;
expect(createdCar).to.deep.include({
brand: data.brand,
model: data.model,
mileage: parseInt(data.mileage)
});
}
)}

async deleteAllCars() {
const response = await this.getCarsViaApi();

if (response.status !== 200 || !response.body.data?.length) return;

for (const car of response.body.data) {
await cy.request({
method: 'DELETE',
url: `/api/cars/${car.id}`,
auth: {
username: "guest",
password: "welcome2qauto"
},
failOnStatusCode: false
});
}
}

}

export default new GaragePage();
Binary file not shown.
Binary file not shown.
Loading