Hi folks,
Today, we are back with another topic regarding Salesforce testing with Katalon Studio! Within this topic, we will show you a few tips & tricks on how to ensure that your Salesforce testing environment remain clean for the best testing efficiency. Read more below
1. Why do we need a clean environment for Salesforce?
Depending on the type of Salesforce environment being used, there may be tabs, saved progress from the last login, or different account types. All of these things can cause issues when doing automation testing, as when the test starts it may encounter these dynamic items that can interfere with the execution of the test.
For example, if a tab was left open on last login, when our test starts it will open to this most recent tab, if the test does not account for this, the test will fail.
2. What do we need to account for to have a clean environment?
2.1. Tabs
Depending on the environment that Salesforce is using (such as “Console”), there is a field reserved for opening multiple tabs. Since Salesforce handles these tabs within a single HTML document, looking for a specific element may run into errors by locating it on multiple tabs since they are all available on the same HTML page.
2.2. Tab/Page history
Whenever a user logs out of Salesforce, the current structure of the working environment is saved so that it can be reopened upon the next log in. In this case, any tabs that were left open as well as whatever section of Salesforce (some generic examples would be Home, Cases, Invoices) the user was last on will appear when logging back in.
Therefore, if the test case does not take into account that Salesforce is in the correct section or if there are unexpected (or multiple) tabs open, the test could fail due to the unexpected environment.
2.3. Account
Accounts in Salesforce have different permissions and access to various parts of the environment. This means that tests written for one user may not be applicable for another, as the page may have a different DOM, layout, or flow between different user types.
3. How do we ensure a clean environment?
3.1. Test Listener
-
Test Listeners can be used to handle cleaning the environment, once implemented they can run for each Test Suite and each Test Case that is run. They can be used in four ways, before a Test Suite or Test Case, and after a Test Suite or Test Case.
-
As an example specific to Salesforce, we can implement a Test Listener to run before every Test Case that ensures all of the current tabs that are open are closed.
Learn more about Test Listeners by visiting our Documentation at: Test Fixtures and Test Listeners (Test Hooks) in Katalon Studio | Katalon Docs
3.2. SetUp / TearDown
-
SetUp and TearDown can also be used to handle cleaning the environment, but unlike Test Listeners they are executed on a single Test Suite or Test Case, and do not affect any other Test Suites or Test Cases.
-
As an example specific to Salesforce, if we have a Test Case that creates a unique item on Salesforce that needs to be removed before the next test, and this is the only Test Case that needs to ensure something is deleted, we can place the scripting that deletes the item on Salesforce in the TearDown method for the Test Case.
For more information on SetUp and TearDown methods, please refer to our Documentation at: Test Fixtures and Test Listeners (Test Hooks) in Katalon Studio | Katalon Docs
4. Salesforce Clean Environment Examples
4.1. Example #1: Close All Tabs (Test Listener)
-
Scenario: When we log in to Salesforce, it remembers the tabs that were previously left open. Through our automation, several tabs are opened but not closed after the tests are run. Later in our test suite run, we are noticing errors from objects not being found due to there being multiple tabs open.
-
Solution: Before and after every test case, we should ensure that we enter and leave the environment clean by closing all tabs. This way, we prevent any instances where an unexpected tab is open and we ensure the environment is as clean as possible for the test that follows.
-
Implementation:
class NewTestListener {
@BeforeTestCase
def sampleBeforeTestCase(TestCaseContext testCaseContext) {
'Call Login Test Case'
WebUI.callTestCase(findTestCase('loginTest'), [:], FailureHandling.STOP_ON_FAILURE)
'Once logged in, send keys SHIFT + W to open the Close All Tabs option'
WebUI.sendKeys(findTestObject(null), Keys.chord(Keys.SHIFT, 'w'))
'Click on the Close All Tabs button'
WebUI.click("Object Repository/Page_Salesforce/btn_CloseAllTabs")
// Object XPATH: //button[@class='slds-button slds-button_neutral slds-button slds-button_brand']
}
@AfterTestCase
def sampleAfterTestCase(TestCaseContext testCaseContext) {
'Since already logged in, send keys SHIFT + W to open the Close All Tabs option'
WebUI.sendKeys(findTestObject(null), Keys.chord(Keys.SHIFT, 'w'))
'Click on the Close All Tabs button'
WebUI.click("Object Repository/Page_Salesforce/btn_CloseAllTabs")
// Object XPATH: //button[@class='slds-button slds-button_neutral slds-button slds-button_brand']
}
}
4.2. Example #2: Picking up where we last Logged Out (SetUp / TearDown)
-
Scenario: When we log in to Salesforce, it remembers what section of the environment we last were looking at. This includes which App (for example Customer Support Console or Sales Team Console) and which section of the App (for example Home, Cases, Invoices, Calls, etc.). One specific test we have navigates to a different section of our Console, when the next test starts it resumes from this section and breaks due to the test expecting to be in a different section.
-
Solution: After the Test Case that navigates to another section of the Console, we need to navigate back to the expected section of the Console that our other tests run on. To do this, we can add a TearDown method to the Test Case, this TearDown method will contain code that navigates back to the expected section of the Console.
-
Implementation:
@com.kms.katalon.core.annotation.TearDown
def tearDown() {
'Check if we are already at the location we want to reset the environment to'
boolean isCorrectLocation = WebUI.verifyElementText('Object Repository/Page_Salesforce/menu-current_Home', 'Home', FailureHandling.CONTINUE_ON_FAILURE)
// Object XPATH: //span[@class='slds-truncate' and contains(text(), 'Home')]
'If at the location we want to go to, do nothing, else navigate to location'
if (isCorrectLocation == true) {
// Do nothing
}
else {
'Open Navigation Menu'
WebUI.click('Object Repository/Page_Salesforce/menu-button_Navigation')
// Object XPATH: //button[@title='Show Navigation Menu']
'Click on desired location'
WebUI.click('Object Repository/Page_Salesforce/menu-list_Home')
// Object XPATH: //span[@class='menuLabel slds-listbox__option-text slds-listbox__option-text_entity' and contains(text(), 'Home')]
}
}
If you find this article helpful, then don’t forget to leave us a big like or a big heart
!
To view other KShare topics similar to this one, simply navigate to the support tag, or go to our Docs section.