Skip to content

Instantly share code, notes, and snippets.

@natritmeyer
Created May 11, 2011 18:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natritmeyer/966981 to your computer and use it in GitHub Desktop.
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
/*
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();
}
}
}
@smurtagh
Copy link

Worked great for me. Thanks @natritmeyer

@ChandrasekarRadhakrishnan

Hi Guys,

I have a requirement i need to automate a web application using c#.
Right now i have created a windows form application and have used web browser tool in tht.
But im struck on open dialog file i can able to select the file but i cant able to clcik the open button.Can u explain how to do in web browser.

@deaktomi
Copy link

This code works perfect on Windows 10. It doesn't work on Windows 7.

On Windows 7 the Open button looks like this:
win7button

On Windows 10 th Open button looks like this:
win10button

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.

@jainsushma
Copy link

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