WatiN testing of a popup - c#

I have an application that brings up a popup window when you click a link. I have a watin test that naviates to the page and clicks a link to open the popup. This is my current code:
[Test]
public void TestCommentBoxInput()
{
window.GoTo("mylocalurl");
window.Link(Find.ById("popuplink.aspx")).Click();
IE iepopup_1 = IE.AttachTo<IE>(Find.ByUrl("popuplinkurl.aspx"));
iepopup_1.TextField(Find.ById("txtComments")).TypeText("Commenttest");
}
As you can see I tried attatching the popup window to the created browser called window. When I run my test it just stops at the popup window and never enters text in the box. How do I go about making my program regonize that it is now to be operating on the popup and not the original window?
EDIT: I am dealing with a Modal Dialog.

I think the Find.ByUrl try to do a exact match, try with a Find.ByUrl(u => u.Contains("popuplinkurl.aspx"))

So I have figured out the problem, the problem was I was using a Modal dialog and they are handled differently. My new code is as follows in case anyone is stuck in the same position I was in. :)
public void TestCommentBox()
{
window.GoTo("mylocalurl");
window.Link(Find.ById("popuplink.aspx")).ClickNoWait();
HtmlDialog dialog = window.HtmlDialog(Find.ByTitle("TestPopup"));
dialog.TextField(Find.ById("Txtcomments")).TypeText("Commmenttest!");
}
The important lines are:
window.Link(Find.ById("popuplink.aspx")).ClickNoWait();
Notice that I am using ClickNoWait() and not just Click, I am unsure as to why this makes the difference, but it does! If someone could explain that that would be great.
HtmlDialog dialog = window.HtmlDialog(Find.ByTitle("TestPopup"));
Because I am dealing with a Modal dialog you have to declare a new HtmlDialog. Also in order to use Html dialog make sure you include Watin.Core.DialogHandlers. I hope this is helpful to someone out there! :)

Related

Disabling Dialog Flashing C#

I have a populated ListView dialog. If a user clicks on an option in the ListView, a new dialog is shown above the ListView.
My problems is that when I click off of the new top-most dialog (onto the ListView behind it), the new dialog's borders flash/blink several times. The icon on the taskbar also flashes. I wish to disable the flashing, but cannot find a property to change.
To show my dialog, I use the following code:
if (detail == null)
detail = new Details(opt, val, user, desc, m_l);
else
detail = null;
detail.ShowDialog();
This is intended behavior, it's because the new dialog is modal. It's drawing attention to the fact that something needs to be done.
If you need to make a non-modal form, instead of using ShowDialog(), simply use Show().
Sounds like to me you are creating modal windows each time. And you cannot resume the previous dialogs until you dismiss your new top-most window.
Take a look at this wikipedia article for information about modal dialogs.
I would advise you look at how you are creating/showing your windows.
In WPF you show windows via Show() or ShowDialog(), however, I do not know which type of ListView you are using
EDIT:
Per your comment, you want modal dialogs. The only ways I can think of even trying to remove the flashing is going into WINAPI. This doesn't seem like a job for .NET.
I want to suggest a few things:
Take a look at options for showing each window. See this MSDN page
Take a look at the options for styling each window. See this MSDN page
Reconsider your design. I know this may take a lot of work, but having so many layers of windows is kind of unappealing to most users. Ultimately, I believe this option will make your application the best.
Thank you all for your answers and guidance. I have found the best way to handle my problem.
I was using an event ItemActivated. This event was called when an a highlighted item on the ListView was clicked. This became a problem when the user would double click on an already selected item. This would cause the new dialog to show, but also flash several times.
By using the DoubleClick event instead, a single click on a selected object does nothing. A double click on either a selected or non-selected item opens the dialog without the flashes. The flashes still appear if you try to click off of the dialog box, but are not as much of an issue.

Custom dialog box in wpf issue

I'm calling my custom dialog window with this code:
GUI.SLDialog sd = new GUI.SLDialog();
if (sd.ShowDialog() == false)
{
return;
}
But sd.ShowDialog() always returns nothing (i think), because the function breaks, but the waypoint at return; isn't reached.
Dialog is automaticly closing when I add to button:
this.DialogResult = false;//or true
Anybody know what am I doing wrong?
Thanks in advance for your help.C.H.
#edit
This is my SLDialog:
xaml: http://wklej.org/hash/9fb67fb0c7c/
cs: http://wklej.org/hash/16e3ccc6c0d/
I don't think I can tell you much here unless you post the code for the dialog but I do have a suggestion in the mean time.
Since you're already unhappy with the standard dialog boxes and customization is clearly an option why not move towards what people are coming to expect? Instead of your standard dialog why not just create a user control that lays over the rest of your UI and blurs everything out from the background? Much like a jquery dialog box you might see on a web page.
Modality is easier to control since it's just a matter of covering your entire app window with a translucent rectangle and then make the dialog window appear however you want.
Just a suggestion.

