How to get cell color information of a WinCell with codedui? - coded-ui-tests

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:

Related

Getting object name and randomly placing it to Text UI - Unity

I am a beginner in Unity and I am currently making a simple game. I have a problem managing the flow of my minigame. The minigame is simply finding an object, when I found and tap on the item name below will be shaded or marked or there can be animation just to indicate that the item is found.
What I want to do is to get the name of the objects that need to be found and set them randomly in the three (3) item names below. like every time this minigame opens the names of the items are randomly placed in the 3 texts. And when the item is found the name below will be marked or shaded or anything that will indicate it is found, but for now I will just set it inactive for easier indication. How can I properly do this whole process?
The objects inside the scene are button for them to have onCLick() events
Correction: the term choices are wrong because they just display the name of the items that you need to find, just in case you get confused with the term choice in my minigame. I will fix it.
Here is the visuals for the minigame:
The script I currently have for when the objects was clicked:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using TMPro;
public class ClickObject : MonoBehaviour
{
[SerializeField] Button pillowBtn, pcBtn, lampBtn;
// [SerializeField] TextMeshProUGUI choice1, choice2, choice3;
// FROM THE SOLUTION
[SerializeField] List<GameObject> gameObjectWanted;
[SerializeField] List<TextMeshProUGUI> textBoxes;
// METHOD NAMES IS TEMPORARY
public void pillowClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
public void desktopClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
public void lampClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
}
You asked for a lot and I hope I understood your intention.
first, if you want to randomly choose an amount of game objects from your game, I think the best way to do it is by adding the refernece of all the game objects you want to choose from radomly inside a list of game objects and then randomly take a game object of the list and make it a child of another game object I call "fatherGoTranform" on my code like that:
[SerializeField] List<GameObject> gameObjectWanted;
[SerializeField] float numOfGO = 4;
[SerializeField] Transform fatherGoTranform;
void Start()
{
for(int i=0;i<numOfGO;i++)
{
int index = Random.Range(0, gameObjectWanted.Count-1);
GameObject currentGO = gameObjectWanted[index ];
currentGO.transform.parent = fatherGoTranform;
gameObjectWanted.RemoveAt(index);
}
}
and then to click on a game object and the do with what you want try this:
void Update()
{
//Check for mouse click
if (Input.GetMouseButtonDown(0))
{
RaycastHit raycastHit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out raycastHit, 100f))
{
if (raycastHit.transform != null)
{
//Our custom method.
CurrentClickedGameObject(raycastHit.transform.gameObject);
}
}
}
}
I have not checked the code so if there is an error tell me and I will fix it
This is the solution that worked for my problem. I randomly placed numbers to the list according to childCount and every index indicate the index of the text that I want to put them on which I get as a Transform child.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using TMPro;
public class ClickObject : MonoBehaviour
{
// [SerializeField] Button pillowBtn, pcBtn, lampBtn;
[SerializeField] GameObject choices, v1, finishPanel;
// RANDOM NUMBER HOLDER FOR CHECKING
private int randomNumber, foundCount;
// RANDOMIZED NUMBER LIST HOLDER
public List<int> RndmList = new List<int>();
private void Awake()
{
foundCount = 0;
RndmList = new List<int>(new int[v1.transform.childCount]);
for (int i = 0; i < v1.transform.childCount; i++)
{
randomNumber = UnityEngine.Random.Range(0, (v1.transform.childCount) + 1);
while (RndmList.Contains(randomNumber))
{
randomNumber = UnityEngine.Random.Range(0, (v1.transform.childCount) + 1);
}
RndmList[i] = randomNumber;
// Debug.Log(v1.transform.GetChild(randomNumber-1).name);
choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().text = v1.transform.GetChild(randomNumber - 1).name;
}
}
public void objectFound()
{
string objectName = EventSystem.current.currentSelectedGameObject.name;
Debug.Log("you found the " + objectName);
for (int i = 0; i < choices.transform.childCount; i++)
{
if (objectName == choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().text)
{
// Debug.Log(i);
// choices.transform.GetChild(i).gameObject.SetActive(false);
// choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().color = Color.gray;
if(RndmList.Contains(i+1))
{
// Debug.Log(i);
// Debug.Log(v1.transform.GetChild(RndmList[i]-1).name);
choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().color = Color.gray;
v1.transform.GetChild(RndmList[i]-1).GetComponent<Button>().enabled = false;
foundCount++;
}
}
}
if(foundCount == v1.transform.childCount)
{
finishPanel.SetActive(true);
}
}
}

