textToBePresentInElementValue等待文本存在且jasmine超时

时间:2017-06-27 18:44:57

标签: protractor

我有一种情况,我等待元素文本从运行变为成功,但我可以看到我的测试运行时,即使文本显示为成功,它仍在等待,然后发生茉莉花超时错误。

我不知道如何解决这个问题 PropertyPage:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            for(View view : getWindowManagerViews()){
                try {
                    Class clazz = view.getClass();
                    Field outerField = clazz.getDeclaredField("this$0");
                    outerField.setAccessible(true);
                    Object popupWindow = outerField.get(view);

                    Field field = popupWindow.getClass().getDeclaredField("mTouchInterceptor");
                    field.setAccessible(true);
                    final View.OnTouchListener innerOnTouchListener = (View.OnTouchListener) field.get(popupWindow);
                    View.OnTouchListener outerOnTOuchListener = new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.d(MainActivity.class.getSimpleName(), String.format("popupwindow event %s at %s-%s", event.getAction(), event.getX(), event.getY()));
                            return innerOnTouchListener.onTouch(v, event);
                        }
                    };
                    field.set(popupWindow, outerOnTOuchListener);
                }catch (Exception e){
                    //e.printStackTrace();
                }
            }
        }
    });
}

测试用例:

public static List<View> getWindowManagerViews() {
    try {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
                Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {

            // get the list from WindowManagerImpl.mViews
            Class wmiClass = Class.forName("android.view.WindowManagerImpl");
            Object wmiInstance = wmiClass.getMethod("getDefault").invoke(null);

            return viewsFromWM(wmiClass, wmiInstance);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {

            // get the list from WindowManagerGlobal.mViews
            Class wmgClass = Class.forName("android.view.WindowManagerGlobal");
            Object wmgInstance = wmgClass.getMethod("getInstance").invoke(null);

            return viewsFromWM(wmgClass, wmgInstance);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return new ArrayList<View>();
}

private static List<View> viewsFromWM(Class wmClass, Object wmInstance) throws Exception {

    Field viewsField = wmClass.getDeclaredField("mViews");
    viewsField.setAccessible(true);
    Object views = viewsField.get(wmInstance);

    if (views instanceof List) {
        return (List<View>) viewsField.get(wmInstance);
    } else if (views instanceof View[]) {
        return Arrays.asList((View[])viewsField.get(wmInstance));
    }

    return new ArrayList<View>();
}

2 个答案:

答案 0 :(得分:1)

textToBePresentInElement textToBePresentInElementValue

之间的区别
  • 第一个方法是检查元素('#abc')中文本的值
  • 第二个是检查元素文本字段('#myInput')中文本的输入值

当我使用 textToBePresentInElementValue 来获取元素的文本时,我遇到相同的错误无法读取null的属性'indexOf'

ExpectedConditions.textToBePresentInElement

var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to contain the text 'foo'.
browser.wait(EC.textToBePresentInElement($('#abc'), 'foo'), 5000);

ExpectedConditions.textToBePresentInElementValue

var EC = protractor.ExpectedConditions;
// Waits for the element with id 'myInput' to contain the input 'foo'.
browser.wait(EC.textToBePresentInElementValue($('#myInput'), 'foo'), 5000);

答案 1 :(得分:0)

如果您之前没有设置变量但是等待中的整个元素,是否会强制再次获取该值?

browser.wait(EC.textToBePresentInElementValue(element.all(by.repeater("report in reportList")).all(by.css('div.display-inherit')), 'Successful'));

另外,您的定位器“div.display-inherit”是否在页面上查找单个元素div.display-inherit?你必须确保找到准确的一个来查看它。你能用另一个定位器得到吗?