Appium appium wd.js 的 sendKeys 输入的值并不会更新 react component 的 state,请问需要怎么处理?

Huang · 2018年08月20日 · 最后由 Huang 回复于 2018年08月21日 · 1118 次阅读

测试工具:Appium
测试 App:cordova + react.js
测试语言:JavaScript

共收到 1 条回复 时间 点赞

这是因为 iOS 的平台的 XCUITest 驱动程序没有很好地支持 react.js 引起的,需要在 react 启动页(html 页面),引入下面代码:

<script>
    /** This is a WORKAROUND to allow React <input> to be scriptable
     * from Appium in iOS.
     */
    document.addEventListener('blur', evt => {
      // we are only interested in <input>s
      if (evt.target.tagName !== 'INPUT' && evt.target.tagName !== 'TEXTAREA') {
        return;
      }

      // whenever a "blur" event is fired,
      // we will simulate an onChange event in React

      // fire "change" event in React 15 and before
      const event = new Event('change', { bubbles: true });
      event.simulated = true;

      // simulate change event in React 16
      const input = evt.target;
      const lastValue = input.value + 'HACK'; // this forces last value to be different than the current value, and forces the change event to fire
      const tracker = input._valueTracker;
      tracker && tracker.setValue(lastValue);

      input.dispatchEvent(event);
    }, true);
</script>

这样可以解决 sendKeys 没有更新 react component state 的情况,但是毕竟注入了多余 Script,如果有更好的方法欢迎留言评论。

Huang 关闭了讨论 08月21日 15:59
Huang 重新开启了讨论 08月21日 15:59
Huang 重新开启了讨论 08月21日 15:59
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册