Extension methods for table handling and using certain controls in the table
This commit is contained in:
133
EATestFramework/Extensions/HtmlTableExtension.cs
Normal file
133
EATestFramework/Extensions/HtmlTableExtension.cs
Normal file
@ -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<TableDataCollection> ReadTable(IWebElement table)
|
||||||
|
{
|
||||||
|
var tableDataCollection = new List<TableDataCollection>();
|
||||||
|
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<TableDataCollection> 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<IWebElement>? ElementCollection { get; set; }
|
||||||
|
public ControlType? ControlType { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ControlType
|
||||||
|
{
|
||||||
|
hyperlink,
|
||||||
|
input,
|
||||||
|
option,
|
||||||
|
select
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
using EATestFramework.Driver;
|
using EATestFramework.Driver;
|
||||||
|
using EATestFramework.Extensions;
|
||||||
using OpenQA.Selenium;
|
using OpenQA.Selenium;
|
||||||
|
|
||||||
namespace EATestProject.Pages
|
namespace EATestProject.Pages
|
||||||
@ -6,6 +7,7 @@ namespace EATestProject.Pages
|
|||||||
public interface IHomePage
|
public interface IHomePage
|
||||||
{
|
{
|
||||||
void CreateProduct();
|
void CreateProduct();
|
||||||
|
void PerformClickOnSpecialValue(string name, string operation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class HomePage : IHomePage
|
public class HomePage : IHomePage
|
||||||
@ -18,11 +20,18 @@ namespace EATestProject.Pages
|
|||||||
|
|
||||||
IWebElement lnkProduct => _driver.FindElement(By.LinkText("Product"));
|
IWebElement lnkProduct => _driver.FindElement(By.LinkText("Product"));
|
||||||
IWebElement lnkCreate => _driver.FindElement(By.LinkText("Create"));
|
IWebElement lnkCreate => _driver.FindElement(By.LinkText("Create"));
|
||||||
|
IWebElement tblList => _driver.FindElement(By.CssSelector(".table"));
|
||||||
|
|
||||||
|
|
||||||
public void CreateProduct()
|
public void CreateProduct()
|
||||||
{
|
{
|
||||||
lnkProduct.Click();
|
lnkProduct.Click();
|
||||||
lnkCreate.Click();
|
lnkCreate.Click();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void PerformClickOnSpecialValue(string name, string operation)
|
||||||
|
{
|
||||||
|
tblList.PerformActionOnCell("5", "Name", name, operation);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -42,6 +42,7 @@ namespace EATestProject
|
|||||||
|
|
||||||
_homePage.CreateProduct();
|
_homePage.CreateProduct();
|
||||||
_createProductPage.EnterProductDetails(product);
|
_createProductPage.EnterProductDetails(product);
|
||||||
|
_homePage.PerformClickOnSpecialValue("Monitor", "Details");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory, AutoData]
|
[Theory, AutoData]
|
||||||
|
|||||||
Reference in New Issue
Block a user