I tried to use white to click the new project button, I try using the below code, but it can not work, anyone can help me?
public void Notepad()
{
Application app = Application.Launch("C:\\Program Files (x86)\\SourceMonitor\\SourceMonitor.exe");
Window window = app.GetWindow("SourceMonitor", InitializeOption.NoCache);
Button button = window.Get<Button>("File");
button.Click();
app.Kill();
}
I already figured out how to do it, below is the correct code:
public void Notepad()
{
// Arrange
Application app = Application.Launch("C:\\Program Files (x86)\\SourceMonitor\\SourceMonitor.exe");
Window window = app.GetWindow("SourceMonitor", InitializeOption.NoCache);
// Act
MenuBar menuBar = window.MenuBar;
menuBar.MenuItem("File","New Project").Click(); ; //can use any other search criteria as well.
}
Related
I would like to test to Outlook application and send an email to me by using White Stack framework. I implemented code who click on 'New Item' and after that the new window appears. I want to type a my mail to TextBox 'To' but I don't know how get to access to second window 'Untitled - Message (HTML)'. Photo
[TestMethod]
public void mail()
{
var application = Application.Launch(appPatch);
Thread.Sleep(2000);
var window = application.GetWindow(appTitle, InitializeOption.NoCache);
SearchCriteria searchCriteriaNewEmail = SearchCriteria.ByText("New Email");
Button buttonNewEmail = window.Get<Button>(searchCriteriaNewEmail);
buttonNewEmail.Click();
Thread.Sleep(1000);
string windowTitle = "Untitled‬ - Message(HTML)";
var window2 = application.GetWindow(windowTitle, InitializeOption.NoCache);
SearchCriteria searchCriteriaTo = SearchCriteria.ByText("To");
TextBox tbxTo = window2.Get<TextBox>(searchCriteriaTo);
tbxTo.BulkText = "mymail#gmail.com";
Thread.Sleep(2000);
window.Close();
}
The "second window" is in the same process id, so with that you can't simply use the .GetWindow() method.
For MDI applications you should use MdiChild(), or if it is a modal window, use .ModalWindow() on the parent window, or go back to the application then use Window() to get the window.
I would add that getting the window by it's title is not recommended, since that will change as you type the new title of the e-mail.
I'm trying to click a button on an external windows application. The following code successfully finds the element, brings the parent window into focus and then "manually" clicks the button
This works okay...
Process tProcess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle.StartsWith("MainWindowName"));
if (tProcess != null)
{
TestStack.White.Application application = TestStack.White.Application.Attach(tProcess.Id);
var tWindow = application.GetWindow(SearchCriteria.ByAutomationId("SubWindowName"), InitializeOption.NoCache);
SearchCriteria searchCriteria = SearchCriteria.ByAutomationId("btnCalibrate");
var calibrateBtn = tWindow.Get<TestStack.White.UIItems.Button>(searchCriteria);
tWindow.Focus();
var clickablePoint = calibrateBtn.AutomationElement.GetClickablePoint();
Mouse.Instance.Click(clickablePoint);
}
The problem with this is that Mouse.Instance.Click(clickablePoint); moves the cursor, ideally I don't want the cursor moved.
My initial code tried to click the button directly using the following
Process tProcess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle.StartsWith("MainWindowName"));
if (tProcess != null)
{
TestStack.White.Application application = TestStack.White.Application.Attach(tProcess.Id);
var tWindow = application.GetWindow(SearchCriteria.ByAutomationId("SubWindowName"), InitializeOption.NoCache);
SearchCriteria searchCriteria = SearchCriteria.ByAutomationId("btnCalibrate");
var calibrateBtn = tWindow.Get<TestStack.White.UIItems.Button>(searchCriteria);
tWindow.Focus();
calibrateBtn.Click();
}
but this gives the following error every time
TestStack.White.AutomationException
HResult=0x80131500
Message=Cannot perform action on Button. AutomationId:btnCalibrate, Name:Calibrate, ControlType:button, FrameworkId:WinForm,
Source=TestStack.White
StackTrace:
at TestStack.White.UIItems.UIItem.PerformIfValid(Action action) in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\UIItem.cs:line 254
at TestStack.White.UIItems.UIItem.Click() in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\UIItem.cs:line 231
at BetfairStreamingAPI.RadForm1.radLabelBetTime_Click(Object sender, EventArgs e) in D:
Does anyone know why the second method is throwing this error and if it's possible to fix this so that the button can be clicked without manually moving the cursor?
Edit: Screenshot of attempt to set togglestate
The solution to this particular problem appears to be use .RaiseClickEvent() instead of .Click()
The following code works
Process tProcess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle.StartsWith("MainWindowName"));
if (tProcess != null)
{
TestStack.White.Application application = TestStack.White.Application.Attach(tProcess.Id);
var tWindow = application.GetWindow(SearchCriteria.ByAutomationId("SubWindowName"), InitializeOption.NoCache);
SearchCriteria searchCriteria = SearchCriteria.ByAutomationId("btnCalibrate");
var calibrateBtn = tWindow.Get<TestStack.White.UIItems.Button>(searchCriteria);
calibrateBtn.RaiseClickEvent();
}
It's not entirely clear from the White docs when/why this is preferred. I found method RaiseClickEvent this on this link https://github.com/TestStack/White/commit/7b6d4dbc0008c3375e2ebf8810c55cb1abf91b60
EDIT2
I think you might have found something interesting. Since your button state is Indeterminate, it could be worth turning it on before clicking it:
calibrateBtn.State = ToggleState.On;
EDIT1
Alright, let's sort this out together.
There are only two reasons for that action to fail:
The button is not enabled, which I guess can't be the case
The button is OffScreen
If you do something like
Console.WriteLine(calibrateBtn.IsOffScreen.ToString());
You should see
true
If so, try this before you click it:
var pattern = calibrateBtn.AutomationElement.GetCurrentPattern(System.Windows.Automation.InvokePattern.Pattern);
(pattern as System.Windows.Automation.InvokePattern).Invoke();
This is for iPhone.
I have a button and when it's clicked I want to pop-up another control which covers the whole screen. This screen could have any number of controls. And I can close this screen by clicking on an x in the top right corner or programmatically inside any event on the new screen.
I could probably do this by using a UINavigationController which just brings me to a new screen and has a link back to the previous screen but I would just like to ask if there is another option?
What I am doing is I have a map which shows a users location from a pin. But if the user wants to type in a new location instead of using the pin location then they will click a button, go to a new screen, type in an address and click a "suggested" address from what they type.
Any advice would be appreciated or a link to a code sample would be great
You can't use popover for iPhone, I think that you can use Modal View.
yourButton.TouchUpInside += (object sender, EventArgs e) =>
{
YourController yourController = new YourController();
yourController.ModalPresentationStyle = UIModalPresentationStyle.FormSheet;
this.PresentViewController(yourController, true, null);
};
And to close you only need to dismiss the modal.
If it's an iPad app you can use UIPopoverController.
// myvc is an instance of the view controller you want to display in the popup
UIPopoverController pop = new UIPopoverController(myvc);
// target is another view that the popover will be "anchored" to
pop.PresentFromRect(target.Bounds, target, UIPopoverArrowDirection.Any, true);
If it's an iPhone app you can't use UIPopoverController. If you just want a small input box with a single button, you can use UIAlertView (this works for iPhone and iPad)
UIAlertView alert = new UIAlertView();
alert.Title = "Title";
alert.AddButton("OK");
alert.Message = "Please Enter a Value.";
alert.AlertViewStyle = UIAlertViewStyle.PlainTextInput;
alert.Clicked += (object s, UIButtonEventArgs ev) => {
// handle click event here
// user input will be in alert.GetTextField(0).Text;
};
alert.Show();
I have asp menu item button inside my page.so when i click on that button i need to open a pop up on this current tab and open a new tab and focus on it. When user comeback to the actual tab then will click 'OK' on the pop up before they continue anything.This is what I got so far:
var menuTable = document.getElementById("<%=ASPTableMenu.ClientID%>");
var menuLinks = menuTable.getElementsByTagName("a");
menuLinks[0].onclick = function () {
var winPopup = window.open("z_container.aspx");
if (winPopup) {
winPopup.focus();
}
alert('test');
}
Instead of alert I need to open a pop up with OK button and when user click on OK I need to run some conditions.
Thanks
Instead of alert, you have to use confirm:
var menuTable = document.getElementById("<%=ASPTableMenu.ClientID%>");
var menuLinks = menuTable.getElementsByTagName("a");
menuLinks[0].onclick = function () {
var winPopup = window.open("z_container.aspx");
if (winPopup) {
winPopup.focus();
}
//alert('test');
if (confirm("Your message here")) {
//OK button clicked; add your code here
}
}
I have a program that has some buttons, one of them is used to switch "Themes".
There are two themes, one is the normal Windows theme and the other is called Style2.
This is how I tried the switching
private bool UsingWindowsStyle = true;
private ResourceDictionary Style2= new ResourceDictionary() { Source = new Uri("/...;component/Resources/DefaultStyles.xaml", UriKind.RelativeOrAbsolute) };
private void SwitchButton_Click(object sender, RoutedEventArgs e)
{
if (UsingWindowsStyle)
{
Resources.MergedDictionaries.Add(Style2);
UsingWindowsStyle = false;
}
else
{
Resources.MergedDictionaries.Remove(Style2);
UsingWindowsStyle = true;
}
}
My problem is, when I use this program, and press this Button, this is what happens:
Window Opened Program operating normally with Windows theme.
SwitchButton First Click Program changes visuals to the Style2 theme. All the program's buttons operating normally.
SwitchButton Second Click Program reverts back to Windows theme, but all the buttons in the program seize to work.
Points to Consider
The program does not throw any exceptions at this point.
Debugging the code, it seems that after the second click, the program does not enter the SwitchButton_Click method.
I tried readding the Click EventHandler but with no use.
SwitchButton.Click += new RoutedEventHandler(SwitchButton_Click);
Thanks in advance for your help.
I would suggest that you are trying too hard. All you need to do is to change the Style on the Window itself. Leave the dictionaries alone. :-)
Here is an example that changes a windows style when you click from the list of available styles.
My command boils down to
//Here I am changing the style on the window
NewWindow.Style = ((StyleDetailsViewModel)x).Style;
NewWindow.Show();
with various input data
public StylingViewModel(Func<string, Style> findStyle)
{
Styles = new StyleDetailsViewModel[]
{
new StyleDetailsViewModel
{
Name = "None",
Description = "Completely remove all styling and show the raw NavigationWindow including default navigation elements",
WindowStyleNone = false,
Image = "\\Resources\\WindowStyleNone.png"
},
new StyleDetailsViewModel
{
Name = "PlainWindow",
Style = findStyle("PlainWindow"),
Description = "Hides the navigation elemetns of the NavigationWindow to make it look just like a normal window",
WindowStyleNone = false,
Image = "\\Resources\\WindowStylePlain.png"
},
new StyleDetailsViewModel
{
Name = "Windows 7",
Style = findStyle("Win7NavigationWindow"),
Description = "Uses glass effects to create a window that looks almost identical to the control panel from Windows 7.",
WindowStyleNone = false,
Image = "\\Resources\\WindowStyleWin7Nav.png"
},
and
this.DataContext = new StylingViewModel(x => (Style)this.FindResource(x));
Also beware of certain Window properties that can only be set before the window opens, such as WindowStyle="None" which you need if you are doing custom chrome.