keepcalculator

06 Numbers and Powers

Salesforce Automation: Lightning DOM Tests & MFA

Card No. 06·49679 · Updated · Standard formula, shown below

Salesforce automation involving Lightning components and multi-factor authentication (MFA) requires understanding how the Lightning DOM (Document Object Model) differs from classic Salesforce, and how MFA affects automated login flows. This article explains what you need to know about testing Lightning components and handling MFA in your automation scripts, including the key challenges and practical solutions.

Background: Lightning DOM vs. Classic

Salesforce Classic uses a standard HTML DOM where elements have predictable IDs and names. Lightning Experience (LEX) uses a shadow DOM for many components, which means that elements are encapsulated and not directly accessible via standard CSS selectors. This encapsulation is a core part of Lightning’s design, providing style and behavior isolation, but it breaks traditional automation locators.

When you automate with tools like Selenium, you typically use findElement(By.id()) or By.cssSelector(). In Lightning, many elements are inside a shadow root, so those locators fail. You need to use JavaScript execution to pierce the shadow DOM, or rely on the Lightning-specific testing utilities.

How Lightning DOM Tests Work

Testing Lightning components requires a strategy that accounts for the shadow DOM. Here are the primary approaches:

  • Use Lightning Testing Service (LTS): This is a Salesforce-provided framework that integrates with Jasmine or Mocha for unit testing. It runs tests in a real browser context, allowing you to interact with components as a user would.
  • Use Selenium with Shadow DOM piercing: For end-to-end tests, you can execute JavaScript to traverse shadow roots. For example, document.querySelector('lightning-input').shadowRoot.querySelector('input').
  • Use the data-* attributes: Salesforce recommends adding data-* attributes to your Lightning components for testability. These attributes remain accessible even inside the shadow DOM, and they are the most stable locators.
  • Use the force:lightning utilities: Some tools like Provar or Selenium’s Lightning extensions provide built-in support for Lightning components, handling the shadow DOM automatically.

When writing tests, prefer data-* attributes over CSS classes or element order, because classes are often obfuscated in production builds and order can change.

MFA and Automation: The Challenge

Salesforce now requires MFA for all users, including system administrators and API integrations. For automation, this means that a simple username/password login is no longer sufficient. The MFA challenge typically appears as a push notification, a TOTP code, or a security question. Automating past this requires a strategy that balances security with test reliability.

How to Handle MFA in Automation

There are several ways to manage MFA in your test automation:

  1. Use a test user with MFA disabled: Salesforce allows you to exempt certain profiles or permission sets from MFA, but this is not recommended for production. For sandbox environments, you can configure MFA to not apply to a specific test user, but this may not reflect production behavior.
  2. Store the TOTP secret: If your MFA uses a time-based one-time password (TOTP), you can store the secret key in your test configuration and generate codes programmatically. Libraries like pyotp (Python) or otplib (Node.js) can generate the current code at runtime.
  3. Use a mock or stub MFA provider: In sandbox, you can configure a custom MFA provider that returns a fixed code. This is often done for integration testing.
  4. Use Salesforce’s API for login: Instead of UI automation, use the Salesforce REST or SOAP API to authenticate. You can pass the MFA code as part of the login request if you are using a connected app with the mfa flow. This is the most robust method for backend automation.
  5. Use a dedicated automation user: Create a user specifically for automation, and set up MFA with a TOTP secret that you control. Then, in your test setup, generate the code just before login.

The best approach depends on your environment and the type of test. For UI tests, the TOTP secret method is common. For API tests, direct API login with MFA is cleaner.

Common Problems and Solutions

When automating Lightning with MFA, you will encounter specific issues:

  • Element not found: This is usually due to shadow DOM. Solution: Use data-* attributes or JavaScript to pierce the shadow root.
  • Stale element references: Lightning re-renders components frequently. Solution: Use explicit waits that poll for the element to be present and clickable, and re-query the element after any navigation or state change.
  • MFA timeout: TOTP codes expire every 30 seconds. Solution: Generate the code immediately before submitting the login form, and ensure your test doesn’t wait too long.
  • Iframe handling: Some Lightning components use iframes. Solution: Switch your WebDriver context to the iframe before interacting with its elements.
  • Performance issues: Lightning apps are heavy. Solution: Use window.performance to measure load times and set generous timeouts.

Best Practices for Reliable Automation

To make your tests stable and maintainable:

  • Always use explicit waits (e.g., WebDriverWait) instead of fixed sleeps.
  • Use the @TestVisible annotation on Apex methods if you need to expose them for testing.
  • For UI tests, consider using the lightning:datatable or lightning:input components’ data-* attributes.
  • Run tests in a sandbox that mirrors production configuration, including MFA settings.
  • Store secrets (like TOTP secrets) in environment variables or a secure vault, not in the test code.
  • For CI/CD, use a dedicated test user with a stable TOTP secret.

Comparing Automation Tools

Different tools handle Lightning and MFA differently:

  • Selenium: Flexible but requires manual shadow DOM handling. Works well with TOTP generation.
  • Provar: Has built-in Lightning support and MFA handling for Salesforce. It can manage TOTP and even push notifications in some configurations.
  • Copado Robotic Testing: Designed for Salesforce, it handles Lightning and MFA natively.
  • Playwright: Modern browser automation that can pierce shadow DOM more easily with its querySelector on shadow roots, but still requires custom logic.

Choose a tool that aligns with your team’s skills and your test complexity. For simple tests, Selenium with a few helpers is enough. For enterprise-scale, consider a Salesforce-native tool.

Security and Compliance Considerations

When automating MFA, you must be careful not to weaken security. Never disable MFA for all users just to make tests easier. Instead, use a dedicated test user and keep the MFA secret secure. Also, be aware that Salesforce may log MFA attempts, and your automation should not trigger lockouts. Use a separate IP range or a test profile to avoid affecting real users.

Frequently Asked Questions

Q: Can I use Selenium to automate Lightning components?
Yes, but you need to handle the shadow DOM. Use JavaScript execution to find elements inside shadow roots, or rely on data-* attributes that are accessible directly.

Q: How do I get a TOTP code for MFA in my test?
Store the TOTP secret (usually a base32 string) in your test config. Use a library like pyotp or otplib to generate the current code at runtime. For example, in Python: import pyotp; totp = pyotp.TOTP('JBSWY3DPEHPK3PXP'); print(totp.now()).

Q: What if my MFA uses a push notification instead of TOTP?
Push notifications are harder to automate. Consider using a test user with TOTP instead, or use a mock MFA provider in sandbox. Some tools like Provar can handle push notifications by intercepting the push, but this is complex.

Q: Is it safe to disable MFA for a test user?
In a sandbox, yes, if you keep it isolated. In production, never disable MFA for any user. Always use a dedicated test user with MFA enabled and manage the secret securely.

Q: Why do my tests fail after a Lightning component update?
Lightning updates can change the DOM structure, especially if you used CSS classes or element order. Use data-* attributes and avoid relying on implementation details. Also, update your tests when you update your components.

Conclusion

Automating Salesforce Lightning with MFA is challenging but manageable. The key is to understand the shadow DOM and use stable locators, and to handle MFA with a TOTP secret or a mock provider. By following the practices above, you can build robust automation that keeps up with Salesforce’s evolving platform.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →