-
-
Save sjurgis/287d3ede6aeb0ec9f8123cb7f5adf84b to your computer and use it in GitHub Desktop.
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 yes, you need sfdx-node which exposes sfdx CLI api in JS - https://www.npmjs.com/package/sfdx-node
Alternatively you can try with core sfdx now that it was decoupled - forcedotcom/cli#531 (comment)
Alternatively you can try with core sfdx now that it was decoupled - forcedotcom/cli#531 (comment)
Thank you :)
@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 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 :)
Hi sjurgis,
Is there any package I need to install in Playwright for performing Salesforce API login?