From 1e82bcfeaded6ebb7025af736da21bcf335de34f Mon Sep 17 00:00:00 2001 From: icelandld Date: Mon, 17 Jun 2024 21:45:48 +0800 Subject: [PATCH] add comment --- cypress.config.js | 6 +----- cypress/KongBase/KongElement.js | 3 +++ cypress/e2e/route.cy.js | 9 +++++---- cypress/pages/base.js | 14 +++++++++----- cypress/pages/home.js | 8 +++++--- cypress/pages/navigator.js | 13 +++++++++---- cypress/pages/route.js | 21 +++++++++++++++++---- cypress/pages/routes.js | 10 ++++++++-- cypress/pages/service.js | 28 ++++++++++++++++++++-------- cypress/pages/services.js | 11 ++++++++++- 10 files changed, 87 insertions(+), 36 deletions(-) diff --git a/cypress.config.js b/cypress.config.js index ba6292c..fc0684f 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -6,6 +6,7 @@ module.exports = defineConfig({ env:{ adminUrl: 'http://localhost:8001', clientUrl: 'http://localhost:8000', + //config whether the execution can be reapeatable repeatable: true }, reporter: 'mochawesome', @@ -19,10 +20,5 @@ module.exports = defineConfig({ // implement node event listeners here }, }, - env: { - //config whether the execution can be reapeatable - repeatable: true - }, "screenshotsFolder": "outputs/screenshots" - }); diff --git a/cypress/KongBase/KongElement.js b/cypress/KongBase/KongElement.js index 1798a7d..2bbbfa3 100644 --- a/cypress/KongBase/KongElement.js +++ b/cypress/KongBase/KongElement.js @@ -1,3 +1,6 @@ +/** + * KongElement, get the element by data-testid or aria-label which specified by Kong + */ export default class KongElement { static fromTAId(uatId) { diff --git a/cypress/e2e/route.cy.js b/cypress/e2e/route.cy.js index 768d717..d641608 100644 --- a/cypress/e2e/route.cy.js +++ b/cypress/e2e/route.cy.js @@ -21,15 +21,15 @@ describe('test for route creation', () => { }) }); - //add route with data from parameter - it('create route', () => { - - //access route page + beforeEach(() => { home.access(); home.openWS(); nav.navigate('Routes'); + }) + it('create route', () => { routes.add(); + //create route with data object route.create({ name: "example_route", service: service.id, @@ -39,6 +39,7 @@ describe('test for route creation', () => { }); }) + //FIXME: this test works in local but not in CI, response as 404 in CI it('check route works', () => { if (routeName) { const clientBase = `${Cypress.env('clientUrl')}`; diff --git a/cypress/pages/base.js b/cypress/pages/base.js index f1267dd..418fc00 100644 --- a/cypress/pages/base.js +++ b/cypress/pages/base.js @@ -1,8 +1,10 @@ -import ke from "../KongBase/KongElement"; - -// cypress/pages/BasePage.js export const ReapatAble = (() => Cypress.env('repeatable'))(); +import ke from "../KongBase/KongElement"; + +/** + * BasePage, the base page for all page object + */ export default class BasePage { constructor(url) { @@ -15,12 +17,14 @@ export default class BasePage { } ke.open(this.url) } - + /** Load data from fixture file or data object + * @param {*} data + * @returns return as cypress chainable object + */ loadData(data) { if (!data) { throw new Error('Data is required'); } - if (typeof (data) !== 'object' && data.includes('.json')) { return cy.fixture(data).then((fixtureData) => { return fixtureData; diff --git a/cypress/pages/home.js b/cypress/pages/home.js index 34054e1..b6f087c 100644 --- a/cypress/pages/home.js +++ b/cypress/pages/home.js @@ -1,10 +1,12 @@ -// import ke from "../widgets/KongElement"; -import BasePage from "./base"; - const HomePage = { defaultWorkspace: "workspace-link-default" } +//-------------------Hompage-------------------// + +import ke from "../KongBase/KongElement"; +import BasePage from "./base"; + export class Home extends BasePage { constructor() { super('/') diff --git a/cypress/pages/navigator.js b/cypress/pages/navigator.js index dd7617d..0efbf4c 100644 --- a/cypress/pages/navigator.js +++ b/cypress/pages/navigator.js @@ -1,14 +1,18 @@ -// import ke from "../widgets/KongElement"; - - -//automatioin id for the main menu const NavigationMenu = { main: "Open Main Menu", "Gateway Serivces": "sidebar-item-gateway-services", "Routes": "sidebar-item-routes" } +//-------------------NavigatorPage-------------------// + +import ke from "../KongBase/KongElement"; + class Navigator { + + /** Navigate to the page by name + * @param {string} name + */ navigate(name) { ke.fromTAId(NavigationMenu[name]).then(($el) => { if (Cypress.dom.isVisible($el)) { @@ -18,6 +22,7 @@ class Navigator { ke.fromTAId(NavigationMenu[name]).click(); } }) + cy.log(`Navigated to ${name}`) } } diff --git a/cypress/pages/route.js b/cypress/pages/route.js index 95eee6a..f041ee7 100644 --- a/cypress/pages/route.js +++ b/cypress/pages/route.js @@ -1,23 +1,35 @@ +//key is same as entity property name const RouteForm = { - "name": "route-form-name", + "name": "route-form-name", "service": "route-form-service-id", "paths": "route-form-paths-input-{index}", "service-select": "select-item-{serviceId}", "Save": "form-submit" } -import { randomNum } from "../utils/randomUtil"; +//-------------------RoutePage-------------------// + import BasePage, { ReapatAble } from "./base"; +import ke from "../KongBase/KongElement"; +import { randomNum } from "../utils/randomUtil"; export class Route extends BasePage { + + constructor(ws = 'default', rootId) { + super(`/${ws}/routes/${rootId}`); + } + + /** + * create route with given data + * @param {*} data {service: string, name: string, paths: string[]} + * @returns cypress chain object + */ create(data) { return super.loadData(data).then((route) => { if (ReapatAble) { route.name = route.name + randomNum(6); } - Object.entries(route).forEach(([key, value]) => { - cy.log(key, value); if (key === 'service') { ke.fromTAId(RouteForm["service"]).click(); ke.fromTAId(RouteForm["service-select"].replace("{serviceId}", value)).click(); @@ -32,6 +44,7 @@ export class Route extends BasePage { }); ke.fromTAId(RouteForm["Save"]).click(); cy.get('.toaster-message').should('have.text', `Route "${route.name}" successfully created!`); + cy.log('route created', route); return cy.wrap(route); }); } diff --git a/cypress/pages/routes.js b/cypress/pages/routes.js index 9e7c488..38295ee 100644 --- a/cypress/pages/routes.js +++ b/cypress/pages/routes.js @@ -1,15 +1,21 @@ const RoutesPage = { "New Route": ["new-route", "toolbar-add-route"] } +//-------------------RoutesPage-------------------// import BasePage from "./base"; import ke from "../KongBase/KongElement"; export class Routes extends BasePage { + + constructor(ws = 'default') { + super(`/${ws}/routes`); + } + add() { - ke.fromTAId(RoutesPage["New Route"][0]).click(); + ke.fromTAId(RoutesPage["New Route"][1]).click(); - //FIXME: How to handle there's already routes + //FIXME: How to handle there's routes already exist // cy.get('body').then($el => { // let $element = $el.find(`[data-testid="${RoutesPage["New Route"][1]}"]`); // if ($element.length > 0 && $element.css('display')!=='none') { diff --git a/cypress/pages/service.js b/cypress/pages/service.js index 8326e25..ffc37b3 100644 --- a/cypress/pages/service.js +++ b/cypress/pages/service.js @@ -1,28 +1,40 @@ -// import ke from "../widgets/KongElement"; -import BasePage, { ReapatAble } from "./base"; -import { randomNum } from "../utils/randomUtil"; -import route from "./route"; - -//automatioin id for the main menu const ServiceForm = { name: "gateway-service-name-input", url: "gateway-service-url-input", submit: "form-submit" } +//-------------------ServicePage-------------------// + + +import BasePage, { ReapatAble } from "./base"; +import route from "./route"; +import { randomNum } from "../utils/randomUtil"; +import ke from "../KongBase/KongElement"; + export class Service extends BasePage { + + constructor(ws = 'default', serviceId) { + super(`/${ws}/services/${serviceId}`); + } + + /** + * create service with given data + * @param {} service {name: string, url: string} + * @returns cypress chain object + */ create(service) { return super.loadData(service).then((serviceData) => { if (ReapatAble) { serviceData.name = serviceData.name + randomNum(6); } Object.entries(serviceData).forEach(([key, value]) => { - cy.log(key, value); + cy.log(`set service with ${key} as ${value}`); ke.fromTAId(ServiceForm[key]).type(value); }); ke.fromTAId(ServiceForm["submit"]).click(); cy.get('.toaster-message').should('have.text', `Gateway Service "${serviceData.name}" successfully created!`); - + cy.log('service created', serviceData); return cy.wrap(serviceData); }); } diff --git a/cypress/pages/services.js b/cypress/pages/services.js index 07253ed..5b4f2a4 100644 --- a/cypress/pages/services.js +++ b/cypress/pages/services.js @@ -7,14 +7,22 @@ const ServicesPage = { "Service-Row": "{serviceName}", } +//-------------------ServicesPage-------------------// + import BasePage from "./base"; +import ke from "../KongBase/KongElement"; + export class Services extends BasePage { + constructor(ws = 'default') { + super(`/${ws}/services`); + } add() { ke.fromTAId(ServicesPage["New Gateway Service"][1]).click(); + //FIXME: How to handle there's services already exist // cy.get('body').then($el => { // let $element = $el.find(`[data-testid="${ServicesPage["New Gateway Service"][1]}"]`); - // if ($element.length >0 && $element.css('display')!=='None') { + // if ($element.length >0 && $element.css('display')!=='none') { // ke.fromTAId(ServicesPage["New Gateway Service"][1]).click(); // } else { // ke.fromTAId(ServicesPage["New Gateway Service"][0]).click(); @@ -35,6 +43,7 @@ export class Services extends BasePage { ke.fromTAId(ServicesPage["Service-Row"].replace("{serviceName}", serviceName)) .should('be.visible').first() .click(); + cy.log('service is opened', serviceName) } }