如何使用Codedui获取WinCell的单元格颜色信息? [英] How to get cell color information of a WinCell with codedui?

查看:89
本文介绍了如何使用Codedui获取WinCell的单元格颜色信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取wintable上某个单元格的颜色,我想为此获取颜色的样式,但是我不知道是否有可能在codedui中获取winCell的样式信息。我可以访问其中的单元格和值,但无法获取任何样式信息,例如颜色。

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.

我尝试了在堆栈上找到的代码,但是它给Cells提供了错误

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)
        {
          ....
        }


推荐答案

您可以在被测试程序的属性中提供这些值。使用您有权访问的属性。在此示例程序中,我将单元格背景色序列化为json,并通过 DataGridView 中的 AccessibleDescription 属性将其公开。

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.

我将为以下内容编写编码UI测试的示例程序:

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.

下面的代码演示了如何反序列化此信息并将其用于编码的UI测试:

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:

这篇关于如何使用Codedui获取WinCell的单元格颜色信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