当前位置: 首页 > 编程语言 > C#

C#LearningTutorial-UsingWatiNScripttoPassKeystrokes(i.e.EnterKey)intotheApplicationShare

时间:2023-04-11 11:30:15 C#

UsingWatiNScripttoPassKeystrokes(i.e.EnterKey)intotheApplication我正在使用WatiN测试工具。我可以使用WatiN脚本将击键(即按Enter键)传递给应用程序吗?此选项在WatiR中可用。这个选项在WatiN中可用吗?编辑:经过进一步检查,我发现发送ENTER键的标准方式在WatiN中不起作用,就像在WatiR中一样。您需要使用System.Windows.Forms.SendKeys另外,我建议您下载WatiNTestRecorder。这是示例代码。using(IEie=newIE("http://someurl")){TextFieldmyTxt=ie.TextField(Find.ById("myTextBox")).TypeText("一些值");System.Windows.Forms.SendKeys.SendWait("{ENTER}");DegreeDevBlog上有一篇非常好的博文,它解释了如何将Enterpress添加为扩展方法,如下所示:hWnd);[DllImport("user32.dll")][返回:MarshalAs(UnmanagedType.Bool)]privatestaticexternboolSetForegroundWindow(IntPtrhWnd);publicstaticvoidTypeTextQuickly(thisTextFieldtextField,stringtext){textField.SetAttributeValue("value",text);}publicstaticvoidPressEnter(thisTextFieldtextField){SetForegroundWindow(textField.DomContainer.hWnd);SetFocus(textField.DomContainer.hWnd);textField.Focus();System.Windows.Forms.SendKeys.SendWait("{ENTER}");线程.睡眠(1000);这使得在测试中按下Enter变得非常容易。browser.TextField("txtSearchLarge").PressEnter();vartextUrl=ie.TextField("txtUrl");textUrl.Focus();textUrl.TypeText("www.mydomain.com");线程.睡眠(3000);textUrl.KeyDown((char)Keys.Down);textUrl.KeyDown((char)Keys.Down);textUrl.KeyDown((char)Keys.Enter);您必须使用System.Windows.Forms。为什么不执行以下操作?using(IEie=newIE("http://someurl")){TextFieldmyTxt=ie.TextField(Find.ById("myTextBox")).TypeText("一些值");TextField.KeyPress('r');\r是一个回车}我正在使用Watin来测试只要浏览器有焦点,上面的答案就有效,如果没有,那么SendKeys.SendWait会为任何有焦点的应用程序触发。ie.Eval("vare=$.Event('keydown');e.which=$.ui.keyCode.ENTER;$('#myTextBox').trigger(e);");有点笨拙,但无论如何都会触发输入。试试这个://此方法旨在模拟最终用户按下ENTER键。privatevoidCheckKeys(objectsender,KeyPressEventArgse){//设置要按下的键;在这种情况下,ENTER键。if(e.KeyChar==(char)13){//按下ENTER键。e.Handled=true;然后,当您需要模拟按下的ENTER键时,只需调用此方法即可。以上就是C#学习教程:使用WatiN脚本将击键(即Enter键)传递给应用中共享的全部内容。如果对你有用,需要了解更多C#学习教程,希望大家多加关注——本文来自网络收藏,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