当前位置: 首页 > 工具软件 > WatiJ > 使用案例 >

如何用Watij判断一个输入字段是在UI上是只读的字段

寇甫
2023-12-01

首先看一下封装后的为字段设置的api如下所示,

  public void Set_TextField( String label, String value ) throws Exception
  {
    String xpath = "//*[(contains(@class, 'labelCol') or contains(@class, 'x-form-item-label') or contains(@class, 'fieldLabel')) and (normalize-space()='" + label + "' or normalize-space()='" + label + ":')]" +
      "/following::*[(local-name() = 'INPUT' or local-name() = 'TEXTAREA') and (not(@type) or @type != 'hidden')]";
    this.WaitFor_XpathWithinTime( xpath );
    
    //if popup exist, fill value in popup only
    if (this.isElementExistOnPageByXpathHelper( TestText.pltx( "//DIV[contains(@class, 'x-window') and contains(@style, 'visible')]" ) )) {
      xpath = "//DIV[contains(@class, 'x-window') and contains(@style, 'visible')]/descendant::" + xpath.substring(2, xpath.length());
    }
    
    TextField _txtField = _browser.getIE().textField( FinderFactory.xpath( xpath ) );
    _txtField.focus();
    _txtField.clear();
    _txtField.set( value );
  }

在方法的最后是为一个输入字段设置值,但是当这个字段是一个只读的字段时这个方法就会调用失败。但是它会抛出一个为ObjectReadOnlyException的异常。因此在脚本调用该api的时候做了如下的try..catch 处理后就可以判断该输入字段是一个只读的字段了。


    try

    {

      ui.Set_TextField(firstWorkedHours.toString(),“10.5”);

    }

    catch (Exception e)

    {

      Assert.assertEquals(true, e instanceofObjectReadOnlyException);

    }


 类似资料: