TA的每日心情 | 奋斗 2021-8-16 14:04 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]测试小兵
|
Coded UI中用C#对WebBrowser执行JavaScript,之前一直纠结这个问题各种找方法,最后查看WatiN中的Ru
nScript()方法的实现,终于有所收获。
有例有真相:Technet Library的登录实现:
- 复制代码
- [TestMethod]
- public void VerifySignIn()
- {
- string url = "https://login.live-int.com/login.srf?wa=wsignin1.0&rpsnv=46&ct=1364435925&rver=6.0.5276.0&wp=MCLBI&wlcxt=TechNet%24TechNet%24TechNet&wreply=http%3a%2f%2ftechnettest.redmond.corp.microsoft.com%2fen-US%2flibrary%2faa991542%3flc%3d1033&lc=1033&id=266001&mkt=en-US";
- string name = "abc";
- string psw = "123";
- //js代码:f1是表单,i0116是UserName文本框的id,i0118是Password的文本框id,idSIButton9是Sign in按钮的id。
- string scriptCode = "f1.i0116.value=" + "'" + name + "'; f1.i0118.value=" + "'" + psw + "'; f1.idSIButton9.click();";
-
- BrowserWindow browser = BrowserWindow.Launch(new Uri(url));
- HtmlInputButton _btnSubmit = new HtmlInputButton(browser);
- _btnSubmit.SearchProperties[HtmlInputButton.PropertyNames.Id] = "idSIButton9";
- //将HtmlControl转换成HtmlElement,然后获取到IHTMLDocument2对象,进而获取到IHTMLWindow2对象,然后调用其execScript()方法
- IHTMLElement btnSubmit = _btnSubmit.NativeElement as IHTMLElement;
- var doc = btnSubmit.document as IHTMLDocument2;
- IHTMLWindow2 window = doc.parentWindow;
- window.execScript(scriptCode, "javascript");
- browser.WaitForControlReady();
- }
复制代码
|
|