def try_find_element(web_driver: WebDriver, by: FindElementBy, unique_val, retry_count, ignore_if_not_found=False) \ -> WebElement: """ attempts to find element within defined retry count. raises NoSuchElementException if ignore_if_not_found=false """ element = None retried = 0 while True: try: if by == FindElementBy.CLASS: element = web_driver.find_element_by_class_name(unique_val) elif by == FindElementBy.NAME: element = web_driver.find_element_by_name(unique_val) elif by == FindElementBy.AUTOMATION_ID: element = web_driver.find_element_by_accessibility_id( unique_val) except NoSuchElementException: retried = retried + 1 if retried > retry_count: if ignore_if_not_found: return None raise NoSuchElementException else: sleep(1) continue break return element
def click_context_menu(dsk_session: WebDriver, element: WebElement, menu_name): """ does context menu click (Right Click) on supplied element and performs click on supplied menu item """ context = None while context is None: # retry until context menu opens # open context menu actions = ActionChains(dsk_session) actions.move_to_element(element) actions.click(element) actions.context_click(element).perform() context = dsk_session.find_element_by_name("Context") # click context menu menu = context.find_element_by_name(menu_name) actions = ActionChains(dsk_session) actions.move_to_element(menu) actions.click(menu).perform()
def test_application(): capabilities = { "deviceName": "iPhone SE", "udid": "b3346f9b0c4797e7a68fffb4532e1727bc23ad76", "platformName": "iOS", "automationName": "XCUITest", "app": "/Users/pavlo.tsyupka/Downloads/Nucleus Smart 1.431.2.zip", "xcodeSigningId": "iPhone Developer", "updatedWDABundleId": "com.afkTestTeam.WebDriverAgentLib", "shouldWaitForQuiescence": "False", "fastReset": True } driver = WebDriver("http://0.0.0.0:4723/wd/hub", desired_capabilities=capabilities) driver.implicitly_wait(5) for _ in range(5): driver.execute_script("mobile: swipe", {"direction": "left"}) time.sleep(0.125) demo_mode_button = driver.find_element_by_name('demoModeButton') demo_mode_button.click() volume_open_button = driver.find_element_by_xpath( '//XCUIElementTypeCell[@name="volume"]') volume_open_button.click() plus_button = driver.find_element_by_xpath( '//XCUIElementTypeOther[4]/XCUIElementTypeOther/XCUIElementTypeButton[1]' ) minus_button = driver.find_element_by_xpath( '//XCUIElementTypeOther[4]/XCUIElementTypeOther/XCUIElementTypeButton[2]' ) value_element = driver.find_element_by_xpath( '//XCUIElementTypeCell[@name="volume"]') old_value = value_element.get_attribute('value') plus_button.click() time.sleep(1) new_value = value_element.get_attribute('value') assert old_value != new_value minus_button.click() time.sleep(1) new_value = value_element.get_attribute('value') assert old_value == new_value menu_button = driver.find_element_by_xpath( '//XCUIElementTypeButton[@name="Settings menu"]') menu_button.click() time.sleep(1) exit_button = driver.find_element_by_xpath( '//XCUIElementTypeCell[@name="exitPracticeMode"]') exit_button.click() time.sleep(1) for _ in range(5): driver.execute_script("mobile: swipe", {"direction": "right"}) time.sleep(0.125) driver.quit()