Can't convert string to ui text in unity

I have looked around for solutions but nothing has worked so far, here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BirdController : MonoBehaviour
{
public float jumpVelocity;
public Text jumpCooldownDisplayText = "";
public float speed = 18;
private Rigidbody rb;
public float jumpCooldown = 0f;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate ()
{
jumpCooldown -= Time.deltaTime;
jumpCooldownDisplayText = Mathf.Round (jumpCooldown).ToString();
if (Input.GetButtonDown ("Jump") && jumpCooldown <= 0)
{
rb.velocity = Vector3.up * jumpVelocity;
jumpCooldown = 0.5f;
}
float hAxis = Input.GetAxis("Horizontal");
float vAxis = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(hAxis*speed, 0, vAxis*speed) * Time.deltaTime;
rb.MovePosition(transform.position + movement);
}
}
The error I'm getting is "Cannot implicitly convert type string' toUnityEngine.UI.Text'". I'm getting this error into different places of the code.
UI.Text is a UI text component, not the associated string. What you want is the text property of the UI Text:
jumpCooldownDisplayText.text = Mathf.Round (jumpCooldown).ToString();

App for printing Dymo labels disables scalling when print

I've made a little app for printing Dymo labels and it works as intended, but when I run it on my Surface tablet it disables scalling af I've pushed the print button. If reopened the scalling is correct till print button is pushed again.
Can anybody tell me why?
The print button only runs my LabelPrinter class which has the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dymo;
using System.Windows.Forms;
namespace LabelPrinter
{
class LabelPrinter
{
private DymoAddInClass dymoAddIn;
private DymoLabelsClass dymoLabels;
private string labelPath, classRoom;
private int labelCount;
public LabelPrinter(string labelPath, string classRoom, int labelCount)
{
dymoAddIn = new DymoAddInClass();
dymoLabels = new DymoLabelsClass();
this.labelPath = labelPath;
this.classRoom = classRoom;
this.labelCount = labelCount;
}
public void printLabels()
{
dymoAddIn.SelectPrinter(dymoAddIn.GetDymoPrinters().Split('|')[0]);
if (dymoAddIn.Open(labelPath))
{
int firstNo = StaticMethods.getNextCodeEntryWithString(classRoom), lastNo = firstNo + labelCount;
dymoAddIn.StartPrintJob();
for (int i = firstNo; i < lastNo; i++)
{
foreach (string objName in dymoLabels.GetObjectNames(true).Split('|'))
{
dymoLabels.SetField(objName, classRoom + "_" + i);
}
dymoAddIn.Print(1, false);
}
dymoAddIn.EndPrintJob();
}
}
}
}

How can I grayscale a emf image

Using C# and Visual Studio 2010, how can I make a grayscaled emf from a colored one? Should I enumerate records and change color settings somehow?
As a result I want to have a new emf image.
Here is an open source emf read/write utility. I don't think it can be done with GDI+ and preserve the vector content. So this hopefully will help you, but I haven't remotely tested all possible emf cases.
https://wmf.codeplex.com/
this works for both wmf and emf formats. We can combine the Oxage.Wmf strategy with the strategy from the other post which essentially re-colors a pen or brush when it is created so that anything drawn with that pen or brush should be gray.
using Oxage.Wmf;
using Oxage.Wmf.Records;
using Oxage.Wmf.Objects;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MetafileTest
{
public class GrayMap
{
public void GrayFile(string sourceFile, string destFile){
WmfDocument wmf = new WmfDocument();
wmf.Load(sourceFile);
foreach (var record in wmf.Records)
{
if (record is WmfCreateBrushIndirectRecord)
{
var brush = record as WmfCreateBrushIndirectRecord;
brush.Color = Gray(brush.Color);
}
else if (record is WmfCreatePenIndirectRecord)
{
var pen = record as WmfCreatePenIndirectRecord;
pen.Color = Gray(pen.Color);
}
else if (record is WmfCreatePalette) {
var pal = record as WmfCreatePalette;
foreach (PaletteEntry entry in pal.Palette.PaletteEntries) {
Color test = Color.FromArgb(entry.Red, entry.Green, entry.Blue);
Color grayTest = Gray(test);
entry.Red = grayTest.R;
entry.Green = grayTest.G;
entry.Blue = grayTest.B;
}
}
}
wmf.Save(destFile);
}
public Color Gray(Color original) {
int r = (int)(original.R*0.2989);
int g = (int)(original.G*0.5870);
int b = (int)(original.B*0.1140);
Color result = Color.FromArgb(r, g, b);
return result;
}
}
}
This was tested with the following super simple case of filling a blue rectangle, so for at least the simple cases this will work. It may not be able to handle all cases, but in such a case you can probably extend the original source to suite your needs since it is open source.
private void button1_Click(object sender, EventArgs e)
{
var wmf = new WmfDocument();
wmf.Width = 1000;
wmf.Height = 1000;
wmf.Format.Unit = 288;
wmf.AddPolyFillMode(PolyFillMode.WINDING);
wmf.AddCreateBrushIndirect(Color.Blue, BrushStyle.BS_SOLID);
wmf.AddSelectObject(0);
wmf.AddCreatePenIndirect(Color.Black, PenStyle.PS_SOLID, 1);
wmf.AddSelectObject(1);
wmf.AddRectangle(100, 100, 800, 800);
wmf.AddDeleteObject(0);
wmf.AddDeleteObject(1);
wmf.Save("D:\\test.emf");
}
private void button2_Click(object sender, EventArgs e)
{
GrayMap map = new GrayMap();
map.GrayFile("D:\\test.emf", "D:\\test2.emf");
}

Watin: Iterating through text boxes in a telerik gridview

I am currently developing a testing framework for a web data entry application that is using the Telerik ASP.Net framework and have run into a blocker. If I step through my code in debug mode the test will find the text box that I am looking for and enter some test data and then save that data to the database. The problem that I am running into is that when I let the test run on it's own the test fails saying that it couldn't fine the column that was declared. Here is my code:
/*Method to enter test data into cell*/
private TableCell EditFieldCell(string columnHeader)
{
var columnIndex = ColumnIndex(columnHeader);
if (columnIndex == -1)
throw new InvalidOperationException(String.Format("Column {0} not found.", columnHeader));
return NewRecordRow.TableCells[columnIndex];
}
/*Method to return column index of column searching for*/
public int ColumnIndex(string columnHeader)
{
var rgTable = GridTable;
var rgCount = 0;
var rgIndex = -1;
foreach (var rgRow in rgTable.TableRows)
{
foreach (var rgElement in rgRow.Elements)
{
if (rgElement.Text != null)
{
if (rgElement.Text.Equals(columnHeader))
{
rgIndex = rgCount;
break;
}
}
rgCount++;
}
return rgIndex;
}
My thinking is that something with my nested for loops is presenting the problem because the rgIndex value that is returned when I let the program run is -1 which tells me that the code in the for loops isn't being run.
TIA,
Bill Youngman
Code that gets the table Column index. You need to pass the Table(verify that the table exists while debug):
public int GetColumnIndex(Table table, string headerName)
{
ElementCollection headerElements = table.TableRows[0].Elements; //First row contains the header
int counter = 0;
foreach (var header in headerElements)
{
if (header.ClassName != null && header.ClassName.Contains(headerName)) //In this case i use class name of the header you can use the text
{
return counter;
}
counter++;
}
// If not found
return -1;
}

Resources