Playwright Automation Framework for Salesforce
This blog will guide you through setting up a Playwright automation
framework using JavaScript and Page Object Model (POM) for Salesforce login and
account creation.
📂 Project Structure
project-root/
├── config/
│ └── constants.js
├── pages/
│ ├── LoginPage.js
│ ├── HomePage.js
│ └── AccountsPage.js
├── tests/
│ └── salesforce.test.js
├──
playwright.config.js
└──
package.json
⚙ Configuration Files
config/constants.js
module.exports
= {
SALESFORCE: {
LOGIN_URL: 'https://login.salesforce.com/',
USERNAME: 'your_username',
PASSWORD: 'your_password',
ACCOUNT_NAME: 'Test Account'
}
};
playwright.config.js
const { defineConfig } = require('@playwright/test');
module.exports
= defineConfig({
timeout: 30000,
retries: 1,
reporter: 'list',
use: {
headless: false,
viewport: { width: 1280, height: 720 },
actionTimeout: 10000,
ignoreHTTPSErrors: true,
screenshot: 'only-on-failure',
video: 'retain-on-failure'
},
projects: [
{
name: 'chromium',
use: { browserName: 'chromium' }
}
]
});
🏗 Page Objects (POM)
pages/LoginPage.js
class
LoginPage {
constructor(page) {
this.page = page;
this.usernameInput = '#username';
this.passwordInput = '#password';
this.loginButton = '#Login';
}
async navigateToLogin() {
await
this.page.goto(process.env.SALESFORCE_LOGIN_URL);
}
async login(username, password) {
await this.page.fill(this.usernameInput,
username);
await this.page.fill(this.passwordInput,
password);
await this.page.click(this.loginButton);
await
this.page.waitForLoadState('networkidle');
}
}
module.exports
= LoginPage;
pages/HomePage.js
class HomePage
{
constructor(page) {
this.page = page;
this.appLauncher =
'button[aria-label="App Launcher"]';
this.viewAllLink = 'button[aria-label="View All
Applications"]';
this.salesLink =
'a[data-label="Sales"]';
}
async navigateToSales() {
await this.page.click(this.appLauncher);
await this.page.click(this.viewAllLink);
await this.page.click(this.salesLink);
await
this.page.waitForLoadState('networkidle');
}
}
module.exports
= HomePage;
pages/AccountsPage.js
class
AccountsPage {
constructor(page) {
this.page = page;
this.newButton =
'div[title="New"]';
this.accountNameInput =
'label:has-text("Account Name") + div input';
this.saveButton =
'button[title="Save"]';
}
async createNewAccount(accountName) {
await this.page.click(this.newButton);
await this.page.fill(this.accountNameInput,
accountName);
await this.page.click(this.saveButton);
await
this.page.waitForLoadState('networkidle');
}
async getToastMessage() {
return
this.page.innerText('span[data-aura-class="forceActionsText"]');
}
}
module.exports
= AccountsPage;
🧪 Test Implementation
tests/salesforce.test.js
const { test,
expect } = require('@playwright/test');
const {
SALESFORCE } = require('../config/constants');
const
LoginPage = require('../pages/LoginPage');
const HomePage
= require('../pages/HomePage');
const
AccountsPage = require('../pages/AccountsPage');
test.describe('Salesforce
Account Creation', () => {
let page;
let loginPage;
let homePage;
let accountsPage;
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
loginPage = new LoginPage(page);
homePage = new HomePage(page);
accountsPage = new AccountsPage(page);
});
test.beforeEach(async () => {
await
loginPage.navigateToLogin();
await loginPage.login(SALESFORCE.USERNAME,
SALESFORCE.PASSWORD);
});
test('Create new account in Salesforce',
async () => {
await homePage.navigateToSales();
await
accountsPage.createNewAccount(SALESFORCE.ACCOUNT_NAME);
const toastMessage = await
accountsPage.getToastMessage();
expect(toastMessage).toContain('was
created');
});
test.afterAll(async () => {
await page.close();
});
});
🛠 Setup Instructions
Install dependencies
npm init -y
npm install
@playwright/test
Set up environment variables in package.json
{
"scripts": {
"test":
"SALESFORCE_LOGIN_URL=https://login.salesforce.com npx playwright
test"
}
}
Run tests
npm test
🔥 Important Notes:
Ø
Replace placeholder credentials in constants.js with actual Salesforce credentials.
Ø
Adjust selectors according to your Salesforce
instance's DOM structure.
Ø
Handle 2FA/MFA if required by your organization.
Ø
Add proper waits and error handling based on
network speed and Salesforce responsiveness.
🚀 Consider Adding:
Ø
Screenshot on failure
Ø
Retry mechanisms
Ø
Test data cleanup
Ø
Environment-specific
configurations
This implementation provides a scalable POM structure that you can
extend for other Salesforce objects and functionalities.
0 Comments