From 030b7b749ef4ea737d541cee36b18d664fe43aa7 Mon Sep 17 00:00:00 2001 From: Annie Huang Date: Sun, 29 Oct 2023 02:01:01 +0800 Subject: [PATCH] Update function in command.js --- tests/cypress/e2e/helloworld.cy.js | 31 ++++++++---------------------- tests/cypress/support/commands.js | 16 ++++++++++++++- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/tests/cypress/e2e/helloworld.cy.js b/tests/cypress/e2e/helloworld.cy.js index 77ccd0f..0967050 100644 --- a/tests/cypress/e2e/helloworld.cy.js +++ b/tests/cypress/e2e/helloworld.cy.js @@ -17,12 +17,6 @@ export function getTerminalrowsContain(content) { .children() .filter(`:contains('${content}')`) } -export function checkHint(index, total) { - cy.get('#hint') - .get('.flex-wrap') - .contains(`${index}/${total}`) - .should('be.visible') -} const finishSign = 'commander:~ $' const continueSign = '↵' const storyStart = 'Unix Spirit: ' @@ -93,12 +87,7 @@ describe('example helloworld app', () => { }) describe('Game Start Page UI', () => { beforeEach(() => { - cy.visit('/') - cy.LoginWithPassword('alex', '123456') - cy.get('.xterm-screen').as('Terminaltextbox').should('be.visible') - cy.findByRole('button', { name: 'Reset' }).click() - cy.typeInCommand('clear{enter}') - cy.get('@Terminaltextbox').should('contain', finishSign) + cy.PrepareForGame() }) it('Check Header', () => { cy.get('#quest').should('be.visible') @@ -146,7 +135,7 @@ describe('example helloworld app', () => { .and('contain', 'commander:~ $') }) it('Check Hint Part', () => { - cy.get('#hint').should('be.visible') + cy.get('#hint', { timeout: 20000 }).should('be.visible') cy.get('#hint').get('svg[data-icon="lightbulb"]').should('be.visible') cy.get('#hint').get('h1').should('contain', 'Hint').and('be.visible') }) @@ -156,7 +145,7 @@ describe('example helloworld app', () => { cy.get('#tree').get('a').should('contain', '???').and('be.visible') }) it('Check Fucnctional Buttons', () => { - cy.findByRole('button', { name: 'Solution' }, { timeout: 20000 }).should( + cy.findByRole('button', { name: 'Solution' }, { timeout: 30000 }).should( 'be.visible' ) cy.findByRole('button', { name: 'Reset' }).should('be.visible') @@ -165,15 +154,11 @@ describe('example helloworld app', () => { }) describe('Game Play', () => { beforeEach(() => { - cy.visit('/') - cy.LoginWithPassword('alex', '123456') - cy.get('.xterm-screen').as('Terminaltextbox') - cy.findByRole('button', { name: 'Reset' }).click() - cy.get('@Terminaltextbox').should('contain', finishSign) + cy.PrepareForGame() cy.readFile('../quests/helloworld/answer.sh', 'utf-8').as('answers') }) it('Typing in Terminal', () => { - cy.get('@Terminaltextbox', { timeout: 20000 }).type('12345{enter}') + cy.typeInCommand('12345{enter}') cy.get('@Terminaltextbox') .should('contain', 'zsh: command not found: 12345') .and('contain', '12345') @@ -202,7 +187,7 @@ describe('example helloworld app', () => { .nextUntil(`:contains('${finishSign}')`) cy.get('#Lbutton').should('be.visible').and('be.disabled') cy.get('#Rbutton').should('be.visible').and('be.disabled') - checkHint(1, 1) + cy.checkHint(1, 1) // Stage2 cy.log('Stage2') cy.typeInCommand(answerarr[1]) @@ -227,10 +212,10 @@ describe('example helloworld app', () => { .nextUntil(`:contains('${finishSign}')`) cy.get('#Lbutton').should('be.visible').and('be.enabled') cy.get('#Rbutton').should('be.visible').and('be.disabled') - checkHint(3, 3) + cy.checkHint(3, 3) cy.get('#Lbutton').click() cy.get('#Rbutton').should('be.enabled') - checkHint(2, 3) + cy.checkHint(2, 3) // Stage secret cy.log('Stage secret') cy.typeInCommand(answerarr[2]) diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index 65c2638..f6fcb78 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -32,8 +32,16 @@ Cypress.Commands.add('LoginWithPassword', (username, password) => { cy.get('#password').type(password) cy.findByRole('button', { name: 'Log in' }).click() }) +Cypress.Commands.add('PrepareForGame', () => { + cy.visit('/') + cy.LoginWithPassword('alex', '123456') + cy.get('.xterm-screen').as('Terminaltextbox').should('be.visible') + cy.findByRole('button', { name: 'Reset' }).click() + cy.typeInCommand('clear{enter}') + cy.get('@Terminaltextbox').should('contain', 'commander:~ $') +}) Cypress.Commands.add('typeInCommand', (command) => { - cy.get('.xterm-screen').type(command) + cy.get('.xterm-screen', { timeout: 150000 }).type(command) }) Cypress.Commands.add('getQuestInfo', (id) => { return cy.get('#quest').find('p.text-text').contains(`${id}`) @@ -41,3 +49,9 @@ Cypress.Commands.add('getQuestInfo', (id) => { Cypress.Commands.add('getTaskCheckbox', (id) => { return cy.getQuestInfo(id).findByRole('checkbox') }) +Cypress.Commands.add('checkHint', (index, total) => { + cy.get('#hint') + .get('.flex-wrap') + .contains(`${index}/${total}`) + .should('be.visible') +})