C# 使用 WatiN 脚本将击键(即 Enter Key)传递到应用程序中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/856766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:23:18  来源:igfitidea点击:

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

c#watin

提问by

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

我正在使用 WatiN 测试工具。我可以使用 WatiN 脚本将击键(即按下 Enter 键)传递给应用程序吗?

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

该选项在 WatiR 中可用。这个选项在 WatiN 中可用吗?

采纳答案by Jose Basilio

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

编辑:经过进一步检查,我发现发送 ENTER 键的标准方式在 WatiN 中无法像在 WatiR 中那样工作。您需要使用System.Windows.Forms.SendKeys

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

另外,我建议您下载WatiN 测试记录器

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

回答by Brian

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.

然后,当您需要模拟按下 ENTER 键时调用此方法。

回答by DarthOpto

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

为我使用 Watin 开发的测试工作

回答by Mark Broadhurst

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.

只要浏览器有焦点,上面的答案就可以正常工作,如果没有,则 SendKeys.SendWait 会在任何有焦点的应用程序上触发。

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.

虽然有点笨重,但无论如何都会触发按下回车键。

回答by Stian

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:

它解释了如何将 Enter 键添加为扩展方法,如下所示:

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.

这使得在测试中按 Enter 键变得非常容易。

browser.TextField("txtSearchLarge").PressEnter();

回答by yongfa365

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.

你必须使用System.Windows.Forms.