robotium 中的组件Class By获取web元素的方法

robotium中的组件Class By

Class By继承了java.lang.Object 类,用于获取页面元素对象 可以通过元素的类名、css选择器、id、元素名、标签名、文本内容、绝对路径 获取

例如By.id("note"),同过布局文件中的android:id="@+id/note"获取元素,这个id值会在R.java中注册

要注意的是:By.id("note")返回的是一个对象,其它方法也是返回WebElement对象,要通过父类的toString()返回对象的字符串表示形式。

Class By也提供了方法getValue(),这个方法是干嘛用的呢?

反编译robotium的jar查看源码,我们知道By本身是个抽象类,

By.id()方法其实是返回嵌套类Id的一个对象,要获取id值还要调用方法getValue()

嵌套类Id有一个getValue()方法,返回id

By.id("note").getValue() 返回String类型的id

 static class Id extends By  {

  private final String id;

  public String getValue()   {    return id;   }

  public Id(String id)   {    this.id = id;   }  }

By类自身也有个getValue(),不过返回空

public String getValue()  {   return "";  }

原文地址:https://www.cnblogs.com/zhitang2009/p/3424800.html