Katalon Studio移动端自动化测试:复用元素值做后续校验的方法
Hey there! Let’s break down how to verify that the like count increases by 1 after tapping the like icon in your mobile app using Katalon Studio. I’ll cover three main approaches—recording & playback, manual test case building, and scripting—plus a bonus reusable method to make your tests more maintainable.
This is the quickest way to get started if you’re new to Katalon:
- Open Katalon Studio, create a new Mobile Test Case, and connect your device/emulator.
- Click the Record Mobile button to launch the recording session.
- First, locate the like count element (
android.widget.TextView5), right-click it, select Capture Object, then add a Get Text action. Save the result to a variable (e.g.,beforeLikeCount). - Next, tap the like icon (
android.widget.ImageView3) directly in the recording view. - Locate the count element again, add another Get Text action, and save the result to
afterLikeCount. - Finally, add a Verify Match action. Set the expected value to
String.valueOf(Integer.parseInt(beforeLikeCount) + 1)and compare it toafterLikeCount. - Stop recording, save your test case, and run it to validate the flow.
If you prefer building steps manually instead of recording:
- Create a new Mobile Test Case and open the Test Case Editor.
- First, add the
Mobile >> Get Textkeyword. Select your pre-captured count element, then assign the return value to a variablebeforeCount(define this variable in the Variables panel with typeString). - Add the
Mobile >> Tapkeyword, and select the like icon element. - Add another
Mobile >> Get Textkeyword for the count element, assigning the result toafterCount. - Add the
Verify Expressionkeyword, input this condition:
Set a custom failure message like "Like count did not increase as expected".Integer.parseInt(afterCount) == Integer.parseInt(beforeCount) + 1 - Save and run the test case.
For more flexibility (like adding waits or error handling), use Katalon’s Groovy scripting:
// Grab your elements from the Object Repository (or define them manually) def likeIcon = findTestObject('Object Repository/YourApp/android.widget.ImageView3') def countText = findTestObject('Object Repository/YourApp/android.widget.TextView5') // Get the initial like count and convert to integer String beforeCountStr = Mobile.getText(countText, FailureHandling.STOP_ON_FAILURE) int beforeCount = Integer.parseInt(beforeCountStr) // Tap the like icon (wait up to 10 seconds for the element) Mobile.tap(likeIcon, 10) // Wait for the count to update to avoid stale values Mobile.waitForElementVisible(countText, 5) // Get the updated count String afterCountStr = Mobile.getText(countText, FailureHandling.STOP_ON_FAILURE) int afterCount = Integer.parseInt(afterCountStr) // Verify the count increased by 1 assert afterCount == beforeCount + 1 : "Like count verification failed! Expected: ${beforeCount + 1}, Actual: ${afterCount}"
This approach lets you add edge case handling (like catching number format exceptions) or reuse code across multiple test cases.
If you need to run this verification across multiple test cases, wrap the logic in a custom keyword:
- Go to Keywords > New to create a new custom keyword class (e.g.,
com.yourcompany.app.LikeUtils). - Add this code:
package com.yourcompany.app import com.kms.katalon.core.testobject.TestObject import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile import com.kms.katalon.core.exception.StepFailedException class LikeUtils { static void verifyLikeCountIncreases(TestObject likeIcon, TestObject countElement) { // Get initial count String beforeStr = Mobile.getText(countElement, FailureHandling.STOP_ON_FAILURE) int beforeCount = Integer.parseInt(beforeStr) // Tap like icon Mobile.tap(likeIcon, 10) // Wait for the count to update to the expected value Mobile.waitForElementText(countElement, String.valueOf(beforeCount + 1), 5) // Final verification String afterStr = Mobile.getText(countElement, FailureHandling.STOP_ON_FAILURE) int afterCount = Integer.parseInt(afterStr) if (afterCount != beforeCount + 1) { throw new StepFailedException("Like count didn't increase correctly! Expected ${beforeCount +1}, got ${afterCount}") } } }
- In your test case, call the keyword like this:
CustomKeywords.'com.yourcompany.app.LikeUtils.verifyLikeCountIncreases'( findTestObject('Object Repository/YourApp/android.widget.ImageView3'), findTestObject('Object Repository/YourApp/android.widget.TextView5') )
Quick Tips
- Always use stable locators: Instead of relying on the default
android.widget.TextView5, use more robust locators like XPath (e.g.,//*[@resource-id='com.yourapp:id/like_count']) or accessibility IDs to avoid test failures if element IDs change. - Add waits: Never skip waiting for elements to update—use
waitForElementVisibleorwaitForElementTextto ensure the page has loaded before fetching values. - Handle edge cases: What if the count starts at 0? Or if the user already liked the post? Adjust your test logic accordingly (e.g., check if the icon changes state first).
内容的提问来源于stack exchange,提问作者Lasan




