def _predicate_literal(value): text = str(value) text = text.replace('\\', '\\\\') text = text.replace('"', '\\"') text = text.replace('\n', '\\n') return f'"{text}"' def _with_index(locator, index): if index is None: return locator return f'{locator}[{index}]' def _class_chain(element_type, predicate, index=None): locator = f'**/{element_type}[`{predicate}`]' return _with_index(locator, index) def ios_predicate_name_equals(name): return f'name == {_predicate_literal(name)}' def ios_other_by_name(name, index=None): return _class_chain( 'XCUIElementTypeOther', ios_predicate_name_equals(name), index, ) def ios_static_text_by_name(name, index=None): return _class_chain( 'XCUIElementTypeStaticText', ios_predicate_name_equals(name), index, ) def ios_name_contains(name, element_type='XCUIElementTypeOther', index=None): return _class_chain( element_type, f'name CONTAINS {_predicate_literal(name)}', index, ) def ios_label_equals( label, element_type='XCUIElementTypeStaticText', index=None): return _class_chain( element_type, f'label == {_predicate_literal(label)}', index, )