Sending text into whatever window that is currently in focus

I'm trying to write a program with C# that sends text into other windows.
How do I write a command in C# that sends a text into the window that is currently under the users focus?
For example:
If the user clicks an open notepad window, or an open outlook letter, or an open excel sheet, and then clicks the button on my program, a text will be "pasted" directly into the last notepad window/outlook letter/excel cell that the user clicked on last.
I hope my question is clear enough. I'm not so experienced and am missing a lot of terminology.
Take your application out of focus by minimizing or hiding the main window, and then send your text with
SendKeys.SendWait("Hello World!");
Finally, restore your main window.
If the code is executed in the main form, you could do this
this.Visible = false;
SendKeys.SendWait("Hello World!");
this.Visible = true;
Olivier's response actually seems more accurate (and taught me something :)) than my original "does not seem achievable". If you need an example, then take a look at this:
http://www.codeproject.com/Articles/18366/Sending-Keystrokes-to-another-Application-in-C
However, on a more complex level, without an API to call into, there is not much more that you can do beyond this solution.

C# WinForms Wait on form to proceed

I'm tired and hungry, so I might of missed it, but from what I can see no existing post covers this...
I'm writing a plugin for an application. My plugin loads a form to get some data specifically, it uses the webcam to scan for a barcode. Once it's found a barcode, the form hides itself (incase it's needed again later). This is how I currently call the form that does the barcode work:
string readData = null;
if (eye == null)
{
System.Windows.Forms.Application.EnableVisualStyles();
eye = new CamView();
}
eye.Show();
if (eye.found)
{
readData = eye.readData;
}
return readData;
So, my problem is that eye.show() doesn't block. It makes the form appear and carries right on before there's a chance for the barcode to appear. I imagine I need to use some form of threading or locking, but my crude attempts to do so have just frozen the interface completely.
The "eye" form is basically just a viewfinder for the webcam, and relies on the camera_OnImageCapture event to make it do it's image checks for the barcode.
Is there an elegant way to make the application calling the plugin wait for the form to finish? Or do I just need to add an accept button to the "eye form?"
Cheers. And humble apologies if this is in anyway a repost.
.ShowDialog();
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
"You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed."
You are on the right track. You change the code to show CamView as a modal dialog but do no add an Accept button. Instead change camera_OnImageCapture to close the dialog.

Issue with WatiN and IE9 regarding multiple dialogs

I am using the latest 4/12/2011 build of WatiN (2.1.0.1196).
I have an aspx page loaded into an IFrame in Dynamics Crm. There is a button on the page that opens and modal dialog form. So far most of the test cases are working correctly with WatiN except for a specific use case.
If a specific combination of controls are set on the modal dialog, a confirm dialog box will pop up after the user presses the submit button. If the user selects ok it will continue the submit execution, if cancel it will return back to the modal dialog. The test times out when this confirm dialog box appears. I cant seem to get the handler setup right to catch the confirm dialog.
Here is a test method I have been trying:
[TestMethod]
public void Add_New_Post_To_Record_Public_NotOnBehalf_NoSub_No_Notifications()
{
using (var browser = new IE("URL to the IFRAME"))
{
var approveConfirmDialog = ReturnDialogHandler.CreateInstance();
var confirmCode = Guid.NewGuid();
//logon to CRM
logonToADFS(browser);
var recordPage = browser.Page<DiscussionRecordpage>();
recordPage.CreateNewPostButton.ClickNoWait();
HtmlDialog dialog = browser.HtmlDialog(Find.ByTitle("New Post"));
var messageText = dialog.TextField(Find.ByClass("required"));
messageText.TypeText("Type some text. Confirmation code: " + confirmCode.ToString());
var button = dialog.Button(Find.ByClass("submit-button"));
using (new UseDialogOnce(browser.DialogWatcher, approveConfirmDialog))
{
button.ClickNoWait();
approveConfirmDialog.WaitUntilExists();
approveConfirmDialog.OKButton.Click();
}
browser.WaitForComplete();
Assert.IsTrue(browser.ContainsText(confirmCode.ToString()));
}
}
I have tried a couple different variations all with the same result. I assume that I need to use the ReturnDialogHanlder.CreateInstance() method for IE9 compatibility, but the standard ConfirmDialogHandler did not seem to work either. Honestly, I don’t know if I am using the dialog handler correctly in this case, but I can click the ok buttons on other confirm dialogs that spawn from buttons on other pages. This is a bit unique since it is spawning from a modal dialog instead of a page.
I also tried using this custom handler (C# WatiN - Add an AlertDialogHandler to click ok button on every Alert dialog window) with no results.
Thanks in advance.
It's too hard. I suggest a workaround: most of the case, you could close the dialog using keyboard. so you could sendkeys to close the dialog. Just FYI.

Categories