Pass a key stroke (i.e., Enter Key) into applicati

2019-01-23 15:28发布

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?

标签: c# watin
6条回答
淡お忘
2楼-- · 2019-01-23 15:39

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-23 15:51

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();
查看更多
Rolldiameter
4楼-- · 2019-01-23 15:54

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}");      
}
查看更多
来,给爷笑一个
5楼-- · 2019-01-23 15:56
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.

查看更多
爷、活的狠高调
6楼-- · 2019-01-23 15:56

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

查看更多
贪生不怕死
7楼-- · 2019-01-23 15:59

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.

查看更多
登录 后发表回答