Skip to content

Commit 8cb0929

Browse files
authored
Merge pull request #48 from mailchimp/enhancement/e2e-tests
Add E2E tests
2 parents 57c0058 + 4fe1d6e commit 8cb0929

File tree

11 files changed

+380
-41
lines changed

11 files changed

+380
-41
lines changed

.github/workflows/e2e.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ on:
1010
- develop
1111

1212
jobs:
13+
build:
14+
uses: mailchimp/wordpress/.github/workflows/build-release-zip.yml@develop
15+
1316
cypress:
17+
needs: build
1418
name: ${{ matrix.core.name }}
1519
runs-on: ubuntu-latest
20+
env:
21+
CYPRESS_MAILCHIMP_USERNAME: ${{ secrets.MAILCHIMP_USERNAME }}
22+
CYPRESS_MAILCHIMP_PASSWORD: ${{ secrets.MAILCHIMP_PASSWORD }}
1623
strategy:
1724
matrix:
1825
core:
@@ -24,15 +31,25 @@ jobs:
2431
- name: Checkout
2532
uses: actions/checkout@v4
2633

34+
- name: Download build zip
35+
uses: actions/download-artifact@v4
36+
with:
37+
name: ${{ github.event.repository.name }}
38+
path: ${{ github.event.repository.name }}
39+
40+
- name: Display structure of downloaded files
41+
run: ls -R
42+
working-directory: ${{ github.event.repository.name }}
43+
2744
- uses: actions/setup-node@v4
2845
with:
2946
node-version-file: '.nvmrc'
3047

3148
- name: Install dependencies
3249
run: npm ci
3350

34-
- name: Set the core version
35-
run: ./tests/bin/set-core-version.js ${{ matrix.core.version }}
51+
- name: Set the core version and plugins config
52+
run: ./tests/bin/set-core-version.js --core=${{ matrix.core.version }} --plugins=./${{ github.event.repository.name }}
3653

3754
- name: Set up WP environment
3855
run: npm run env:start
@@ -41,6 +58,7 @@ jobs:
4158
run: npm run cypress:run
4259

4360
- name: Update summary
61+
if: always()
4462
run: |
4563
npx mochawesome-merge ./tests/cypress/reports/*.json -o tests/cypress/reports/mochawesome.json
4664
rm -rf ./tests/cypress/reports/mochawesome-*.json

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ Currently we have the plugin configured so it can be translated and the followin
104104
* sv_SE - Swedish in Sweden (thanks to [Sebastian Johnsson](http://www.agiley.se/) for contributing)
105105
* tr_TR - Turkish in Turkey (thanks to [Hakan E.](http://kazancexpert.com/) for contributing)
106106

107+
## E2E tests
108+
The `tests` directory contains end-to-end tests for the project, utilizing Cypress to run tests in an environment created using wp-env.
109+
110+
### Pre-requisites
111+
- Node.js v20
112+
- Docker
113+
- Create an account in [Mailchimp](https://mailchimp.com/)
114+
115+
### Run E2E tests in local
116+
1. Run `npm install`.
117+
2. Run `npm run build`.
118+
3. Run `npm run env:start`.
119+
4. Set Mailchimp credentials as environment variables:
120+
- run `export CYPRESS_MAILCHIMP_USERNAME="your mailchimp username"`
121+
- run `export CYPRESS_MAILCHIMP_PASSWORD="your mailchimp password"`
122+
5. Run `npm run cypress:run`. You can also run `npm run cypress:open` to run tests in UI mode.
123+
107124
## Support Level
108125

109126
**Active:** Mailchimp is actively working on this, and we expect to continue work for the foreseeable future including keeping tested up to the most recent version of WordPress. Bug reports, feature requests, questions, and pull requests are welcome.

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"scripts": {
2121
"cypress:open": "cypress open --config-file tests/cypress/config.js --e2e --browser chrome",
22-
"cypress:run": "cypress run --config-file tests/cypress/config.js",
22+
"cypress:run": "cypress run --config-file tests/cypress/config.js --e2e --browser chrome",
2323
"env": "wp-env",
2424
"env:start": "wp-env start",
2525
"env:stop": "wp-env stop",
@@ -31,10 +31,10 @@
3131
"build": "10up-toolkit build"
3232
},
3333
"devDependencies": {
34-
"@10up/cypress-wp-utils": "^0.3.0",
34+
"@10up/cypress-wp-utils": "^0.4.0",
3535
"@wordpress/env": "^10.2.0",
3636
"10up-toolkit": "^6.2.0",
37-
"cypress": "^13.12.0",
37+
"cypress": "^13.13.2",
3838
"cypress-mochawesome-reporter": "^3.8.2",
3939
"mochawesome-json-to-md": "^1.3.5"
4040
},

tests/bin/set-core-version.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
#!/usr/bin/env node
22

33
const fs = require( 'fs' );
4-
const { exit } = require( 'process' );
54

6-
const path = `${ process.cwd() }/.wp-env.override.json`;
5+
const path = `${ process.cwd() }/.wp-env.json`;
76

8-
// eslint-disable-next-line import/no-dynamic-require
9-
const config = fs.existsSync( path ) ? require( path ) : {};
7+
let config = fs.existsSync( path ) ? require( path ) : { plugins: [ '.' ] };
108

11-
const args = process.argv.slice( 2 );
9+
const args = {};
10+
process.argv
11+
.slice(2, process.argv.length)
12+
.forEach( arg => {
13+
if (arg.slice(0,2) === '--') {
14+
const param = arg.split('=');
15+
const paramName = param[0].slice(2,param[0].length);
16+
const paramValue = param.length > 1 ? param[1] : true;
17+
args[paramName] = paramValue;
18+
}
19+
});
1220

13-
if ( args.length === 0 ) exit( 0 );
21+
if ( ! args.core && ! args.plugins ) {
22+
return;
23+
}
24+
25+
if ( 'latest' === args.core ) {
26+
delete args.core;
27+
}
1428

15-
if ( args[ 0 ] === 'latest' ) {
16-
if ( fs.existsSync( path ) ) {
17-
fs.unlinkSync( path );
18-
}
19-
exit( 0 );
29+
if( Object.keys(args).length === 0 ) {
30+
return;
2031
}
2132

22-
config.core = args[ 0 ];
33+
if ( args.plugins ) {
34+
args.plugins = args.plugins.split(',');
35+
}
2336

24-
// eslint-disable-next-line no-useless-escape
25-
if ( ! config.core.match( /^WordPress\/WordPress\#/ ) ) {
26-
config.core = `WordPress/WordPress#${ config.core }`;
37+
config = {
38+
...config,
39+
...args,
2740
}
2841

2942
try {
30-
fs.writeFileSync( path, JSON.stringify( config ) );
43+
fs.writeFileSync( path, JSON.stringify( config ) );
3144
} catch ( err ) {
32-
// eslint-disable-next-line no-console
33-
console.error( err );
45+
console.error( err );
3446
}

tests/cypress/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ module.exports = defineConfig( {
2525
supportFile: 'tests/cypress/support/index.js',
2626
defaultCommandTimeout: 20000,
2727
},
28+
retries: {
29+
runMode: 2,
30+
openMode: 0,
31+
},
2832
} );
2933

3034
/**

tests/cypress/e2e/admin.test.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
describe( 'Admin can login and make sure plugin is activated', () => {
2-
before( () => {
1+
/* eslint-disable no-undef */
2+
describe('Admin can login and make sure plugin is activated', () => {
3+
before(() => {
34
cy.login();
4-
} );
5+
});
56

