From 839b87b47837f0224d6e5245244d873b1d696179 Mon Sep 17 00:00:00 2001 From: Annie Huang Date: Sun, 29 Oct 2023 01:34:37 +0800 Subject: [PATCH] fix remove verify by storytext add global function --- quests/helloworld/answer.json | 30 ------ quests/helloworld/answer.sh | 2 +- tests/cypress/e2e/helloworld.cy.js | 145 ++++++++++++++--------------- tests/cypress/support/commands.js | 9 ++ 4 files changed, 80 insertions(+), 106 deletions(-) delete mode 100644 quests/helloworld/answer.json diff --git a/quests/helloworld/answer.json b/quests/helloworld/answer.json deleted file mode 100644 index d81391f..0000000 --- a/quests/helloworld/answer.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "stage1": "echo Hello World!{enter}", - "story1": "{enter}{enter}{enter}{enter}{enter}{enter}{enter}{enter}{enter}", - "start1": "你聽到了一個聲音:", - "end1": "現在,請你輸入 `ls` 來查看目前的目錄下有哪些檔案。", - "stage2": "ls{enter}", - "story2": "{enter}{enter}{enter}{enter}{enter}", - "start2": "你找到了一個被遺忘的卷軸,上面寫著解除封印的咒語。", - "end2": "使用 `Tab` 鍵來取得指令提示。", - "stage_hidden1": "mkdir jail{enter}", - "stage_hidden2": "ls{enter}", - "stage_hidden3": "cd jail{enter}", - "stage_hidden_story1": "你進入了一個牢房,牢房裡有一個門,門上有一個鎖。", - "stage_hidden_story2": "轉身發現,自己被關在了牢房裡。", - "stage_hidden4": "{enter}{enter}{enter}", - "stage_hidden5": "cd ..{enter}", - "stage_hidden6": "rmdir jail{enter}", - "stage_hidden7": "ls{enter}", - "stage3": "cat forgotten_scroll.txt{enter}", - "story3": "{enter}{enter}", - "scrollcontent": "Unix, rise and guide me!", - "start3": "你找到的是一個被遺忘的卷軸,上面寫著解除封印的咒語。", - "end3": "使用你已經知道的 `echo` 指令來詠唱咒語。", - "stage4": "echo Unix, rise and guide me!{enter}", - "end4": "突然之間,你感覺到一股強大的力量從你的身體中湧出,你的身體開始發光,你的手中出現了一把魔法之劍。", - "hint1": "使用 `ls` 指令來查看目前的目錄下有哪些檔案。", - "hint2": "使用 `cat [檔案名稱]` 指令來查看檔案內容。", - "hint3": "使用 `Tab` 鍵來取得指令提示。", - "Thanks": "您已成功完成挑戰,真的非常感謝您的參與! 如果您能撥冗填寫這份問卷,我們將不勝感激,這對我們非常重要!" -} diff --git a/quests/helloworld/answer.sh b/quests/helloworld/answer.sh index eb29a7f..703fb42 100644 --- a/quests/helloworld/answer.sh +++ b/quests/helloworld/answer.sh @@ -7,4 +7,4 @@ cd ..{enter} rmdir jail{enter} ls {enter} cat forgotten_scroll.txt{enter} -echo Unix,rise and guide me!{enter} \ No newline at end of file +echo Unix, rise and guide me!{enter} \ No newline at end of file diff --git a/tests/cypress/e2e/helloworld.cy.js b/tests/cypress/e2e/helloworld.cy.js index 0d36f1a..2dc015b 100644 --- a/tests/cypress/e2e/helloworld.cy.js +++ b/tests/cypress/e2e/helloworld.cy.js @@ -17,23 +17,15 @@ export function getTerminalrowsContain(content) { .children() .filter(`:contains('${content}')`) } -export function checkStoryStart() { - cy.get('@Terminaltextbox').should('contain', `Unix Spirit: `) -} -export function checkHint(index, total, content) { +export function checkHint(index, total) { cy.get('#hint') .get('.flex-wrap') - .invoke('text') - .should('contain', `${index}/${total}`) - .and('contain', content) + .contains(`${index}/${total}`) + .should('be.visible') } -export const getQuestInfo = (id) => - cy.get('#quest').get('p.text-text').contains(`${id}`) -export const getTaskCheckbox = (id) => - cy.get('#quest').get('p.text-text').contains(`${id}`).findByRole('checkbox') const finishSign = 'commander:~ ' const continueSign = '↵' -// const YAML = require('yamljs') +const storyStart = 'Unix Spirit: ' describe('example helloworld app', () => { describe('Login tests', () => { @@ -88,7 +80,7 @@ describe('example helloworld app', () => { cy.findByRole('link', { name: 'GitHub' }).should('be.visible') }) it('Login-Password Already Register', () => { - cy.LoginWithPassword() + cy.LoginWithPassword('alex', '123456') }) it.skip('Login-Google', () => { cy.clearCookies() @@ -102,10 +94,10 @@ describe('example helloworld app', () => { describe('Game Start Page UI', () => { beforeEach(() => { cy.visit('/') - cy.LoginWithPassword() + cy.LoginWithPassword('alex', '123456') cy.get('.xterm-screen').as('Terminaltextbox').should('be.visible') cy.findByRole('button', { name: 'Reset' }).click() - cy.get('@Terminaltextbox').type('clear{enter}') + cy.typeInCommand('clear{enter}') cy.get('@Terminaltextbox').should('contain', 'commander:~ $') }) it('Check Header', () => { @@ -121,12 +113,12 @@ describe('example helloworld app', () => { }) it('Check QuestInfo', () => { cy.get('#topic').should('contain', 'Hello, Linux World!') - getQuestInfo( + cy.getQuestInfo( '你身在一個漆黑的房間裡,沒有任何的光線或聲音。你想不起來為什麼自己會來到這裡。但你的腦中隱約感到某個聲音跟你說: 「輸入 echo Hello World!」' ).should('be.visible') - getQuestInfo('Tasks:').should('be.visible') - getQuestInfo('使用 `echo Hello World!` 召喚精靈').should('be.visible') - getTaskCheckbox('使用 `echo Hello World!` 召喚精靈').should( + cy.getQuestInfo('Tasks:').should('be.visible') + cy.getQuestInfo('使用 `echo Hello World!` 召喚精靈').should('be.visible') + cy.getTaskCheckbox('使用 `echo Hello World!` 召喚精靈').should( 'not.be.checked' ) }) @@ -174,11 +166,11 @@ describe('example helloworld app', () => { describe('Game Play', () => { beforeEach(() => { cy.visit('/') - cy.LoginWithPassword() + cy.LoginWithPassword('alex', '123456') cy.get('.xterm-screen').as('Terminaltextbox') cy.findByRole('button', { name: 'Reset' }).click() cy.get('@Terminaltextbox').should('contain', finishSign) - cy.readFile('../quests/helloworld/answer.json', 'utf-8').as('answersheet') + cy.readFile('../quests/helloworld/answer.sh', 'utf-8').as('answers') }) it('Typing in Terminal', () => { cy.get('@Terminaltextbox', { timeout: 20000 }).type('12345{enter}') @@ -188,96 +180,99 @@ describe('example helloworld app', () => { }) it('Complete the Game', () => { // get answersheet - cy.get('@answersheet').then((answers) => { + cy.get('@answers').then((answers) => { + const answerarr = answers.split('\n') // Stage1 cy.log('Stage1') - cy.get('@Terminaltextbox').type(answers.stage1) + cy.typeInCommand(answerarr[0]) cy.get('@Terminaltextbox') - .should('contain', answers.start1) - .and('contain', continueSign) - cy.get('@Terminaltextbox').type(answers.story1) - getTaskCheckbox('使用 `echo Hello World!` 召喚精靈').should( + // .should('contain', answers.start1) + .should('contain', continueSign) + cy.typeInCommand( + '{enter}{enter}{enter}{enter}{enter}{enter}{enter}{enter}{enter}' + ) + cy.getTaskCheckbox('使用 `echo Hello World!` 召喚精靈').should( 'be.checked' ) - getTaskCheckbox('搜索卷軸').should('be.visible').and('not.be.checked') + cy.getTaskCheckbox('搜索卷軸') + .should('be.visible') + .and('not.be.checked') cy.get('@Terminaltextbox') - .should('contain', `Unix Spirit: `) - .and('contain', answers.end1) - .and('contain', finishSign) + .contains(storyStart) + .nextUntil(`:contains('${finishSign}')`) cy.get('#Lbutton').should('be.visible').and('be.disabled') cy.get('#Rbutton').should('be.visible').and('be.disabled') - checkHint(1, 1, answers.hint1) + checkHint(1, 1) // Stage2 cy.log('Stage2') - cy.get('@Terminaltextbox').type(answers.stage2) + cy.typeInCommand(answerarr[1]) cy.get('@Terminaltextbox') - .should('contain', 'forgotten_scroll.txt') - .and('contain', answers.start2) - .and('contain', continueSign) + .contains(answerarr[8].replace('{enter}', '').replace('cat ', '')) + .wait(1000) + .nextUntil(`:contains('${continueSign}')`) cy.get('#tree') .get('a') .should('contain', 'forgotten_scroll.txt') .and('be.visible') - cy.get('@Terminaltextbox').type(answers.story2) - getTaskCheckbox('搜索卷軸').should('be.checked') - getTaskCheckbox('移動到下一個房間') + cy.typeInCommand('{enter}{enter}{enter}{enter}{enter}') + cy.getTaskCheckbox('搜索卷軸').should('be.checked') + cy.getTaskCheckbox('移動到下一個房間') + .should('be.visible') + .and('not.be.checked') + cy.getTaskCheckbox('查看卷軸') .should('be.visible') .and('not.be.checked') - getTaskCheckbox('查看卷軸').should('be.visible').and('not.be.checked') cy.get('@Terminaltextbox') - .should('contain', answers.end2) - .and('contain', finishSign) + .contains(storyStart) + .nextUntil(`:contains('${finishSign}')`) cy.get('#Lbutton').should('be.visible').and('be.enabled') cy.get('#Rbutton').should('be.visible').and('be.disabled') - checkHint(3, 3, answers.hint3) + checkHint(3, 3) cy.get('#Lbutton').click() cy.get('#Rbutton').should('be.enabled') - checkHint(2, 3, answers.hint2) + checkHint(2, 3) // Stage secret cy.log('Stage secret') - cy.get('@Terminaltextbox').type(answers.stage_secret1) - cy.get('@Terminaltextbox').type(answers.stage_secret2) + cy.typeInCommand(answerarr[2]) + cy.typeInCommand(answerarr[3]) cy.get('#tree').get('a').should('contain', 'jail').and('be.visible') - cy.get('@Terminaltextbox').type(answers.stage_secret3) - cy.get('@Terminaltextbox').should( - 'contain', - answers.stage_secret_story1 - ) - cy.get('@Terminaltextbox').type(answers.stage_secret4) - cy.get('@Terminaltextbox').should( - 'contain', - answers.stage_secret_story2 - ) - cy.get('@Terminaltextbox').type(answers.stage_secret5) - cy.get('@Terminaltextbox').type(answers.stage_secret6) - cy.get('@Terminaltextbox').type(answers.stage_secret7) + cy.typeInCommand(answerarr[4]) + cy.get('@Terminaltextbox') + .contains(answerarr[4].replace('{enter}', '')) + .wait(1000) + .nextUntil(`:contains('${continueSign}')`) + cy.typeInCommand(`{enter}{enter}{enter}`) + cy.typeInCommand(answerarr[5]) + cy.typeInCommand(answerarr[6]) + cy.typeInCommand(answerarr[7]) cy.get('#tree').get('a').contains('jail').and('not.exist') - getTaskCheckbox('移動到下一個房間').should('be.checked') + cy.getTaskCheckbox('移動到下一個房間').should('be.checked') // Stage3 cy.log('Stage3') - cy.get('@Terminaltextbox').type(answers.stage3) + cy.typeInCommand(answerarr[8]) cy.get('@Terminaltextbox') - .should('contain', answers.scrollcontent) - .and('contain', answers.start3) - .and('contain', continueSign) - cy.get('@Terminaltextbox').type(answers.story3) - getTaskCheckbox('查看卷軸').should('be.checked') + .contains(answerarr[8].replace('{enter}', '')) + .wait(1000) + .nextUntil(`:contains('${continueSign}')`) + cy.typeInCommand('{enter}{enter}') + cy.getTaskCheckbox('查看卷軸').should('be.checked') cy.get('@Terminaltextbox') - .should('contain', answers.end3) - .and('contain', finishSign) + .contains(answerarr[8].replace('{enter}', '')) + .nextUntil(`:contains('${finishSign}')`) // Stage4 cy.log('Stage4') - cy.get('@Terminaltextbox').type(answers.stage4) - // cy.get('@Terminaltextbox').should('contain', answers.end4) + cy.typeInCommand(answerarr[9]) + cy.get('@Terminaltextbox') + .contains(answerarr[9].replace('{enter}', '')) + .wait(1000) + .nextUntil(`:contains('${continueSign}')`) cy.findByRole( 'heading', { name: 'Quest Completed!' }, - { timeout: 200000 } + { timeout: 20000 } ).should('be.visible') - cy.get('div[class="modal"]') - .find('p') - .invoke('text') - .should('contain', answers.Thanks) + // Check survey dialog pop up + cy.get('div[class="modal"]').find('p').should('be.visible') cy.findByRole('link', { name: '填寫問卷' }).should('be.visible') }) }) diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index e7ba111..6cffa80 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -32,3 +32,12 @@ Cypress.Commands.add('LoginWithPassword', (username, password) => { cy.get('#password').type(password) cy.findByRole('button', { name: 'Log in' }).click() }) +Cypress.Commands.add('typeInCommand', (command) => { + cy.get('.xterm-screen').type(command) +}) +Cypress.Commands.add('getQuestInfo', (id) => { + return cy.get('#quest').get('p.text-text').contains(`${id}`) +}) +Cypress.Commands.add('getTaskCheckbox', (id) => { + return cy.getQuestInfo(id).findByRole('checkbox') +})