Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions src/plugins/einstein-bots/disable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "../schema.json",
"settings": {
"einsteinBots": {
"enabled": false
}
}
}

9 changes: 9 additions & 0 deletions src/plugins/einstein-bots/enable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "../schema.json",
"settings": {
"einsteinBots": {
"enabled": true
}
}
}

36 changes: 36 additions & 0 deletions src/plugins/einstein-bots/index.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import assert from 'assert';
import { EinsteinBots } from './index.js';

describe(EinsteinBots.name, function () {
this.timeout('10m');
let plugin: EinsteinBots;
before(() => {
plugin = new EinsteinBots(global.browserforce);
});

const configEnabled = {
enabled: true,
};
const configDisabled = {
enabled: false,
};

it('should enable Einstein Bots', async () => {
await plugin.run(configEnabled);
});

it('should verify Einstein Bots is enabled', async () => {
const res = await plugin.retrieve();
assert.deepStrictEqual(res, configEnabled);
});

it('should disable Einstein Bots', async () => {
await plugin.run(configDisabled);
});

it('should verify Einstein Bots is disabled', async () => {
const res = await plugin.retrieve();
assert.deepStrictEqual(res, configDisabled);
});
});

48 changes: 48 additions & 0 deletions src/plugins/einstein-bots/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { BrowserforcePlugin } from '../../plugin.js';

const BASE_PATH = '/lightning/setup/EinsteinBots/home';

// The checkbox input element (for reading state and clicking)
const TOGGLE_INPUT_SELECTOR = 'input[type="checkbox"][aria-label*="Einstein Bots"]';

type Config = {
enabled: boolean;
};

export class EinsteinBots extends BrowserforcePlugin {
public async retrieve(): Promise<Config> {
await using page = await this.browserforce.openPage(BASE_PATH);
await page.locator(TOGGLE_INPUT_SELECTOR).waitFor();
const response = {
enabled: await page.locator(TOGGLE_INPUT_SELECTOR).isChecked(),
};
Comment on lines +14 to +18
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
await using page = await this.browserforce.openPage(BASE_PATH);
await page.locator(TOGGLE_INPUT_SELECTOR).waitFor();
const response = {
enabled: await page.locator(TOGGLE_INPUT_SELECTOR).isChecked(),
};
const botSettings = await this.browserforce.connection.metadata.read('BotSettings', 'BotSettings');
const response = {
enabled: botSettings.enableBots,
};

return response;
}

public async apply(config: Config): Promise<void> {
await using page = await this.browserforce.openPage(BASE_PATH);
await page.locator(TOGGLE_INPUT_SELECTOR).waitFor();
const currentState = await page.locator(TOGGLE_INPUT_SELECTOR).isChecked();
// Only click if the state needs to change
if (currentState !== config.enabled) {
if (!config.enabled) {
// When disabling, click the toggle and wait for the confirmation dialog
await page.locator(TOGGLE_INPUT_SELECTOR).click({ force: true });
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I assume the click event on the input is not working.

Please consider clicking on the parent lightning-primitive-input-toggle element:

div#setupComponent lightning-primitive-input-toggle:has(input[role="switch"]:not(:disabled))


// Wait for the "Disable Einstein Bots" dialog and click Yes
const disableDialog = page.getByRole('dialog', { name: 'Disable Einstein Bots' });
await disableDialog.waitFor({ timeout: 5000 });
await disableDialog.getByRole('button', { name: 'Yes' }).click();

// Wait for the dialog to close (this is when the save happens)
await disableDialog.waitFor({ state: 'hidden', timeout: 10000 });
} else {
// When enabling, just click the toggle
await page.locator(TOGGLE_INPUT_SELECTOR).click({ force: true });
}

// Wait for the save to complete
await page.waitForTimeout(2000);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Sleeping is not a good practice.
Consider something like this:

Suggested change
await page.waitForTimeout(2000);
await page.waitForResponse(/ChatbotSetup\.setPrefs=1/)

}
}
}
14 changes: 14 additions & 0 deletions src/plugins/einstein-bots/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://github.com/amtrack/sfdx-browserforce-plugin/src/plugins/einstein-bots/schema.json",
"title": "Einstein Bots",
"type": "object",
"properties": {
"enabled": {
"title": "Einstein Bots",
"description": "Enable or disable Einstein Bots. Setup -> Einstein Bots -> Einstein Bots toggle.",
Comment on lines +8 to +9
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please include the reason why this is necessary.

Suggested change
"title": "Einstein Bots",
"description": "Enable or disable Einstein Bots. Setup -> Einstein Bots -> Einstein Bots toggle.",
"title": "Enable or disable Einstein Bots. Setup -> Einstein Bots -> Einstein Bots toggle.",
"Although the Metadata API has a BotSettings.enableBots field, it is not possible to enable this setting using an API. Error: Legal Terms acceptance and/or necessary feature dependencies required to enable Bot Settings.",

"type": "boolean"
}
}
}

2 changes: 2 additions & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ActivitySettings as activitySettings } from './activity-settings/index.
import { CompanyInformation as companyInformation } from './company-information/index.js';
import { CustomerPortal as customerPortal } from './customer-portal/index.js';
import { DensitySettings as densitySettings } from './density-settings/index.js';
import { EinsteinBots as einsteinBots } from './einstein-bots/index.js';
import { EmailDeliverability as emailDeliverability } from './email-deliverability/index.js';
import { HighVelocitySalesSettings as highVelocitySalesSettings } from './high-velocity-sales-settings/index.js';
import { HistoryTracking as historyTracking } from './history-tracking/index.js';
Expand All @@ -27,6 +28,7 @@ export {
companyInformation,
customerPortal,
densitySettings,
einsteinBots,
emailDeliverability,
highVelocitySalesSettings,
historyTracking,
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"densitySettings": {
"$ref": "./density-settings/schema.json"
},
"einsteinBots": {
"$ref": "./einstein-bots/schema.json"
},
"historyTracking": {
"$ref": "./history-tracking/schema.json"
},
Expand Down