5

I have a WinForms app, that displays a DataGridView. It is automatically populated from a BindingSource, and contains several rows of data. The columns include the standard things like strings. Some of these columns are CheckBoxes.

Name | User | Admin ---- ---- ----- John | X |
Fred | | X

I am writing some automated tests using TestStack.White. I can read the existing state of the CheckBoxes without issue.

How do I set or clear the checkboxes? I've tried:

  1. Table.Rows[0].Cells[1].Value = true;

Fails because the underlying row/cell is deleted before White can read back the current value.

And also:

  1. Table.Rows[0].Cells[1].SetValue(true);

Fails to set the new value.

Neil
  • 11,059
  • 3
  • 31
  • 56

5 Answers5

1

Because TestStack.White cannot access the underlying object model you will have to come up with a New button or Reset button. Or some GUI way (with logic) to clear the Checkbox values by setting the BindingSource's boolean fields to false.

Here is some psuedo code to illustrate.

private void New_Click(object sender, System.EventArgs e) 
{
   DataTable dt = (DataTable)dgv.DataSource;
   dt[1]["IsAdmin"].value = false;
   //In ASP.Net you also need a dgv.Bind(); //this is not necessary in winforms
}

There should already be a way in your system to test with Checkboxes cleared, it should be consistent with how the end user currently do it.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • It feels wrong that I have to write extra code in the application to be able to test the UI, Fortunately, I have the source code, so I can do this. – Neil Oct 06 '17 at 15:04
  • You should not need to, there should already be a way in your system.. what you are testing with the checkboxes should reflect the end user steps/actions that you're testing. If the user can untick a cell with a click then you should be able to do that with White, maybe this code should be in a CellClick event. – Jeremy Thompson Oct 06 '17 at 21:45
  • But that's the problem, with White, I can't programatically click the check box. If I am manually testing, then of course, the checkbox can be ticked, but this is an automated test, and the automation tool doesn't seem able to click a specific checkbox that is part of a DataGridView. – Neil Oct 08 '17 at 11:41
0

My this be an helpful way to set values? DataGridView checkbox column - value and functionality:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    row.Cells[CheckBoxColumn1.Name].Value = true;
}

Or what is your Table for an Object?

Community
  • 1
  • 1
M. Schena
  • 2,039
  • 1
  • 21
  • 29
  • I suspect this does a string to column number lookup, and then does option 1 in my original question. – Neil Jan 27 '16 at 14:03
0

I believe the check boxes in c# has a property 'Checked'. And even though it's in the DataGridView, I believe the property of the controls remains the same.

So, can you try this instead

Table.Rows[0].Cells[1].Checked = true;

Resources: http://www.c-sharpcorner.com/uploadfile/mahesh/checkbox-in-C-Sharp3/

Sanju
  • 1,478
  • 2
  • 20
  • 41
0

Because your DataGridView is bound to BindingSource, you should change the Boolean value in BindingSource rather than through the dataGridView control.

codeDom
  • 1,623
  • 18
  • 54
  • How do I access the ``BindingSource`` from just the UI? Remember, this is a UI test, so has no access to the internals of the code. – Neil Oct 01 '17 at 15:36
  • If you do UI test then you should just work with keyboard and mouse rather trying to change programmatic. – codeDom Oct 01 '17 at 15:42
  • correct, so how do I do that, when the only UI I can access is the ``DataGridView`` ? TestStack.White only has access to the ``DataGridView``, not ``BindingSource``, – Neil Oct 01 '17 at 16:11
  • `DataGridView` has `DataSource` property, Can TestStack.White access this property? – codeDom Oct 01 '17 at 20:26
0

I also could not extract checkbox that belongs to datagridviewcheckboxcolumn neither from cell nor from row, and finally ended up with a hack using mouse, something like this:

Rect bounds = table.Rows[rowNo].Cells[colNo].Bounds;
Point center = new Point(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
Mouse.Instance.Click(center);
sarh
  • 6,371
  • 4
  • 25
  • 29