NCIOCPL / clinical-trials-search-app

Clinical Trial Search Front-end
1 stars 6 forks source link

Refactor Cypress Step Definitions to Reduce Duplication #629

Open adriancofie opened 4 months ago

adriancofie commented 4 months ago

TLDR: Reduce duplication, confusion, and non-deterministic code.

Currently our version of cypress-cucumber-preprocessor is far outdated and has some inherent deficiencies which required structuring the tests in a non-intuitive fashion.

This ticket is to update the preprocessor to the latest version (Note: the project maintainer changed hands a while ago and it's now @badeball/cypress-cucumber-preprocessor) and refactor the test cases to follow best practices where ideal.

There have been several breaking changes in the preprocessor and the version is now at v20.0.5 while we are at v4. We don't need to upgrade to the latest version but we should at the very least update to a version that allows us to have a sane directory structure.

Newer versions follow a well defined approach for matching step definitions:

https://github.com/badeball/cypress-cucumber-preprocessor/blob/master/docs/step-definitions.md


Note:

We currently have several duplicated step definitions, in the version we're using the preprocessor uses the first step definition it finds and ignores the rest, however looking statically at the code a developer would have no idea which definition to update in order make changes, nor know which ones not to change in order not to break something.

In later versions the preprocessor was updated to explicitly disallow step definitions from having duplicates-- this is the approach that cucumber itself follows (https://cucumber.io/docs/community/faq/?lang=java#duplicate-step-definition). Note this applies across defineStep types ('Given', 'Then' etc).


Note:

From version 13.0.0 onward support for AND and BUT is removed.

https://github.com/badeball/cypress-cucumber-preprocessor/issues/821

See Release Notes for v13.0.0 https://github.com/badeball/cypress-cucumber-preprocessor/releases


Sub-Tasks

Nitty Gritty

Identical Step Definition Text and Functions

common/index.js identical duplications:

Given('trial info displays {string}', (infoText) => {...}):


BasicSearchPage.js, 
AdvancedSearchPage/AgeSection/index.js,
AdvancedSearchPage/KeywordsSection/index.js, 
AdvancedSearchPage/LeadOrganizationSection/index.js,
AdvancedSearchPage/TrialInvestigatorsSection/index.js, 
AdvancedSearchPage/TrialPhaseSection/index.js,
AdvancedSearchPage/TrialTypeSection/index.js, 
AdvancedSearchPage/DrugTreatmentSection/index.js,
AdvancedSearchPage/CancerTypeCondition/index.js, 
SearchResultsPage/GeneralPageComponents/index.js.

Given('the criteria table displays the following', (dataTable) => {...}):

 BasicSearchPage.js
 AdvancedSearchPage/AgeSection/index.js
 AdvancedSearchPage/KeywordsSection/index.js
 AdvancedSearchPage/LeadOrganizationSection/index.js
 AdvancedSearchPage/TrialInvestigatorsSection/index.js
 AdvancedSearchPage/TrialIdSection/index.js
 AdvancedSearchPage/TrialPhaseSection/index.js
 AdvancedSearchPage/TrialTypeSection/index.js
 AdvancedSearchPage/DrugTreatmentSection/index.js
 AdvancedSearchPage/CancerTypeCondition/index.js
SearchResultsPage/GeneralPageComponents/index.js

When('user clicks on Modify Search Criteria button', () => {...}):

BasicSearchPage.js
 AdvancedSearchPage/AgeSection/index.js
 AdvancedSearchPage/KeywordsSection/index.js
 AdvancedSearchPage/LeadOrganizationSection/index.js
 AdvancedSearchPage/TrialInvestigatorsSection/index.js
 AdvancedSearchPage/TrialIdSection/index.js
 AdvancedSearchPage/TrialPhaseSection/index.js
 AdvancedSearchPage/TrialTypeSection/index.js
 AdvancedSearchPage/DrugTreatmentSection/index.js
 AdvancedSearchPage/CancerTypeCondition/index.js
 SearchResultsPage/GeneralPageComponents/index.js

locationSection.js identical duplications:

Given('trial info displays {string}', (infoText) => {...}):


AdvancedSearchPage/LocationSection/LocationLimitedToVA/locationSection.js 
 AdvancedSearchPage/LocationSection/LocationByZipcode/locationSection.js

Given('the criteria table displays the following', (dataTable) => {...}):

AdvancedSearchPage/LocationSection/LocationLimitedToVA/locationSection.js
AdvancedSearchPage/LocationSection/LocationByZipcode/locationSection.js

When('user clicks on Modify Search Criteria button', () => {...}):

AdvancedSearchPage/LocationSection/LocationLimitedToVA/locationSection.js
AdvancedSearchPage/LocationSection/LocationByZipcode/locationSection.js

common/MetaTags.js identical duplications:

Then('the title tag should be {string}', (expectedTitle) => {...}):

BasicSearchPage.js
SearchResultsPage/GeneralPageComponents/index.js

Identical step definition text, differing function definition

The following are areas for potential abstraction where the step definition text is the same but the logic is different. The text should either be updated to be something different, or the function should be made non-anonymous and overridden where necessary.

Given('{string} no trial info is displayed', (noTrialsText) => {...}):

AdvancedSearchPage/LocationSection/LocationLimitedToVA/locationSection.js
AdvancedSearchPage/LocationSection/LocationByZipcode/locationSection.js
Components/Autocomplete/index.js 
SearchResultsPage/GeneralPageComponents/index.js

Given('text {string} is displayed', (text) => {...}):

TrialDescriptionPage/Accordion/index.js 
SearchResultsPage/GeneralPageComponents/index.js

Given('search criteria table is not displayed', () => {...}):

TrialDescriptionPage/GeneralPageComponents/index.js 
SearchResultsPage/GeneralPageComponents/index.js

When('user clicks on {int} trial link', (resItemIndex) => {...}):

AdvancedSearchPage/LocationSection/LocationLimitedToVA/locationSection.js 
AdvancedSearchPage/LocationSection/LocationByZipcode/locationSection.js 

When('user clicks on {string} section of accordion', (section) => {...}):

AdvancedSearchPage/LocationSection/LocationLimitedToVA/locationSection.js
AdvancedSearchPage/LocationSection/LocationByZipcode/locationSection.js
TrialDescriptionPage/Accordion/index.js