6-
it( 'Can deactivate and activate plugin?', () => {
7-
cy.deactivatePlugin( 'mailchimp' );
8-
cy.activatePlugin( 'mailchimp' );
9-
} );
10-
} );
7+
it('Can deactivate and activate plugin?', () => {
8+
cy.deactivatePlugin('mailchimp');
9+
cy.activatePlugin('mailchimp');
10+
});
11+
12+
it('Can see "Mailchimp" menu and Can visit "Mailchimp" settings page.', () => {
13+
cy.visit('/wp-admin/');
14+
15+
// Check Mailchimp menu.
16+
cy.get('#adminmenu li#toplevel_page_mailchimp_sf_options').contains('Mailchimp');
17+
18+
// Check Heading
19+
cy.get('#adminmenu li#toplevel_page_mailchimp_sf_options').click();
20+
cy.get('#wpbody .mailchimp-header h1').contains('Mailchimp List Subscribe Form');
21+
});
22+
});

tests/cypress/e2e/connect.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable no-undef */
2+
describe('Admin can connect to "Mailchimp" Account', () => {
3+
before(() => {
4+
cy.login();
5+
});
6+
7+
it('Can connect to "Mailchimp" using OAuth flow.', () => {
8+
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
9+
10+
// Logout if already connected.
11+
cy.get('body').then(($body) => {
12+
if ($body.find('input[value="Logout"]').length > 0) {
13+
cy.get('input[value="Logout"]').click();
14+
}
15+
});
16+
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+
cy.popup()
27+
.find('input#username')
28+
.clear()
29+
.type(Cypress.env('MAILCHIMP_USERNAME'), { force: true });
30+
cy.popup()
31+
.find('input#password')
32+
.clear()
33+
.type(Cypress.env('MAILCHIMP_PASSWORD'), { force: true });
34+
cy.popup().find('button[type="submit"]').click({ force: true });
35+
cy.wait(10000); // Not a best practice, but did not find a better way to handle this.
36+
37+
cy.popup().find('input#submitButton').click({ force: true });
38+
cy.wait(10000); // Not a best practice, but did not find a better way to handle this.
39+
40+
cy.get('.mc-user h3').contains('Logged in as: ');
41+
cy.get('input[value="Logout"]').should('exist');
42+
});
43+
});

0 commit comments

Comments
 (0)