Skip to content

Commit a491ad1

Browse files
authored
Merge pull request #87 from mailchimp/enhancement/update-critical-flows-with-e2e-test-coverage
Enhancement/update critical flows with e2e test coverage
2 parents f675ce1 + 908aa97 commit a491ad1

34 files changed

+2362
-64
lines changed

.env.test.sample

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
##
2+
## Users
3+
##
4+
5+
# Main user for testing
6+
CYPRESS_MAILCHIMP_USERNAME="your mailchimp username"
7+
CYPRESS_MAILCHIMP_PASSWORD="your mailchimp password"
8+
9+
# Generate API Key: https://mailchimp.com/help/about-api-keys/
10+
CYPRESS_MAILCHIMP_API_KEY="generate an API key from your mailchimp account and paste here" # e.g. "1234567890abcdef1234567890abcdef-us19"
11+
CYPRESS_MAILCHIMP_API_SERVER_PREFIX="" # e.g "us19" - your server prefix may be different

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dist/*
77
docs-built
88
.phpunit.result.cache
99
/.wp-env.override.json
10+
.env.test
1011

1112
tests/cypress/videos
1213
tests/cypress/screenshots
@@ -16,4 +17,4 @@ tests/cypress/downloads
1617
mailchimp.zip
1718

1819
# IDE
19-
.vscode
20+
.vscode

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,13 @@ The `tests` directory contains end-to-end tests for the project, utilizing Cypre
9999
1. Run `npm install`.
100100
2. Run `npm run build`.
101101
3. Run `npm run env:start`.
102-
4. Set Mailchimp credentials as environment variables:
103-
- run `export CYPRESS_MAILCHIMP_USERNAME="your mailchimp username"`
104-
- run `export CYPRESS_MAILCHIMP_PASSWORD="your mailchimp password"`
105-
5. Run `npm run cypress:run`. You can also run `npm run cypress:open` to run tests in UI mode.
102+
4. Duplicate the `.env.test.sample` file and rename `.env.test`. Populate this file with your own Mailchimp account credentials.
103+
- **NOTE:** Use a test account that does not require 2FA. 2FA will fail some of the tests.
104+
5. Set your Mailchimp account up
105+
- Name the audience in your Mailchimp test account "10up". Required for `settings.test.js`.
106+
- Enable all merge fields. From your Mailchimp account home page -> `/audience/settings/` -> Edit merge fields/tags -> Set all merge fields to "visible". Required for `settings.test.js`.
107+
108+
6. Run `npm run cypress:run`. You can also run `npm run cypress:open` to run tests in UI mode.
106109

107110
## Support Level
108111

package-lock.json

Lines changed: 162 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
},
3333
"devDependencies": {
3434
"@10up/cypress-wp-utils": "^0.5.0",
35+
"@mailchimp/mailchimp_marketing": "^3.0.80",
3536
"@wordpress/env": "^10.2.0",
3637
"10up-toolkit": "^6.2.0",
3738
"cypress": "^13.13.2",
39+
"cypress-dotenv": "^3.0.1",
3840
"cypress-mochawesome-reporter": "^3.8.2",
3941
"mochawesome-json-to-md": "^1.3.5"
4042
},

tests/bin/initialize.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ set -e
33

44
npm run env run tests-wordpress chmod -- -c ugo+w /var/www/html
55
npm run env run tests-cli wp rewrite structure '/%postname%/' -- --hard
6+
7+
wp-env run tests-cli wp post create --post_title='Mailchimp signup form – shortcode' --post_type=page --post_status=publish --post_author=1 --post_content='<!-- wp:shortcode -->[mailchimpsf_form]<!-- /wp:shortcode -->'
8+
wp-env run tests-cli wp post create --post_title='Mailchimp signup form - Block' --post_type=page --post_status=publish --post_author=1 --post_content='<!-- wp:mailchimp/mailchimp /-->'

tests/cypress/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { defineConfig } = require( 'cypress' );
22
const { loadConfig } = require( '@wordpress/env/lib/config' );
33
const getCacheDirectory = require( '@wordpress/env/lib/config/get-cache-directory' );
4+
const dotenvPlugin = require('cypress-dotenv');
45

56
module.exports = defineConfig( {
67
chromeWebSecurity: false,
@@ -19,6 +20,7 @@ module.exports = defineConfig( {
1920
},
2021
e2e: {
2122
setupNodeEvents( on, config ) {
23+
config = dotenvPlugin(config, { path: '../../.env.test' }); // Load .env variables into Cypress
2224
return setBaseUrl( on, config );
2325
},
2426
specPattern: [
@@ -30,6 +32,7 @@ module.exports = defineConfig( {
3032
],
3133
supportFile: 'tests/cypress/support/index.js',
3234
defaultCommandTimeout: 20000,
35+
experimentalRunAllSpecs: true,
3336
},
3437
retries: {
3538
runMode: 2,

tests/cypress/e2e/connect.test.js

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,15 @@ describe('Admin can connect to "Mailchimp" Account', () => {
1414
}
1515
});
1616

17-
// Check Mailchimp menu.
18-
cy.get('#mailchimp_sf_oauth_connect').should('exist');
19-
20-
// Enable popup capture.
21-
cy.capturePopup();
22-
23-
cy.get('#mailchimp_sf_oauth_connect').click();
24-
cy.wait(6000);
25-
26-
// Accept cookie consent popup window (if present)
27-
cy.popup().then(($popup) => {
28-
const acceptButtonSelector = '#onetrust-accept-btn-handler';
29-
30-
// Check if the accept button is visible and click it
31-
if ($popup.find(acceptButtonSelector).length > 0 && $popup.find(acceptButtonSelector).is(':visible')) {
32-
$popup.find(acceptButtonSelector).click();
33-
} else {
34-
cy.log('Cookie consent popup not found or not visible.');
35-
}
36-
});
37-
38-
cy.popup()
39-
.find('input#username')
40-
.clear()
41-
.type(Cypress.env('MAILCHIMP_USERNAME'), { force: true });
42-
cy.popup()
43-
.find('input#password')
44-
.clear()
45-
.type(Cypress.env('MAILCHIMP_PASSWORD'), { force: true });
46-
cy.popup().find('button[type="submit"]').click({ force: true });
47-
cy.wait(10000); // Not a best practice, but did not find a better way to handle this.
48-
49-
cy.popup().find('input#submitButton').click({ force: true });
50-
cy.wait(10000); // Not a best practice, but did not find a better way to handle this.
17+
// Mailchimp connection login as a command in order to be reusable
18+
cy.mailchimpLogin();
5119

20+
// Logout exists
5221
cy.get('.mc-user h3').contains('Logged in as: ');
5322
cy.get('input[value="Logout"]').should('exist');
23+
24+
// Mailchimp lists exists and has at least one audience
25+
cy.get('#mc_list_id').should('exist');
26+
cy.get('#mc_list_id').children().should('have.length.greaterThan', 1); // The " — Select A List — " default option will always be present
5427
});
5528
});

tests/cypress/e2e/admin.test.js renamed to tests/cypress/e2e/settings/admin.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ describe('Admin can login and make sure plugin is activated', () => {
2323
it('Admin can see "Create account" button and Can visit "Create account" settings page.', () => {
2424
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
2525

26+
// If logged in then log out
27+
cy.mailchimpLogout();
28+
2629
// Check Create account button.
2730
cy.get('.button.mailchimp-sf-button.button-secondary').should('be.visible');
2831
cy.get('.button.mailchimp-sf-button.button-secondary').contains('Create an account');
@@ -35,6 +38,9 @@ describe('Admin can login and make sure plugin is activated', () => {
3538
it("Admin shouldn't be able to submit create account form with invalid data", () => {
3639
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_create_account');
3740

41+
// If logged in then log out
42+
cy.mailchimpLogout();
43+
3844
// Submit form without filling any data.
3945
cy.get('#mailchimp-sf-create-activate-account').click();
4046

0 commit comments

Comments
 (0)