Created
May 11, 2011 18:10
-
-
Save natritmeyer/966981 to your computer and use it in GitHub Desktop.
A tiny c# app that operates the Open File dialog using White. Useful if you're testing a WPF app with bewildr (http://www.bewildr.info) but the app you're testing uses the old Win32 dialog boxes. The compiled exe takes one arg; the value you want typed in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a console app, add references to the various white libraries and use the following as a template for operating the Open Dialog window | |
Get the white binaries from here: http://white.codeplex.com/ | |
*/ | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Windows.Automation; | |
using White.Core; | |
using White.Core.UIItems; | |
using White.Core.UIItems.Finders; | |
using White.Core.UIItems.WindowItems; | |
namespace OpenFile | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Application myApp = Application.Attach("YOUR_APP_PROCESS_NAME_HERE"); | |
List<Window> myWindows = myApp.GetWindows(); | |
Window openDialog = myWindows.Where(n => n.Name == "Open").First(); | |
TextBox textField = openDialog.Get<TextBox>(SearchCriteria.ByControlType(ControlType.Edit).AndByText("File name:")); | |
textField.Text = args.First(); | |
Button openButton = openDialog.Get<Button>(SearchCriteria.ByControlType(ControlType.Button).AndByText("Open")); | |
openButton.Click(); | |
} | |
} | |
} |
childWindow.Get(SearchCriteria.ByAutomationId("1148")).Select(1);
var OpenButton = childWindow.Get(SearchCriteria.ByClassName("Button").AndAutomationId("1"));
OpenButton.DoubleClick();
works for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code works perfect on Windows 10. It doesn't work on Windows 7.
On Windows 7 the Open button looks like this:

On Windows 10 th Open button looks like this:

I guess that extra arrow makes it impossible for TestStack.White to find the button with this code:
Button openButton = openDialog.Get<Button>(SearchCriteria.ByControlType(ControlType.Button).AndByText("Open"));
The exception I got:
TestStack.White.AutomationException: Failed to get (ControlType=button or ControlType=check box),Name=Open,ControlType=button
Any idea on it? Pressing enter may be a good solution, but I prefer to tell explicitly which button to press.