mouseMove for Watin - c#

I'm able to perform a mouse move over that triggers a menu down with Selenium's mouseMove function, but have codded most of my browser automation is Watin. Is there a suitable replacement for this option in Watin?
I've tried
div.FireEvent("onMouseMove");
div.MouseEnter();
with no luck. I target the same div classes. mouseMove for Selenium works, onmouseMove for Watin doesn't.

Working with some cranky form validation, I had to call the javascript change() method directly to get form validation to fire as Watin's element click() wasn't doing it.
Calling change() looked like this.
var jqElement = string.Format("window.jQuery({0})", GetJavascriptElementReference());
DomContainer.Eval(string.Format("{0}.change()", jqElement));
Obviously you'd need to change the window.jQuery... portion to point at your form, and change the change() method to the appropriate mouseover method you need called.
The negative here is the fragility due to calling a specific javascript method in your test code.
Another example of calling javascript directly can be seen in my answer here: Autocomplete DropDown Menu Testing using WatiN

Give mouse move a try...
div.FireEvent("mouseenter");
div.FireEvent("mousemove");

Related

Winform app, force execute OnLoad Event when focus is on another tab

I have a WinForm app, the form has TabControl, control has three tabs tabPage1,tabPage2,tabPage3.
The Tab 'tabPage3' is hosting a User defined control which internally has one or more child controls.
Now my problem lies in tabPage3,
I know it is a pure Winforms behavior, until your parent is not activated child controls Onload event won't fire.
I have a requirement to force the Onload event to fire when the focus is on tabPage1, tabPage2. Is there any way to force the Onload event to fire.
I have already visited following links but didn't find any clue. Link Link Link
This is a very unusual requirement, strongly smells like an XY problem. The Load event is heavily over-used in Winforms, a side-effect of it being the default event for a Form or UserControl. One of the behaviors inherited from VB6, the Load event was a big deal in that language. What you want can easily be accomplished by not giving Winforms a choice:
public UserControl3() {
InitializeComponent();
CreateHandle();
}
The CreateHandle() call does the forcing, OnLoad will immediately run. But do be aware that this happens very early, too early to do the kind of things that you'd really want to use OnLoad() or the Load event for. Which are rather limited, it is only truly necessary to discover the actual Location and Size of the control. Anything else belongs in the constructor. Surely including the code that you now run in OnLoad().
Strongly favor using the constructor instead.
I had a similar problem for a previous project, for my needs I managed to just iterate over every tab page in the forms constructor (or possibly OnLoad I can't remember) and then reset the index back to 0 before ever showing the end user.
Something similar to:
for(int i = 1; i < tabControl.TabCount; i++)
tabControl.SelectTab(i);
tabControl.SelectTab(0);

Possible to simulate click event on Dialog window of Browser?

I know that the dialog(showMessage) is a closed API and that you can not force a click event on Dialog with any web-based technologies such as jQuery or Javascript. The instance of the window within the browser is single threaded and locks the thread until the dialog receives an event. This I understand.
What I am trying to do is simulate a click event pragmatically for Test Case purposes. I am using the Telerik testing framework to run these Test Cases in C# .NET 4.5 environment.
So is it possible to simulate this click event? It is testing the behavior of one our buttons that when clicked the user must confirm they are leaving the page without saving changes.
Thanks to all in advance!
I am not familiar with Telerik's testing tools, but as far as i know the only way to "issue" such a click would be with a ui macro that automated mouse motions and actually clicked the screen at a particular location.
That said, you may be able to solve your problem by using a mocked method. Rather than directly calling window.prompt, instead define your own prompt function along the lines of:
debug = true; //remove or set to false when not testing
var myPrompt = function(){
if(debug){
return "Greetings, Program";
} else {
return prompt("Please enter your greeting:","Greeting");
}
}
You can naturally set this up for other types of message box, so long as you keep the type they return in mind.

IHTMLDocument -- Click an IHTMLElement by ID, not Name

The input field that I need to fill in has it's name value set to j_password. The button I would like to click is a link within a div that has an id set to loginBtn.
I'm trying something like this:
mshtml.IHTMLDocument2 doc = ((mshtml.IHTMLDocument2)webBrowserControl.Document);
((mshtml.IHTMLElement)doc.all.item("j_password")).setAttribute("value", password);
((mshtml.IHTMLElement)doc.all.item("loginBtn")).click();
But the button is never clicked. I can't tell if it is because it isn't really a button, or if it is because it doesn't have a name attribute and I'm trying to use an id.
Anyway, does anyone see a fix to this?
If the click call did not throw a NullReferenceException, then you found the element. But it is better to use the standard IHTMLDocument3::getElementById orIHTMLDocument3::getElementsByName instead of document.all for the future.
As for why click does not work, it depends on how click is supposed to work. There are a lot of side effects of a mouse click, and the browser may limit those effects if the mouse is not over the element between a mouse down and a mouse up.
If the click is supposed to call a javascript function, just call the function directly (ExecScript). Otherwise, find the login form element, call its onsubmit handler function if there is one, then call its submit method.

sharepoint webpart with many usercontrols

I have created sharepoint 2010 visual webpart in VisualStudio2010 with three user controls (.ascx). I want to dynamically change usercontrol in the webpart by clicking some button at currently loaded usercontrol. The main problem consist in the fact that buttonClick event is handled only after execution CreateChildControls method (where I try to get needed usercontrol using ViewData). Could anyone please help me to solve this problem?
Lee's response is basically right and may work well for you. However, you should not just use __doPostBack and rely that it will be always "magically" there for you. This method and variables mentioned by Lee are internal to ASP.NET and they are not meant to be used directly. Also, if you do not place any postback-ing control on your page this method will actually not be generated and your code calling it would fail.
Luckily, the code to cause and handle a generic postback is very simple. Instead of using built-in event handlers of input controls (which need to be constructed before being triggered - hence the call to CreateChildControls before your handler is called) you would target the postback to the Web Part itself:
public class MyWebPart : WebPart, IPostBackEventHandler {
protected override void CreateChildControls() {
Control clickable = ...; // Create a clickable control.
// Get JavaScript expression to send postback "test" to "this" web part.
var postBack = Page.ClientScript.GetPostBackEventReference(this, "test");
clickable.Attributes["onclick"] = postBack + "; return false";
Controls.Add(clickable);
}
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
if (eventArgument == "test") { // Recognize and handle our postback.
...
}
}
}
The GetPostBackEventReference will generate the necessary JavaScript expression for you. (And actually, just calling it makes the __doPostBack "magically" appear on the page.) The RaisePostBackEvent will be called between OnLoad and OnPreRender. Make sure not to cause child controls be created before that (by calling EnsureChildControls, for example, or by any other means). If you need multiple postback-ing controls the eventArguments parameter will let you differ among them.
You need the postback triggers in your user controls and not directly in the Web Part. I showed it in the Web Part just to keep it simple. You can put the result of GetPostBackEventReference to any control providing you use the right Page and Web Part instances when calling it.
--- Ferda
A way to do this would be have the button call a javascript function that in turn calls the following:
__doPostBack('LoadControl', 'ControlName');
You can then use the server variables __EVENTTARGET and __EVENTARGUMENT to find out which control to load within your CreateChildControls event handler.
I had that problem too.
Add this to the event handler (after executing your code inside the handler)
this.Page.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri, true);
Regards,
Pedro

WatiN choosing the correct event string to use with FireEventNoWait for an input textfield

I want to get the input value of an <input type="text"> element, on the fly (while it is being typed) and implement a search method with it as parameter.
I have this piece of code:
_window.Frame(WatiN.Core.Find.ById("a_frame"))
.TextField(WatiN.Core.Find.ById("an_element"))
.FireEventNoWait("event_string", other params);
What event would you think is the best suited for this? I have some thoughts on KeyPressed or KeyUp, but I'd like some other opinions for this matter? I have searched for TextChanged and some similar Event, but I haven't found anything.
Are we to assume the above code isn't working, or? If nothing else works you could always do a do loop right after the text box gets focus, do loop would contain these:
Sleep 100
Doevents
And of course after every 100 ms break and a Doevents, you can check to see if .value has changed, and if so, query your search. When the text box looses focus, you stop the loop.
You are using the webbrowser control, right? And you want to d this using the webbrowser control as opposed to JavaScript? Because, you can get keyup keydown events through the webbrowser controls eventing system, and that would be a better way to do it, but I'm not clear on the who what when where why of what your doing :)

Categories

Resources