|
如下代码主要完成了如下操作:
在http://localhost:8081/phpwind_UTF8_8.7/upload/网页上将所有对象遍历并把对象类型打印出来
进行对象类型的匹配(如:DomTextField)
进行对象属性的匹配(如:nav_pwuser)
在匹配好的对象上完成了数据的代填(如:用户名)
给所有的DomTextField对象上完成了数据的代填(如:文本框)
下面是完整的代码:- import java.util.List;
- import com.borland.silktest.jtf.Desktop;
- import com.borland.silktest.jtf.TestObject;
- import org.junit.Before;
- import com.borland.silktest.jtf.BrowserBaseState;
- import org.junit.Test;
- import com.borland.silktest.jtf.xbrowser.BrowserApplication;
- import com.borland.silktest.jtf.xbrowser.BrowserWindow;
- import com.borland.silktest.jtf.xbrowser.DomButton;
- import com.borland.silktest.jtf.xbrowser.DomLink;
- import com.borland.silktest.jtf.xbrowser.DomTextField;
- import com.borland.silktest.jtf.common.types.MouseButton;
- import com.borland.silktest.jtf.common.types.Point;
- public class findAllWEBMethod {
- private Desktop desktop = new Desktop();
- @Before
- public void baseState() {
- BrowserBaseState baseState = new BrowserBaseState();
- baseState.execute(desktop);
- }
- @Test
- public void findAllMethod1() {
- desktop.<BrowserApplication>find("/BrowserApplication").setActive();
- desktop.<BrowserWindow>find("/BrowserApplication//BrowserWindow").navigate("http://localhost:8081/phpwind_UTF8_8.7/upload/");
- desktop.<DomTextField>find("//INPUT[@id='nav_pwuser']").click();
- desktop.<DomTextField>find("//INPUT[@id='nav_pwuser']").setText("admin");
- desktop.<DomTextField>find("//INPUT[@id='showpwd']").click();
- desktop.<DomTextField>find("//INPUT[@id='showpwd']").setText("admin");
- desktop.<DomButton>find("//BUTTON[@name='head_login']").click();
- desktop.<DomLink>find("//A[@textContents='退出']").click();
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //获取所有的browserWindow的list对象
- List<TestObject> lc = desktop.findAll("//*");
- //List<TestObject> lc = desktop.getChildren();
- //如果需要获取所有的DomTextField的list对象
- //List<TestObject> lc = desktop.findAll("//DomTextField");
- //定义一个自定义属性值名称
- String propertys = "id";
- //定义一个TestObject对象
- TestObject tobj;
- //获取browserWindow的list的总数
- int lcc = lc.size();
- System.out.println(lcc);
- //以list总数去遍历整个list对象
- for (int i = 0; i < lcc; i++)
- {
- //获取lc的i个对象
- tobj = lc.get(i);
- List<String> tobjl;
- String tobjs = null;
- String tobjt;
- //获取tobj对象的类型,如“SilkTest.Ntf.PushButton”
- //获取到的数据为“com.borland.silktest.jtf.xbrowser.DomLink”
- //tobjt = tobj.getClass().getName().toString();
- //获取到的数据为“DomLink”
- tobjt = tobj.getClass().getSimpleName();
- System.out.println(tobjt);
- //获取tobj对象的属性值,如“name”
- //获取对象的所有属性列表
- tobjl = tobj.getPropertyList();
- System.out.println(tobjl);
- //tobjs = tobj.GetProperty("DomTextField[id]").toString();
- try
- {
- tobjs = tobj.getProperty(propertys).toString();
- System.out.println(tobjs);
- //判断对象类型等于"SilkTest.Ntf.PushButton"
- if (tobjt.equals("DomTextField"))
- {
- //如果tobjs的属性为“nav_pwuser”则对其进行赋值
- if (tobjs.equals("nav_pwuser"))
- {
- //给对象类型为"SilkTest.Ntf.XBrowser.DomTextField",对象属性为tobjs的对象赋值为admin
- desktop.<DomTextField>find("//INPUT[@" + propertys + "='" + tobjs + "']").setText("admin");
- }
- //给所有的DomTextField赋值
- desktop.<DomTextField>find("//INPUT[@" + propertys + "='" + tobjs + "']").setText("admin");
- }
- }catch(Exception e){
- //e.toString("对象不支持此" + propertys + "属性!");
- //e.printStackTrace();
- //e.printStackTrace();
- }
- }
- }
- }
复制代码 |
|