13

I'm using WatiN testing tool. Can I pass a key stroke (i.e., pressing a enter key) to the application using WatiN scripts?

This option was available in WatiR. Is this option available in WatiN?

gunr2171
  • 16,104
  • 25
  • 61
  • 88

6 Answers6

11

EDIT: Upon further inspection, I found that the standard way of sending the ENTER key doesn't work in WatiN as it does in WatiR. You need to use System.Windows.Forms.SendKeys

Also, I recommend that you download the WatiN Test Recorder.

Here's the sample code.

using(IE ie = new IE("http://someurl"))
{
  TextField myTxt = ie.TextField(Find.ById("myTextBox")).TypeText("some value");
  System.Windows.Forms.SendKeys.SendWait("{ENTER}");      
}
Jose Basilio
  • 50,714
  • 13
  • 121
  • 117
5
var textUrl = ie.TextField("txtUrl");
textUrl.Focus();
textUrl.TypeText("www.mydomain.com");
Thread.Sleep(3000);
textUrl.KeyDown((char)Keys.Down);
textUrl.KeyDown((char)Keys.Down);
textUrl.KeyDown((char)Keys.Enter);

You have to use System.Windows.Forms.

Mischa
  • 42,876
  • 8
  • 99
  • 111
yongfa365
  • 352
  • 3
  • 7
5

There is a really good blog article about this at the Degree Dev Blog

It explains how you can add the Enter press as an extension method like this:

public static class MyWatiNExtensions
{
    [DllImport("user32.dll")]
    private static extern IntPtr SetFocus(IntPtr hWnd);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    public static void TypeTextQuickly(this TextField textField, string text)
    {
        textField.SetAttributeValue("value", text);
    }

    public static void PressEnter(this TextField textField)
    {
        SetForegroundWindow(textField.DomContainer.hWnd);
        SetFocus(textField.DomContainer.hWnd);
        textField.Focus();
        System.Windows.Forms.SendKeys.SendWait("{ENTER}");
        Thread.Sleep(1000);
    }
}

This makes it very easy to press the Enter key in a test.

browser.TextField("txtSearchLarge").PressEnter();
Stian
  • 1,261
  • 2
  • 19
  • 38
  • public static TextField TypeTextQuickly(this TextField textField, string text) { textField.SetAttributeValue("value", text); return textField; } public static TextField HitEnter(this TextField textField) { SetForegroundWindow(textField.DomContainer.hWnd); SetFocus(textField.DomContainer.hWnd); textField.Focus(); textField.Highlight(false); System.Windows.Forms.SendKeys.SendWait("{ENTER}"); Thread.Sleep(1000); return textField; } changed it so I could do chaining and not highlighting the field. – rball Apr 17 '12 at 16:49
  • Like this: ClientId.TypeTextQuickly("3050").HitEnter(); – rball Apr 17 '12 at 16:51
3

Why not just do the following?

using(IE ie = new IE("http://someurl"))
{
  TextField myTxt = ie.TextField(Find.ById("myTextBox")).TypeText("some value");
  TextField.KeyPress('\r');   \\ \r is a carriage return   
}

Worked for my a test I was developing using Watin

DarthOpto
  • 101
  • 1
  • 1
  • I shortened it: .TypeText("some value\r"); – rball Feb 16 '12 at 17:41
  • DarthOpto, unfortunately KeyPress('\r') is not real key press. I found a bug in App under test: When I press ENTER – it closes the form without saving any changes. I was not able to reproduce the bug with KeyPress('\r'), but Windows Forms method works fine – Dmytro Zharii Mar 26 '12 at 17:36
2

the above answer works fine as long as the browser has focus, if it doesn't then SendKeys.SendWait triggers on whichever application has focus.

ie.Eval("var e = $.Event('keydown');e.which = $.ui.keyCode.ENTER;$('#myTextBox').trigger(e);");

While being a bit clunky this will trigger a press of enter regardless.

Mark Broadhurst
  • 2,675
  • 23
  • 45
  • I like this one more but it seems you are using jQuery and id of text field, is there any other way to pass enter to text field having no id and with pure JavaScript code? Thanks in adv. – rahoolm Dec 20 '13 at 19:27
0

Try this:

//  This method is designed to simulate an end-user pressing the ENTER key.
private void CheckKeys(object sender, KeyPressEventArgs e)
{
    //  Set the key to be pressed; in this case, the ENTER key.
    if (e.KeyChar == (char)13)
    {
        //  ENTER key pressed.
        e.Handled = true;
    }
}

Then, just call this method when you need to simulate the ENTER key being pressed.

Brian
  • 5,069
  • 7
  • 37
  • 47