|
3 | 3 | import pytest |
4 | 4 | from config.constants import * |
5 | 5 | from pages.homePage import HomePage |
| 6 | +import io |
6 | 7 |
|
7 | 8 | logger = logging.getLogger(__name__) |
8 | 9 |
|
| 10 | +# ----------------- Part A: Functional Tests ----------------- |
| 11 | + |
9 | 12 | def validate_home_and_client(home): |
10 | 13 | assert homepage_title == home.page.locator(home.HOME_PAGE_TITLE).text_content() |
11 | 14 | home.select_a_client(client_name) |
12 | 15 | assert client_name == home.page.locator(home.SELECTED_CLIENT_NAME_LABEL).text_content() |
13 | 16 |
|
14 | | -def save_chat_confirmation_popup(home): |
15 | | - home.click_clear_chat_icon() |
16 | | - home.enter_a_question(golden_path_question1) |
17 | | - home.click_send_button() |
18 | | - home.validate_response_status() |
19 | | - home.click_on_save_chat_plus_icon() |
20 | | - assert home.page.locator(home.SAVE_CHAT_CONFIRMATION_POPUPTEXT).is_visible() |
21 | | - |
22 | 17 | def delete_chat_history_during_response(home): |
23 | | - home.enter_a_question(golden_path_question1) |
24 | | - home.click_send_button() |
25 | | - home.click_on_save_chat_plus_icon() |
26 | | - assert home.page.locator(home.SAVE_CHAT_CONFIRMATION_POPUPTEXT).is_visible() |
27 | | - home.click_on_show_chat_history_button() |
28 | | - home.click_on_saved_chat() |
29 | | - home.enter_a_question(golden_path_question1) |
30 | | - home.click_send_button_for_chat_history_response() |
31 | | - assert home.page.locator(home.SHOW_CHAT_HISTORY_DELETE_ICON).is_disabled() |
32 | | - home.click_hide_chat_history_button() |
33 | | - home.click_clear_chat_icon() |
34 | | - |
35 | | -def golden_path_full_demo(home): |
36 | | - _validate_golden_path_response(home, golden_path_question1) |
37 | | - _validate_golden_path_response(home, golden_path_question2) |
38 | | - _validate_golden_path_response(home, golden_path_question3) |
39 | | - _validate_golden_path_response(home, golden_path_question4) |
40 | | - _validate_golden_path_response(home, golden_path_question5) |
| 18 | + home.delete_chat_history() |
| 19 | + # home.close_chat_history() |
| 20 | + |
| 21 | + |
| 22 | +def validate_client_absence(home): |
41 | 23 | _validate_client_info_absence(home, golden_path_question7) |
42 | 24 |
|
43 | | -# Define test steps and actions |
44 | | -test_cases = [ |
45 | | - ("Validate homepage and select client", validate_home_and_client), |
46 | | - ("Save chat confirmation popup", save_chat_confirmation_popup), |
47 | | - ("Delete chat history during response", delete_chat_history_during_response), |
48 | | - ("Golden path full demo", golden_path_full_demo), |
| 25 | +functional_test_cases = [ |
| 26 | + ("Validate homepage is loaded and select client", validate_home_and_client), |
| 27 | + ("Validate delete chat history", delete_chat_history_during_response), |
| 28 | +] |
| 29 | + |
| 30 | +@pytest.mark.parametrize("desc, action", functional_test_cases, ids=[x[0] for x in functional_test_cases]) |
| 31 | +def test_functional_flows(login_logout, desc, action, request): |
| 32 | + page = login_logout |
| 33 | + home_page = HomePage(page) |
| 34 | + home_page.page = page |
| 35 | + |
| 36 | + log_capture = io.StringIO() |
| 37 | + handler = logging.StreamHandler(log_capture) |
| 38 | + logger.addHandler(handler) |
| 39 | + |
| 40 | + logger.info(f"Running step: {desc}") |
| 41 | + start = time.time() |
| 42 | + try: |
| 43 | + action(home_page) |
| 44 | + finally: |
| 45 | + duration = time.time() - start |
| 46 | + logger.info(f"Execution Time for '{desc}': {duration:.2f}s") |
| 47 | + logger.removeHandler(handler) |
| 48 | + request.node._report_sections.append(("call", "log", log_capture.getvalue())) |
| 49 | + |
| 50 | +# ----------------- Part B: GP Question Tests ----------------- |
| 51 | + |
| 52 | +# GP Questions List |
| 53 | +gp_questions = [ |
| 54 | + golden_path_question1, |
| 55 | + golden_path_question2, |
| 56 | + golden_path_question3, |
| 57 | + golden_path_question4, |
| 58 | + golden_path_question5 |
49 | 59 | ] |
50 | 60 |
|
51 | | -# Create readable test IDs |
52 | | -test_ids = [f"{i+1:02d}. {desc}" for i, (desc, _) in enumerate(test_cases)] |
| 61 | +# Custom readable test IDs |
| 62 | +gp_test_ids = [f"Validate response for prompt: {q[:60]}... " for i, q in enumerate(gp_questions)] |
53 | 63 |
|
54 | 64 | def _validate_golden_path_response(home, question): |
55 | 65 | home.enter_a_question(question) |
56 | 66 | home.click_send_button() |
57 | 67 | home.validate_response_status() |
58 | 68 | response_text = home.page.locator(home.ANSWER_TEXT) |
59 | | - assert response_text.nth(response_text.count() - 1).text_content() != invalid_response, \ |
60 | | - f"Incorrect response for question: {question}" |
| 69 | + last_response = response_text.nth(response_text.count() - 1).text_content() |
| 70 | + assert last_response != invalid_response, f"Incorrect response for: {question}" |
| 71 | + assert last_response != "Chart cannot be generated.", f"Chart error for: {question}" |
| 72 | + |
| 73 | + if home.has_reference_link(): |
| 74 | + logger.info("Citation link found. Opening citation.") |
| 75 | + home.click_reference_link_in_response() |
| 76 | + logger.info("Closing citation.") |
| 77 | + home.close_citation() |
| 78 | + |
| 79 | + home.click_on_show_chat_history_button() |
| 80 | + home.close_chat_history() |
| 81 | + |
61 | 82 |
|
62 | 83 | def _validate_client_info_absence(home, question): |
63 | 84 | home.enter_a_question(question) |
64 | 85 | home.click_send_button() |
65 | 86 | home.validate_response_status() |
66 | | - response_text = home.page.locator(home.ANSWER_TEXT).nth(home.page.locator(home.ANSWER_TEXT).count() - 1).text_content().lower() |
| 87 | + response_text = home.page.locator(home.ANSWER_TEXT).nth( |
| 88 | + home.page.locator(home.ANSWER_TEXT).count() - 1 |
| 89 | + ).text_content().lower() |
67 | 90 | assert "arun sharma" not in response_text, "Other client information appeared in response." |
68 | | - assert client_name.lower() not in response_text, f"Client name '{client_name}' should not be in response for question: {question}" |
| 91 | + assert client_name.lower() not in response_text, f"Client name '{client_name}' appeared in response." |
69 | 92 |
|
70 | | -@pytest.mark.parametrize("desc, action", test_cases, ids=test_ids) |
71 | | -def test_home_page_cases(login_logout, desc, action, request): |
72 | | - """ |
73 | | - Parametrized test for home page scenarios including chat flows and validations. |
74 | | - """ |
| 93 | +@pytest.mark.parametrize("question", gp_questions, ids=gp_test_ids) |
| 94 | +def test_gp_questions_individual(login_logout, question, request): |
75 | 95 | page = login_logout |
76 | | - home_page = HomePage(page) |
77 | | - home_page.page = page # Required for locator access in helper functions |
78 | | - logger.info(f"Running step: {desc}") |
| 96 | + home = HomePage(page) |
| 97 | + home.page = page |
79 | 98 |
|
80 | | - start = time.time() |
81 | | - action(home_page) |
82 | | - end = time.time() |
| 99 | + log_capture = io.StringIO() |
| 100 | + handler = logging.StreamHandler(log_capture) |
| 101 | + logger.addHandler(handler) |
83 | 102 |
|
84 | | - duration = end - start |
85 | | - logger.info(f"Execution Time for '{desc}': {duration:.2f}s") |
86 | | - request.node._report_sections.append(("call", "log", f"Execution time: {duration:.2f}s")) |
| 103 | + logger.info(f"Running Golden Path test for: {question}") |
| 104 | + start = time.time() |
| 105 | + try: |
| 106 | + _validate_golden_path_response(home, question) |
| 107 | + finally: |
| 108 | + duration = time.time() - start |
| 109 | + logger.info(f"Execution Time for GP Question: {duration:.2f}s") |
| 110 | + logger.removeHandler(handler) |
| 111 | + request.node._report_sections.append(("call", "log", log_capture.getvalue())) |
0 commit comments