- 1 year ago
Using Appium and Python for Test Automation with "contains"
To use the "contains" assertion in Appium with Python for test automation, you can utilize the assert
statement along with the text_contains
locator strategy. Here's an example:
- Import the necessary modules:
pythonfrom appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
- Set up the desired capabilities and initialize the Appium driver:
pythondesired_caps = {
'platformName': 'Android',
'deviceName': 'device',
'appPackage': 'com.example.app',
'appActivity': 'com.example.app.MainActivity',
'automationName': 'UiAutomator2'
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
- Locate the element and perform the "contains" assertion:
pythonexpected_text = "Hello World"
element = driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("{}")'.format(expected_text))
assert expected_text in element.text, "Text does not contain the expected value"
In the above example, we are finding an element using the ANDROID_UIAUTOMATOR
locator strategy and passing a UiSelector with the textContains
method to search for an element containing the expected text. The assert
statement then verifies that the expected text is indeed contained in the element's text attribute.
You can modify the expected_text
variable to match the text you are expecting to find and customize the assertion message to provide more specific feedback in case the assertion fails.
Make sure to adapt the desired capabilities, element locators, and assertion logic based on your specific application and test scenario.
Note: This example assumes that you have Appium and the necessary dependencies properly installed and configured.