Skip to content

Commit

Permalink
Fix Gravity Forms tests (#2214)
Browse files Browse the repository at this point in the history
They are flaky sometimes, hopefully these changes will help
  • Loading branch information
mleray committed Feb 14, 2024
1 parent caff78b commit b41f491
Showing 1 changed file with 75 additions and 72 deletions.
147 changes: 75 additions & 72 deletions tests/e2e/gravity-forms.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,92 @@ const TEST_FIRST_NAME = 'Jon';
const TEST_LAST_NAME = 'Snow';
const TEST_EMAIL = 'jon.snow@gmail.com';

let createdForm;

test.useAdminLoggedIn();

test.beforeAll(async ({browser}) => {
const page = await browser.newPage();
test.describe('Gravity Forms tests', () => {
test.describe.configure({mode: 'serial'});
const testId = Math.floor(Math.random() * 10000); //NOSONAR
let createdForm;

// Enable Gravity Forms rest API.
await toggleRestAPI({page}, true);
test.beforeAll(async ({browser}) => {
const page = await browser.newPage();
// Enable Gravity Forms rest API.
await toggleRestAPI({page}, true);

// Create a new form.
createdForm = await createForm({page}, {
title: 'Gravity Forms test form',
// Create a new form.
createdForm = await createForm({page}, {
title: `Gravity Forms test form - ${testId}`,
});
await page.close();
});
});

test.afterAll(async ({browser}) => {
const page = await browser.newPage();
test.afterAll(async ({browser}) => {
const page = await browser.newPage();
// Delete the form.
await page.request.delete(`./wp-json/gf/v2/forms/${createdForm.id}?force=1`);

// Delete the form.
await page.request.delete(`./wp-json/gf/v2/forms/${createdForm.id}?force=1`);

// Disable Gravity Forms rest API.
await toggleRestAPI({page}, false);
});

test('check the Gravity Forms confirmation message, text type', async ({requestUtils, page}) => {
// Update the form's default confirmation text.
await page.goto(`./wp-admin/admin.php?page=gf_edit_forms&view=settings&subview=confirmation&id=${createdForm.id}`);
await page.locator('#the-list').hover();
await page.getByRole('link', {name: 'Edit', exact: true}).click();
await page.frameLocator('#_gform_setting_message_ifr').locator('#tinymce').fill(CONFIRMATION_MESSAGE);
await page.getByRole('button', {name: 'Save Confirmation'}).click();
await expect(page.locator('.gforms_note_success')).toBeVisible();

// Create a new post with the new form.
const newPost = await requestUtils.createPost({
title: 'Gravity Forms test for confirmation message, text type',
content: `<!-- wp:gravityforms/form {"formId":"${createdForm.id}"} /-->`,
status: 'publish',
// Disable Gravity Forms rest API.
await toggleRestAPI({page}, false);
});
await page.goto(newPost.link);

// Fill and submit the form and check the confirmation message text.
const form = page.locator('form[id^="gform"]');
await form.getByLabel('First name').fill(TEST_FIRST_NAME);
await form.getByLabel('Last name').fill(TEST_LAST_NAME);
await form.getByLabel('Email').fill(TEST_EMAIL);
const submitButton = form.getByRole('button', {name: 'Submit'});
await submitButton.click();
const confirmationMessage = page.locator('.gform_confirmation_message');
await expect(confirmationMessage).toBeVisible();
await expect(confirmationMessage).toContainText(CONFIRMATION_MESSAGE);
test('check the confirmation message, text type', async ({requestUtils, page}) => {
// Update the form's default confirmation text.
await page.goto(`./wp-admin/admin.php?page=gf_edit_forms&view=settings&subview=confirmation&id=${createdForm.id}`);
await page.locator('#the-list > tr:first-child').hover();
await page.getByRole('link', {name: 'Edit', exact: true}).click();
await page.frameLocator('#_gform_setting_message_ifr').locator('#tinymce').fill(CONFIRMATION_MESSAGE);
await page.getByRole('button', {name: 'Save Confirmation'}).click();
await expect(page.locator('.gforms_note_success')).toBeVisible();

// Check that the entry has been registered as expected.
await page.goto('./wp-admin/admin.php?page=gf_entries');
const latestEntry = page.locator('#the-list > tr.entry_row').first();
await expect(latestEntry).toBeVisible();
await expect(latestEntry.locator('td[data-colname="First name"]')).toContainText(TEST_FIRST_NAME);
await expect(latestEntry.locator('td[data-colname="Last name"]')).toContainText(TEST_LAST_NAME);
await expect(latestEntry.locator('td[data-colname="Email"]')).toContainText(TEST_EMAIL);
});
// Create a new post with the new form.
const newPost = await requestUtils.createPost({
title: `Gravity Forms test post - ${testId}`,
content: `<!-- wp:gravityforms/form {"formId":"${createdForm.id}"} /-->`,
status: 'publish',
});
await page.goto(newPost.link);

test('check the Gravity Forms Hubspot feeds', async ({page}) => {
// Add a Hubspot feed.
await page.goto(`./wp-admin/admin.php?page=gf_edit_forms&view=settings&subview=gravityformshubspot&id=${createdForm.id}`);
const createFeedLink = page.getByRole('link', {name: 'create one'});
// If the Huspot add-on isn't activated, the page will be empty so this link won't be there.
if (await createFeedLink.isVisible()) {
await createFeedLink.click();
// Set lead status to "new".
await page.selectOption('#_hs_customer_hs_lead_status', {label: 'New'});
// Map contact fields.
await page.selectOption('#_hs_customer_firstname', {label: 'First name'});
await page.selectOption('#_hs_customer_lastname', {label: 'Last name'});
await page.selectOption('#_hs_customer_email', {label: 'Email'});
// Save Hubspot feed.
await page.getByRole('button', {name: 'Save Settings'}).click();
await expect(page.locator('.gforms_note_success')).toBeVisible();
// Fill and submit the form and check the confirmation message text.
const form = page.locator('form[id^="gform"]');
await form.getByLabel('First name').fill(TEST_FIRST_NAME);
await form.getByLabel('Last name').fill(TEST_LAST_NAME);
await form.getByLabel('Email').fill(TEST_EMAIL);
const submitButton = form.getByRole('button', {name: 'Submit'});
await submitButton.click();
const confirmationMessage = page.locator('.gform_confirmation_message');
await expect(confirmationMessage).toBeVisible();
await expect(confirmationMessage).toContainText(CONFIRMATION_MESSAGE);

// Check that the entry has been registered as expected.
await page.goto('./wp-admin/admin.php?page=gf_entries');
const latestEntry = page.locator('#the-list > tr.entry_row').first();
await expect(latestEntry).toBeVisible();
await expect(latestEntry.locator('td[data-colname="First name"]')).toContainText(TEST_FIRST_NAME);
await expect(latestEntry.locator('td[data-colname="Last name"]')).toContainText(TEST_LAST_NAME);
await expect(latestEntry.locator('td[data-colname="Email"]')).toContainText(TEST_EMAIL);
});

// Check that the feed is active.
test('check the Hubspot feeds', async ({page}) => {
// Add a Hubspot feed.
await page.goto(`./wp-admin/admin.php?page=gf_edit_forms&view=settings&subview=gravityformshubspot&id=${createdForm.id}`);
const hubspotFeed = page.locator('#the-list > tr').first();
await expect(hubspotFeed.locator('.gform-status-indicator-status')).toContainText('Active');
}
const createFeedLink = page.getByRole('link', {name: 'create one'});
// If the Huspot add-on isn't activated, the page will be empty so this link won't be there.
if (await createFeedLink.isVisible()) {
await createFeedLink.click();
// Set lead status to "new".
await page.selectOption('#_hs_customer_hs_lead_status', {label: 'New'});
// Map contact fields.
await page.selectOption('#_hs_customer_firstname', {label: 'First name'});
await page.selectOption('#_hs_customer_lastname', {label: 'Last name'});
await page.selectOption('#_hs_customer_email', {label: 'Email'});
// Save Hubspot feed.
await page.getByRole('button', {name: 'Save Settings'}).click();
await expect(page.locator('.gforms_note_success')).toBeVisible();

// Check that the feed is active.
await page.goto(`./wp-admin/admin.php?page=gf_edit_forms&view=settings&subview=gravityformshubspot&id=${createdForm.id}`);
const hubspotFeed = page.locator('#the-list > tr').first();
await expect(hubspotFeed.locator('.gform-status-indicator-status')).toContainText('Active');
}
});
});

0 comments on commit b41f491

Please sign in to comment.