From 63c4b4d3964fc0ff2ab6c9a4062f4c408b65f134 Mon Sep 17 00:00:00 2001 From: Joy Liao <107583686+ddl-joy-liao@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:57:07 -0400 Subject: [PATCH] Jliao/qe 13732/click table column given text (#374) Add a step and tests to allow users to click a specific column that relates to a row that contains specific text. --- CHANGELOG.md | 3 +++ features/browser/tables.feature | 14 +++++++++++++ pyproject.toml | 2 +- src/cucu/steps/table_steps.py | 36 ++++++++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 694372d4..5768fcf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project closely adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.168.0 +- Add - step and test for clicking a table column within a table row that has specified text + ## 0.167.0 - Fix - fuzzy JS code injection in web page redirection diff --git a/features/browser/tables.feature b/features/browser/tables.feature index fa9bf7c7..defebd9a 100644 --- a/features/browser/tables.feature +++ b/features/browser/tables.feature @@ -152,3 +152,17 @@ Feature: Tables Then I should see the text "Africa clicked" When I wait to click the cell corresponding to the "4th" row and "1st" column in the "6th" table Then I should see the text "Tokyo clicked" + + Scenario: User can click a column within a row that contains specific text within the table + Given I open a browser at the url "http://{HOST_ADDRESS}:{PORT}/tables.html" + And I should see a table that matches the following: + | City | Country | Continent | + | Paris | France | Europe | + | Cairo | Egypt | Africa | + | Tokyo | Japan | Asia | + When I wait to click the "2nd" column within a row that contains the text "Paris" in the "6th" table + Then I should see the text "France clicked" + When I wait to click the "1st" column within a row that contains the text "Africa" in the "6th" table + Then I should see the text "Cairo clicked" + When I wait to click the "3rd" column within a row that contains the text "Japan" in the "6th" table + Then I should see the text "Asia clicked" diff --git a/pyproject.toml b/pyproject.toml index 054b6dc4..7015d7ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "cucu" -version = "0.167.0" +version = "0.168.0" license = "MIT" description = "Easy BDD web testing" authors = ["Domino Data Lab "] diff --git a/src/cucu/steps/table_steps.py b/src/cucu/steps/table_steps.py index be758ff2..46957218 100644 --- a/src/cucu/steps/table_steps.py +++ b/src/cucu/steps/table_steps.py @@ -4,7 +4,15 @@ from selenium.webdriver.common.by import By -from cucu import config, format_gherkin_table, fuzzy, helpers, retry, step +from cucu import ( + config, + format_gherkin_table, + fuzzy, + helpers, + logger, + retry, + step, +) from cucu.browser.frames import run_in_all_frames @@ -342,3 +350,29 @@ def wait_click_table_cell(ctx, row, column, table): Add 1 to the row number if the table has a header row. """ retry(click_table_cell)(ctx, row, column, table) + + +@step( + 'I wait to click the "{column:nth}" column within a row that contains the text "{match_text}" in the "{table:nth}" table' +) +def wait_click_table_cell_matching_text(ctx, column, match_text, table): + def click_table_cell_matching_text(ctx, column, match_text, table): + table_element = find_table_element(ctx, table) + + try: + row = table_element.find_elements( + By.XPATH, f'//td[.="{match_text}"]/parent::tr' + ) + if len(row) > 1: + logger.warn( + f'Found {len(row)} rows with matching text "{match_text}", using the first row.' + ) + cell = row[0].find_elements(By.CSS_SELECTOR, "td")[column] + except IndexError: + raise RuntimeError( + f"Cannot find table:{table+1},column:{column+1},text:{match_text}. Please check your table data." + ) + + ctx.browser.click(cell) + + retry(click_table_cell_matching_text)(ctx, column, match_text, table)