I want to get color of a cell on a wintable and I think I need to get style of the color for that but and I do not know if its possible to get style information of a winCell in codedui. I can access cell and value inside it but I can not get any style information such as color.
I tried a code I found on stack however it gives error for Cells
for (int i = 0; i < uIG1Table.Rows.Count; i++)
{
for (int j = 0; j < uIG1Table.Rows[i].Cells.Count; j++)
{
uIG1Table.Rows[i].Cells[j].Style.BackColor = /*color you want*/
}
}
I can access to a cell as below, however there is no color or style property associated to individual cells
WinTable uIG1Table = this.UIProMANAGEWindow.UIStopListWindow.UIG1Window.UIG1Table;
WinRow dataGridrow = uIG1Table.GetRow(0);
foreach (WinCell cell in dataGridrow.Cells)
{
....
}
You could provide these values in a property in the program under test. Use a property you have access to. In this sample program, I serialized the cell background color to json and exposed it through the AccessibleDescription
property from my DataGridView
.
Sample program that I will write a Coded UI Test for:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsTestApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Typically, the datasource is a db, xml, json, etc...
var dataSource = new[]
{
new{ Column1 = "value1", Column2 = "value2", Column3 = "value3"},
new{ Column1 = "value1", Column2 = "value2", Column3 = "value3"},
new{ Column1 = "value1", Column2 = "value2", Column3 = "value3"},
new{ Column1 = "value1", Column2 = "value2", Column3 = "value3"}
}.ToList();
dataGridView1.DataSource = dataSource;
}
private void Form1_Load(object sender, EventArgs e)
{
//Setting the background color for a cell in the dataGridView.
dataGridView1.Rows[1].Cells[1].Style.BackColor = Color.Red;
//This dictionary object will be serialized and used to tranfer the cell color information.
Dictionary<int, List<Color>> gridColors = new Dictionary<int, List<Color>>();
int rowIndex = 0;
//this code iterates the table and stores the information in the dictionary.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
gridColors.Add(rowIndex, new List<Color>());
foreach (DataGridViewCell cell in row.Cells)
{
gridColors[rowIndex].Add(cell.Style.BackColor);
}
rowIndex++;
}
string json = JsonConvert.SerializeObject(gridColors);
dataGridView1.AccessibleDescription = json;
}
}
}
You should only allow the serialization and exposure of this data in the debug version and not in the release version of your software. No need to expose this information on a live software product.
The code below demonstrates how to deserialize this information and use it in a Coded UI Tests:
using CodedUITestProject1.UIMap1Classes;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Drawing;
namespace CodedUITestProject1
{
/// <summary>
/// Summary description for CodedUITest1
/// </summary>
[CodedUITest]
public class CodedUITest1
{
public CodedUITest1()
{
}
[TestMethod]
public void CodedUITestMethod1()
{
UIMap1 uiMap = new UIMap1();
//The UIDataGridViewTable has been added to the UIMap using the inspection tool from VS.
string jsonToConvert = uiMap.UIForm1Window.UIDataGridView1Window.UIDataGridViewTable.AccessibleDescription;
Dictionary<int, List<Color>> gridRowColors = JsonConvert.DeserializeObject<Dictionary<int, List<Color>>>(jsonToConvert);
//iterate the dictionary to retrieve the color for each cell.
int rowIndex = 0;
for (int i = 0; i < gridRowColors.Count - 1; i++)
{
List<Color> rowCellColors = gridRowColors[rowIndex];
foreach (Color cellColor in rowCellColors)
{
//Check what color your cell has here.
}
}
}
}
}
A screen shot of the color data being exposed: