Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cypress Tests events API for get-experience page #329

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ describe( 'Start Setup WP Experience Page', function () {
} );
} );

const EventsAPI = ( experience_val ) => {
cy.intercept('http://localhost:8882/index.php?rest_route=%2Fnewfold-onboarding%2Fv1%2Fevents%2Fbatch&flow=wp-setup&_locale=user').as('events');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this URL into a common constant? Additionally, could we consider using a regex that matches events/batch instead of the entire URL? https://docs.cypress.io/api/commands/intercept#Icon-nameangle-right--url-String-Glob-RegExp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for now I have moved all the API in a single file. I'll do the regex that matches the URL also.

cy.wait('@events').then((interception) => {
avneet-raj marked this conversation as resolved.
Show resolved Hide resolved
const responseBody = interception.request.body;
const responseData1 = responseBody[0].data;
const responseData2 = responseBody[1].data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between responseData1 and responseData2? Do we require both of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for the edge case, sometimes the experience _level comes into the second data and sometimes in the first. I've refined it more now so that, it doesn't fail. 90% of the time it'll come in the resposeData1.

if("experience_level" in responseData1){
expect(responseData1.experience_level).equal(experience_val);
};
if("experience_level" in responseData2){
expect(responseData2.experience_level).equal(experience_val);
};
});
};

it( 'Check if events API call being made after radio buttons are clicked', () => {
let radioCount = 0;
const className = '.components-radio-control__option';
const arr = cy.get( className );
arr.each( () => {
cy.reload();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to reload the page every time we click a radio button? That's not in line with how the user will be clicking the buttons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this'll be needed to clear the older network logs as every time when a radio button is clicked, the API call is overlapped on the earlier one (happening only on cypress). So, to eliminate this, we have to use reload before every radio button click.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed reload.

cy.wait(2000);
cy.get( '[type="radio"]' )
.eq( radioCount )
.click( { force: true } )
if(radioCount==0){
EventsAPI("novice");
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
};
if(radioCount==1){
EventsAPI("intermediate");
};
if(radioCount>1){
cy.wait(5000);
EventsAPI("expert");
};
radioCount += 1;
} );
} );

it( 'Checks if Continue Setup Button is Enabled after the Radio Button is Checked.', () => {
cy.get( '[type=radio]:checked' ).should(
'have.css',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ describe( 'Start Setup WP Experience Page', function () {
} );
} );

const EventsAPI = ( experience_val ) => {
cy.intercept('http://localhost:8882/index.php?rest_route=%2Fnewfold-onboarding%2Fv1%2Fevents%2Fbatch&flow=ecommerce&_locale=user').as('events');
cy.wait('@events').then((interception) => {
const responseBody = interception.request.body;
const responseData1 = responseBody[0].data;
const responseData2 = responseBody[1].data;
if("experience_level" in responseData1){
expect(responseData1.experience_level).equal(experience_val);
};
if("experience_level" in responseData2){
expect(responseData2.experience_level).equal(experience_val);
};
});
};

it( 'Check if events API call being made after radio buttons are clicked', () => {
let radioCount = 0;
const className = '.components-radio-control__option';
const arr = cy.get( className );
arr.each( () => {
cy.reload();
cy.wait(2000);
cy.get( '[type="radio"]' )
.eq( radioCount )
.click( { force: true } )
if(radioCount==0){
EventsAPI("novice");
};
if(radioCount==1){
EventsAPI("intermediate");
};
if(radioCount>1){
cy.wait(5000);
EventsAPI("expert");
};
radioCount += 1;
} );
} );

it( 'Checks if Continue Setup Button is Enabled after the Radio Button is Checked.', () => {
cy.get( '[type=radio]:checked' ).should(
'have.css',
Expand Down