Extract commonly used test logic to commands (#8767)
* meta(tests): enable workflows to run in branch * feat(tests): move commonly used logic to Cypress commands * chore(tests): replace more code with commands * meta(tests): disable workflows to run in branch
This commit is contained in:
		
							parent
							
								
									95a3565d1c
								
							
						
					
					
						commit
						d3e242a7f2
					
				
					 3 changed files with 44 additions and 76 deletions
				
			
		|  | @ -1,11 +1,6 @@ | ||||||
| describe('Before setup instance', () => { | describe('Before setup instance', () => { | ||||||
| 	beforeEach(() => { | 	beforeEach(() => { | ||||||
| 		cy.window(win => { | 		cy.resetState(); | ||||||
| 			win.indexedDB.deleteDatabase('keyval-store'); |  | ||||||
| 		}); |  | ||||||
| 		cy.request('POST', '/api/reset-db').as('reset'); |  | ||||||
| 		cy.get('@reset').its('status').should('equal', 204); |  | ||||||
| 		cy.reload(true); |  | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	afterEach(() => { | 	afterEach(() => { | ||||||
|  | @ -35,18 +30,10 @@ describe('Before setup instance', () => { | ||||||
| 
 | 
 | ||||||
| describe('After setup instance', () => { | describe('After setup instance', () => { | ||||||
| 	beforeEach(() => { | 	beforeEach(() => { | ||||||
| 		cy.window(win => { | 		cy.resetState(); | ||||||
| 			win.indexedDB.deleteDatabase('keyval-store'); |  | ||||||
| 		}); |  | ||||||
| 		cy.request('POST', '/api/reset-db').as('reset'); |  | ||||||
| 		cy.get('@reset').its('status').should('equal', 204); |  | ||||||
| 		cy.reload(true); |  | ||||||
| 
 | 
 | ||||||
| 		// インスタンス初期セットアップ
 | 		// インスタンス初期セットアップ
 | ||||||
| 		cy.request('POST', '/api/admin/accounts/create', { | 		cy.registerUser('admin', 'pass', true); | ||||||
| 			username: 'admin', |  | ||||||
| 			password: 'pass', |  | ||||||
| 		}).its('body').as('admin'); |  | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	afterEach(() => { | 	afterEach(() => { | ||||||
|  | @ -76,24 +63,13 @@ describe('After setup instance', () => { | ||||||
| 
 | 
 | ||||||
| describe('After user signup', () => { | describe('After user signup', () => { | ||||||
| 	beforeEach(() => { | 	beforeEach(() => { | ||||||
| 		cy.window(win => { | 		cy.resetState(); | ||||||
| 			win.indexedDB.deleteDatabase('keyval-store'); |  | ||||||
| 		}); |  | ||||||
| 		cy.request('POST', '/api/reset-db').as('reset'); |  | ||||||
| 		cy.get('@reset').its('status').should('equal', 204); |  | ||||||
| 		cy.reload(true); |  | ||||||
| 
 | 
 | ||||||
| 		// インスタンス初期セットアップ
 | 		// インスタンス初期セットアップ
 | ||||||
| 		cy.request('POST', '/api/admin/accounts/create', { | 		cy.registerUser('admin', 'pass', true); | ||||||
| 			username: 'admin', |  | ||||||
| 			password: 'pass', |  | ||||||
| 		}).its('body').as('admin'); |  | ||||||
| 
 | 
 | ||||||
| 		// ユーザー作成
 | 		// ユーザー作成
 | ||||||
| 		cy.request('POST', '/api/signup', { | 		cy.registerUser('alice', 'alice1234'); | ||||||
| 			username: 'alice', |  | ||||||
| 			password: 'alice1234', |  | ||||||
| 		}).its('body').as('alice'); |  | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	afterEach(() => { | 	afterEach(() => { | ||||||
|  | @ -138,34 +114,15 @@ describe('After user signup', () => { | ||||||
| 
 | 
 | ||||||
| describe('After user singed in', () => { | describe('After user singed in', () => { | ||||||
| 	beforeEach(() => { | 	beforeEach(() => { | ||||||
| 		cy.window(win => { | 		cy.resetState(); | ||||||
| 			win.indexedDB.deleteDatabase('keyval-store'); |  | ||||||
| 		}); |  | ||||||
| 		cy.request('POST', '/api/reset-db').as('reset'); |  | ||||||
| 		cy.get('@reset').its('status').should('equal', 204); |  | ||||||
| 		cy.reload(true); |  | ||||||
| 
 | 
 | ||||||
| 		// インスタンス初期セットアップ
 | 		// インスタンス初期セットアップ
 | ||||||
| 		cy.request('POST', '/api/admin/accounts/create', { | 		cy.registerUser('admin', 'pass', true); | ||||||
| 			username: 'admin', |  | ||||||
| 			password: 'pass', |  | ||||||
| 		}).its('body').as('admin'); |  | ||||||
| 
 | 
 | ||||||
| 		// ユーザー作成
 | 		// ユーザー作成
 | ||||||
| 		cy.request('POST', '/api/signup', { | 		cy.registerUser('alice', 'alice1234'); | ||||||
| 			username: 'alice', |  | ||||||
| 			password: 'alice1234', |  | ||||||
| 		}).its('body').as('alice'); |  | ||||||
| 
 | 
 | ||||||
| 		cy.visit('/'); | 		cy.login('alice', 'alice1234'); | ||||||
| 
 |  | ||||||
| 		cy.intercept('POST', '/api/signin').as('signin'); |  | ||||||
| 
 |  | ||||||
| 		cy.get('[data-cy-signin]').click(); |  | ||||||
| 		cy.get('[data-cy-signin-username] input').type('alice'); |  | ||||||
| 		cy.get('[data-cy-signin-password] input').type('alice1234{enter}'); |  | ||||||
| 
 |  | ||||||
| 		cy.wait('@signin').as('signedIn'); |  | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	afterEach(() => { | 	afterEach(() => { | ||||||
|  |  | ||||||
|  | @ -1,34 +1,15 @@ | ||||||
| describe('After user signed in', () => { | describe('After user signed in', () => { | ||||||
| 	beforeEach(() => { | 	beforeEach(() => { | ||||||
| 		cy.window(win => { | 		cy.resetState(); | ||||||
| 			win.indexedDB.deleteDatabase('keyval-store'); |  | ||||||
| 		}); |  | ||||||
| 		cy.viewport('macbook-16'); | 		cy.viewport('macbook-16'); | ||||||
| 		cy.request('POST', '/api/reset-db').as('reset'); |  | ||||||
| 		cy.get('@reset').its('status').should('equal', 204); |  | ||||||
| 		cy.reload(true); |  | ||||||
| 
 | 
 | ||||||
| 		// インスタンス初期セットアップ
 | 		// インスタンス初期セットアップ
 | ||||||
| 		cy.request('POST', '/api/admin/accounts/create', { | 		cy.registerUser('admin', 'pass', true); | ||||||
| 			username: 'admin', |  | ||||||
| 			password: 'pass', |  | ||||||
| 		}).its('body').as('admin'); |  | ||||||
| 
 | 
 | ||||||
| 		// ユーザー作成
 | 		// ユーザー作成
 | ||||||
| 		cy.request('POST', '/api/signup', { | 		cy.registerUser('alice', 'alice1234'); | ||||||
| 			username: 'alice', |  | ||||||
| 			password: 'alice1234', |  | ||||||
| 		}).its('body').as('alice'); |  | ||||||
| 
 | 
 | ||||||
| 		cy.visit('/'); | 		cy.login('alice', 'alice1234'); | ||||||
| 
 |  | ||||||
| 		cy.intercept('POST', '/api/signin').as('signin'); |  | ||||||
| 
 |  | ||||||
| 		cy.get('[data-cy-signin]').click(); |  | ||||||
| 		cy.get('[data-cy-signin-username] input').type('alice'); |  | ||||||
| 		cy.get('[data-cy-signin-password] input').type('alice1234{enter}'); |  | ||||||
| 
 |  | ||||||
| 		cy.wait('@signin').as('signedIn'); |  | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	afterEach(() => { | 	afterEach(() => { | ||||||
|  |  | ||||||
|  | @ -23,3 +23,33 @@ | ||||||
| //
 | //
 | ||||||
| // -- This will overwrite an existing command --
 | // -- This will overwrite an existing command --
 | ||||||
| // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
 | // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
 | ||||||
|  | 
 | ||||||
|  | Cypress.Commands.add('resetState', () => { | ||||||
|  | 	cy.window(win => { | ||||||
|  | 		win.indexedDB.deleteDatabase('keyval-store'); | ||||||
|  | 	}); | ||||||
|  | 	cy.request('POST', '/api/reset-db').as('reset'); | ||||||
|  | 	cy.get('@reset').its('status').should('equal', 204); | ||||||
|  | 	cy.reload(true); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | Cypress.Commands.add('registerUser', (username, password, isAdmin = false) => { | ||||||
|  | 	const route = isAdmin ? '/api/admin/accounts/create' : '/api/signup'; | ||||||
|  | 
 | ||||||
|  | 	cy.request('POST', route, { | ||||||
|  | 		username: username, | ||||||
|  | 		password: password, | ||||||
|  | 	}).its('body').as(username); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | Cypress.Commands.add('login', (username, password) => { | ||||||
|  | 	cy.visit('/'); | ||||||
|  | 
 | ||||||
|  | 	cy.intercept('POST', '/api/signin').as('signin'); | ||||||
|  | 
 | ||||||
|  | 	cy.get('[data-cy-signin]').click(); | ||||||
|  | 	cy.get('[data-cy-signin-username] input').type(username); | ||||||
|  | 	cy.get('[data-cy-signin-password] input').type(`${password}{enter}`); | ||||||
|  | 
 | ||||||
|  | 	cy.wait('@signin').as('signedIn'); | ||||||
|  | }); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue