Pass a key stroke (i.e., Enter Key) into application using WatiN scripts

风格不统一 提交于 2019-12-18 03:16:14

问题


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?


回答1:


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}");      
}



回答2:


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();



回答3:


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.




回答4:


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




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/856766/pass-a-key-stroke-i-e-enter-key-into-application-using-watin-scripts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!