From b8d5b1fdcff43762a996dd4dcbd10fa31d4a92f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 27 Jan 2022 15:36:23 +0100 Subject: [PATCH] Extension methods for table handling and using certain controls in the table --- .../Extensions/HtmlTableExtension.cs | 133 ++++++++++++++++++ EATestProject/Pages/HomePage.cs | 9 ++ EATestProject/UnitTest1.cs | 1 + 3 files changed, 143 insertions(+) create mode 100644 EATestFramework/Extensions/HtmlTableExtension.cs diff --git a/EATestFramework/Extensions/HtmlTableExtension.cs b/EATestFramework/Extensions/HtmlTableExtension.cs new file mode 100644 index 0000000..14ebea7 --- /dev/null +++ b/EATestFramework/Extensions/HtmlTableExtension.cs @@ -0,0 +1,133 @@ +using OpenQA.Selenium; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EATestFramework.Extensions +{ + public static class HtmlTableExtension + { + //Read of Table + private static List ReadTable(IWebElement table) + { + var tableDataCollection = new List(); + var columns = table.FindElements(By.TagName("th")); + var rows = table.FindElements(By.TagName("tr")); + int rowIndex = 0; + foreach (var row in rows) + { + int colIndex = 0; + var colDatas = row.FindElements(By.TagName("td")); + if (colDatas.Count != 0) + foreach (var colValue in colDatas) + { + tableDataCollection.Add(new TableDataCollection + { + RowNumber = rowIndex, + ColumnName = columns[colIndex].Text != "" ? + columns[colIndex].Text : colIndex.ToString(), + ColumnValue = colValue.Text, + ColumnSpecialValue = GetControl(colValue) + }); + colIndex++; + } + rowIndex++; + } + return tableDataCollection; + } + + private static ColumnSpecialValue GetControl(IWebElement columnValue) + { + ColumnSpecialValue? columnSpecialValue = null; + if (columnValue.FindElements(By.TagName("a")).Count > 0) + { + columnSpecialValue = new ColumnSpecialValue + { + ElementCollection = columnValue.FindElements(By.TagName("a")), + ControlType = ControlType.hyperlink + }; + } + if (columnValue.FindElements(By.TagName("input")).Count > 0) + { + columnSpecialValue = new ColumnSpecialValue + { + ElementCollection = columnValue.FindElements(By.TagName("input")), + ControlType = ControlType.input + }; + } + return columnSpecialValue; + } + + public static void PerformActionOnCell(this IWebElement element, string targetColumnIndex, string refColumnName, string refColumnValue, string controlToOperate = null) + { + var table = ReadTable(element); + + foreach (int rowNumber in GetDynamicRowNumber(table, refColumnName, refColumnValue)) + { + var cell = (from e in table + where e.ColumnName == targetColumnIndex && e.RowNumber == rowNumber + select e.ColumnSpecialValue).SingleOrDefault(); + + if (controlToOperate != null && cell != null) + { + IWebElement elementToClick = null; + + if (cell.ControlType == ControlType.hyperlink) + { + elementToClick = (from c in cell.ElementCollection + where c.Text == controlToOperate.ToString() + select c).SingleOrDefault(); + } + if (cell.ControlType == ControlType.input) + { + elementToClick = (from c in cell.ElementCollection + where c.GetAttribute("value") == controlToOperate.ToString() + select c).SingleOrDefault(); + + } + elementToClick?.Click(); + } + else + { + cell.ElementCollection?.First().Click(); + } + } + + } + + private static IEnumerable GetDynamicRowNumber(List tableCollection, string columnName, string columnValue) + { + foreach (var table in tableCollection) + { + if (table.ColumnName == columnName && table.ColumnValue == columnValue) + yield return table.RowNumber; + } + } + + } + + public class TableDataCollection + { + public int RowNumber { get; set; } + public string? ColumnName { get; set; } + public string? ColumnValue { get; set; } + public ColumnSpecialValue? ColumnSpecialValue { get; set; } + } + + public class ColumnSpecialValue + { + public IEnumerable? ElementCollection { get; set; } + public ControlType? ControlType { get; set; } + } + + public enum ControlType + { + hyperlink, + input, + option, + select + } +} diff --git a/EATestProject/Pages/HomePage.cs b/EATestProject/Pages/HomePage.cs index d530477..ac1de17 100644 --- a/EATestProject/Pages/HomePage.cs +++ b/EATestProject/Pages/HomePage.cs @@ -1,4 +1,5 @@ using EATestFramework.Driver; +using EATestFramework.Extensions; using OpenQA.Selenium; namespace EATestProject.Pages @@ -6,6 +7,7 @@ namespace EATestProject.Pages public interface IHomePage { void CreateProduct(); + void PerformClickOnSpecialValue(string name, string operation); } public class HomePage : IHomePage @@ -18,11 +20,18 @@ namespace EATestProject.Pages IWebElement lnkProduct => _driver.FindElement(By.LinkText("Product")); IWebElement lnkCreate => _driver.FindElement(By.LinkText("Create")); + IWebElement tblList => _driver.FindElement(By.CssSelector(".table")); + public void CreateProduct() { lnkProduct.Click(); lnkCreate.Click(); } + + public void PerformClickOnSpecialValue(string name, string operation) + { + tblList.PerformActionOnCell("5", "Name", name, operation); + } } } diff --git a/EATestProject/UnitTest1.cs b/EATestProject/UnitTest1.cs index 09ee459..55bc5a8 100644 --- a/EATestProject/UnitTest1.cs +++ b/EATestProject/UnitTest1.cs @@ -42,6 +42,7 @@ namespace EATestProject _homePage.CreateProduct(); _createProductPage.EnterProductDetails(product); + _homePage.PerformClickOnSpecialValue("Monitor", "Details"); } [Theory, AutoData]