1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# BitBake Toaster UI tests implementation 4# 5# Copyright (C) 2023 Savoir-faire Linux 6# 7# SPDX-License-Identifier: GPL-2.0-only 8 9 10from time import sleep 11from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException, TimeoutException, WebDriverException 12from selenium.webdriver.common.by import By 13 14from orm.models import Build 15 16 17def wait_until_build(test_instance, state): 18 timeout = 60 19 start_time = 0 20 build_state = '' 21 while True: 22 try: 23 if start_time > timeout: 24 raise TimeoutException( 25 f'Build did not reach {state} state within {timeout} seconds' 26 ) 27 last_build_state = test_instance.driver.find_element( 28 By.XPATH, 29 '//*[@id="latest-builds"]/div[1]//div[@class="build-state"]', 30 ) 31 build_state = last_build_state.get_attribute( 32 'data-build-state') 33 state_text = state.lower().split() 34 if any(x in str(build_state).lower() for x in state_text): 35 return str(build_state).lower() 36 if 'failed' in str(build_state).lower(): 37 break 38 except NoSuchElementException: 39 pass 40 except TimeoutException: 41 break 42 start_time += 1 43 sleep(1) # take a breath and try again 44 45def wait_until_build_cancelled(test_instance): 46 """ Cancel build take a while sometime, the method is to wait driver action 47 until build being cancelled 48 """ 49 timeout = 30 50 start_time = 0 51 while True: 52 try: 53 if start_time > timeout: 54 raise TimeoutException( 55 f'Build did not reach cancelled state within {timeout} seconds' 56 ) 57 last_build_state = test_instance.driver.find_element( 58 By.XPATH, 59 '//*[@id="latest-builds"]/div[1]//div[@class="build-state"]', 60 ) 61 build_state = last_build_state.get_attribute( 62 'data-build-state') 63 if 'failed' in str(build_state).lower(): 64 break 65 if 'cancelling' in str(build_state).lower(): 66 pass 67 if 'cancelled' in str(build_state).lower(): 68 break 69 except TimeoutException: 70 break 71 except NoSuchElementException: 72 pass 73 except StaleElementReferenceException: 74 pass 75 except WebDriverException: 76 pass 77 start_time += 1 78 sleep(1) # take a breath and try again 79 80def get_projectId_from_url(url): 81 # url = 'http://domainename.com/toastergui/project/1656/whatever 82 # or url = 'http://domainename.com/toastergui/project/1/ 83 # or url = 'http://domainename.com/toastergui/project/186 84 assert '/toastergui/project/' in url, "URL is not valid" 85 url_to_list = url.split('/toastergui/project/') 86 return int(url_to_list[1].split('/')[0]) # project_id 87