Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 2 | # |
| 3 | # BitBake Toaster functional tests implementation |
| 4 | # |
| 5 | # Copyright (C) 2017 Intel Corporation |
| 6 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 7 | # SPDX-License-Identifier: GPL-2.0-only |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 8 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 9 | |
| 10 | import os |
| 11 | import logging |
| 12 | import subprocess |
| 13 | import signal |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 14 | import re |
| 15 | |
| 16 | from tests.browser.selenium_helpers_base import SeleniumTestCaseBase |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 17 | from selenium.webdriver.common.by import By |
| 18 | from selenium.common.exceptions import NoSuchElementException |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 19 | |
| 20 | logger = logging.getLogger("toaster") |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 21 | toaster_processes = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | |
| 23 | class SeleniumFunctionalTestCase(SeleniumTestCaseBase): |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 24 | wait_toaster_time = 10 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | |
| 26 | @classmethod |
| 27 | def setUpClass(cls): |
| 28 | # So that the buildinfo helper uses the test database' |
| 29 | if os.environ.get('DJANGO_SETTINGS_MODULE', '') != \ |
| 30 | 'toastermain.settings_test': |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 31 | raise RuntimeError("Please initialise django with the tests settings: " |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | "DJANGO_SETTINGS_MODULE='toastermain.settings_test'") |
| 33 | |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 34 | # Wait for any known toaster processes to exit |
| 35 | global toaster_processes |
| 36 | for toaster_process in toaster_processes: |
| 37 | try: |
| 38 | os.waitpid(toaster_process, os.WNOHANG) |
| 39 | except ChildProcessError: |
| 40 | pass |
| 41 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 42 | # start toaster |
| 43 | cmd = "bash -c 'source toaster start'" |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 44 | start_process = subprocess.Popen( |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 45 | cmd, |
| 46 | cwd=os.environ.get("BUILDDIR"), |
| 47 | shell=True) |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 48 | toaster_processes = [start_process.pid] |
| 49 | if start_process.wait() != 0: |
| 50 | port_use = os.popen("lsof -i -P -n | grep '8000 (LISTEN)'").read().strip() |
| 51 | message = '' |
| 52 | if port_use: |
| 53 | process_id = port_use.split()[1] |
| 54 | process = os.popen(f"ps -o cmd= -p {process_id}").read().strip() |
| 55 | message = f"Port 8000 occupied by {process}" |
| 56 | raise RuntimeError(f"Can't initialize toaster. {message}") |
| 57 | |
| 58 | builddir = os.environ.get("BUILDDIR") |
| 59 | with open(os.path.join(builddir, '.toastermain.pid'), 'r') as f: |
| 60 | toaster_processes.append(int(f.read())) |
| 61 | with open(os.path.join(builddir, '.runbuilds.pid'), 'r') as f: |
| 62 | toaster_processes.append(int(f.read())) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 63 | |
| 64 | super(SeleniumFunctionalTestCase, cls).setUpClass() |
| 65 | cls.live_server_url = 'http://localhost:8000/' |
| 66 | |
| 67 | @classmethod |
| 68 | def tearDownClass(cls): |
| 69 | super(SeleniumFunctionalTestCase, cls).tearDownClass() |
| 70 | |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 71 | global toaster_processes |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 72 | |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 73 | cmd = "bash -c 'source toaster stop'" |
| 74 | stop_process = subprocess.Popen( |
| 75 | cmd, |
| 76 | cwd=os.environ.get("BUILDDIR"), |
| 77 | shell=True) |
| 78 | # Toaster stop has been known to hang in these tests so force kill if it stalls |
| 79 | try: |
| 80 | if stop_process.wait(cls.wait_toaster_time) != 0: |
| 81 | raise Exception('Toaster stop process failed') |
| 82 | except Exception as e: |
| 83 | if e is subprocess.TimeoutExpired: |
| 84 | print('Toaster stop process took too long. Force killing toaster...') |
| 85 | else: |
| 86 | print('Toaster stop process failed. Force killing toaster...') |
| 87 | stop_process.kill() |
| 88 | for toaster_process in toaster_processes: |
| 89 | os.kill(toaster_process, signal.SIGTERM) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 90 | |
| 91 | |
| 92 | def get_URL(self): |
| 93 | rc=self.get_page_source() |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame^] | 94 | project_url=re.search(r"(projectPageUrl\s:\s\")(.*)(\",)",rc) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 95 | return project_url.group(2) |
| 96 | |
| 97 | |
| 98 | def find_element_by_link_text_in_table(self, table_id, link_text): |
| 99 | """ |
| 100 | Assume there're multiple suitable "find_element_by_link_text". |
| 101 | In this circumstance we need to specify "table". |
| 102 | """ |
| 103 | try: |
| 104 | table_element = self.get_table_element(table_id) |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 105 | element = table_element.find_element(By.LINK_TEXT, link_text) |
| 106 | except NoSuchElementException: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 107 | print('no element found') |
| 108 | raise |
| 109 | return element |
| 110 | |
| 111 | def get_table_element(self, table_id, *coordinate): |
| 112 | if len(coordinate) == 0: |
| 113 | #return whole-table element |
| 114 | element_xpath = "//*[@id='" + table_id + "']" |
| 115 | try: |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 116 | element = self.driver.find_element(By.XPATH, element_xpath) |
| 117 | except NoSuchElementException: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 118 | raise |
| 119 | return element |
| 120 | row = coordinate[0] |
| 121 | |
| 122 | if len(coordinate) == 1: |
| 123 | #return whole-row element |
| 124 | element_xpath = "//*[@id='" + table_id + "']/tbody/tr[" + str(row) + "]" |
| 125 | try: |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 126 | element = self.driver.find_element(By.XPATH, element_xpath) |
| 127 | except NoSuchElementException: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 128 | return False |
| 129 | return element |
| 130 | #now we are looking for an element with specified X and Y |
| 131 | column = coordinate[1] |
| 132 | |
| 133 | element_xpath = "//*[@id='" + table_id + "']/tbody/tr[" + str(row) + "]/td[" + str(column) + "]" |
| 134 | try: |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 135 | element = self.driver.find_element(By.XPATH, element_xpath) |
| 136 | except NoSuchElementException: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 137 | return False |
| 138 | return element |