|
如下代码主要完成了如下操作:
在http://localhost:8081/phpwind_UTF8_8.7/upload/网页上将所有对象遍历并把对象类型打印出来
进行对象类型的匹配(如:SilkTest.Ntf.XBrowser.DomTextField)
进行对象属性的匹配(如:nav_pwuser)
在匹配好的对象上完成了数据的代填(如:用户名)
完整的代码如下:- using System;
- using System.Collections.Generic;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using SilkTest.Ntf;
- using SilkTest.Ntf.XBrowser;
- using System.Text.RegularExpressions;
- namespace Silk4NETFoundAllObject
- {
- [SilkTestClass]
- public class findAllWEBObject
- {
- private readonly Desktop _desktop = Agent.Desktop;
- [TestInitialize]
- public void Initialize()
- {
- BaseState baseState = new BaseState();
- baseState.Execute();
- }
- [TestMethod]
- public void TestMethod11()
- {
- BrowserApplication webBrowser = _desktop.BrowserApplication();
- BrowserWindow browserWindow = webBrowser.BrowserWindow();
- browserWindow.Navigate("http://localhost:8081/phpwind_UTF8_8.7/upload/");
- browserWindow.DomTextField("//INPUT[@id='nav_pwuser']").Click(MouseButton.Left, new Point(70, 9));
- browserWindow.DomTextField("//INPUT[@id='nav_pwuser']").TypeKeys("admin");
- browserWindow.DomTextField("//INPUT[@id='showpwd']").Click(MouseButton.Left, new Point(45, 10));
- browserWindow.DomTextField("//INPUT[@id='showpwd']").TypeKeys("admin");
- browserWindow.DomButton("//BUTTON[@name='head_login']").Click(MouseButton.Left, new Point(20, 11));
- Assert.AreEqual("admin", browserWindow.DomElement("//SPAN[@textContents='admin'][1]").Text);
- browserWindow.DomLink("//A[@textContents='退出']").Click(MouseButton.Left, new Point(13, 6));
- System.Threading.Thread.Sleep(2000);
- //获取所有的browserWindow的list对象
- IList<TestObject> lc = browserWindow.FindAll("//*");
- //如果需要获取所有的DomTextField的list对象
- //IList<TestObject> lc = browserWindow.FindAll("//DomTextField");
- //定义一个自定义属性值名称
- String propertys = "id";
- //定义一个TestObject对象
- TestObject tobj;
- //获取browserWindow的list的总数
- int lcc = lc.Count;
- Console.WriteLine(lcc);
- //以list总数去遍历整个list对象
- for (int i = 0; i < lcc; i++)
- {
- //获取lc的i个对象
- tobj = lc[i];
- String tobjs;
- String tobjt;
- //获取tobj对象的类型,如“SilkTest.Ntf.PushButton”
- tobjt = tobj.GetType().ToString();
- //获取tobj对象的属性值,如“name”
- //tobjs = browserWindow.DomTextField("//INPUT[@id='showpwd']").GetProperty("name").ToString();
- tobjs = tobj.GetProperty(propertys).ToString();
- //tobjs = tobj.GetProperty("DomTextField[id]").ToString();
- Console.WriteLine(tobjt);
- Console.WriteLine(tobjs);
- //判断对象类型等于" SilkTest.Ntf.XBrowser.DomTextField "
- if (tobjt == "SilkTest.Ntf.XBrowser.DomTextField")
- {
- //如果tobjs的属性为“nav_pwuser”则对其进行赋值
- if (tobjs == "nav_pwuser")
- {
- //给对象类型为"SilkTest.Ntf.XBrowser.DomTextField",对象属性为tobjs的对象赋值为admin
- browserWindow.DomTextField("@" + propertys + "='" + tobjs + "'").SetText("admin");
- }
- }
- }
- }
- }
- }
复制代码 |
|