Skip to content

Instantly share code, notes, and snippets.

@sjurgis
Last active June 24, 2024 11:02
Show Gist options
  • Save sjurgis/287d3ede6aeb0ec9f8123cb7f5adf84b to your computer and use it in GitHub Desktop.
Save sjurgis/287d3ede6aeb0ec9f8123cb7f5adf84b to your computer and use it in GitHub Desktop.
Authorize Salesforce Connected App using Playwright
const common = require('./common');
const sfdx = require('sfdx-node/parallel');
class apiIntegrate extends common {
constructor(page, element) {
super(page, element);
}
async login(popup, user, attempt = 0) {
if (attempt > 100) {
throw new Error('Login Retry count exceeded');
}
await popup.fill('input[name="username"]', user.fields.username);
await popup.fill('input[type="password"]', user.password);
await popup.click('input[type="submit"][name="Login"]');
let err;
try {
err = await popup.waitForSelector(
'//div[contains(text(),"Please check your username and password")]',
{ timeout: 60000 }
);
} catch (e) {
// all good!
}
if (err) {
console.log('Retrying login, attempt: ', attempt + 1);
await this.login(popup, user, attempt + 1);
}
}
async integrate() {
await this.page.goto(`https://mycompany.com/salesforce-integration`);
const [popup] = await Promise.all([
this.page.waitForEvent('popup'),
this.page.click('//button[contains(text(),"Oauth to Sandbox")]') // our button that launches connected App integration
]);
await popup.waitForLoadState();
await popup.setViewportSize({ width: 1920, height: 1080 });
await this.page.waitForTimeout(1000);
if (!(await popup.isClosed())) {
if ((await popup.$$('input[type="submit"][name="Login"]')).length) {
const user = await sfdx.user.create({
definitionfile: 'config/api-user.json',
_rejectOnError: true
});
await this.login(popup, user);
}
if (
await popup.$('//h2[contains(text(),"Change Your Password")]')
) {
await popup.click('button[name="cancel"]', {
timeout: 15000
});
}
await this.page.waitForTimeout(1000);
if (!(await popup.isClosed())) {
await popup.click('input[type="submit"][title="Allow"]', {
timeout: 15000
});
}
}
await this.page.waitForTimeout(1000);
}
}
module.exports = apiIntegrate;
@UmeshChittlur
Copy link

Hi sjurgis,

Is there any package I need to install in Playwright for performing Salesforce API login?

@sjurgis
Copy link
Author

sjurgis commented Jul 3, 2023

@UmeshChittlur yes, you need sfdx-node which exposes sfdx CLI api in JS - https://www.npmjs.com/package/sfdx-node

@sjurgis
Copy link
Author

sjurgis commented Jul 3, 2023

Alternatively you can try with core sfdx now that it was decoupled - forcedotcom/cli#531 (comment)

@UmeshChittlur
Copy link

Alternatively you can try with core sfdx now that it was decoupled - forcedotcom/cli#531 (comment)

Thank you :)

@sjurgis
Copy link
Author

sjurgis commented Jul 4, 2023

@UmeshChittlur and by the way this might work for you too - https://salesforce.stackexchange.com/questions/80722/pre-authorize-a-connected-app

I've came across this recently, but hasn't tried

@UmeshChittlur
Copy link

UmeshChittlur commented Jul 5, 2023

@UmeshChittlur and by the way this might work for you too - https://salesforce.stackexchange.com/questions/80722/pre-authorize-a-connected-app

I've came across this recently, but hasn't tried

@sjurgis Let me give a try and update you.
thank you so much for sharing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment