2016-02-22 135 views
0

我正在做一些AngularJS应用程序的测试,我不认为测试框架在这里一定很重要,但我想通过一个可选变量。这个变量将指定如果我想要抓住传递给函数的ID的子元素通过JavaScript元素作为变量

commonFunctions.elementDoesNotHaveAttribute('register-0', 'disabled', 'children[0].children[0]'); 

children[0].children[0]是不是一个真正的字符串,如果我删除引号函数调用失败,因为它找不到.children[0]

这里的子对象是功能

function elementDoesNotHaveAttribute(id, attribute, child) { 
    var hasElement = false; 

    hasElement = casper.evaluate(function (id, attribute, child) { 
     var element = document.getElementById(id); 
     if (child) { 
      element = element.child; 
     }   
     return element.hasAttribute(attribute); 
    }, id, attribute, child); 

    if (hasElement) { 
     casper.test.fail('element has' + attribute + ' but shouldn\'t'); 
    } else { 
     casper.test.pass('element didn\'t have ' + attribute + ' and shouldn\'t'); 
    } 
} 

我怎样才能改变这些等式的两边来结束我的函数中评估element.children[0].children[0]

+0

工作,我过去没有使用eval() –

+0

@ T.J.Crowder,它采用一个ID,属性和可选的子元素。它正在检查该选择器是否为通过的属性 –

回答

2

虽然我在评论中发布的“eval hack”会起作用,但这不是一个好主意。相反,考虑通过某种“转换”功能。

if(child) { 
    element = child(element); 
} 

当调用,比如说,children[0].children[0],你可以这样做:

commonFunctions.elementDoesNotHaveAttribute(
    'register-0', 
    'disabled', 
    function(e) {return e.children[0].children[0];} 
); 

功能将采取元素并返回别的东西与它有关。根据需要调整功能。

+0

好的解决方案,并且很好地理解了这个问题。 :-) –

+0

很好,谢谢你的提示。我喜欢解决方案 –

+0

Niet,我建议清理问题的评论。 (*这个*评论将自毁于五...四...三...) –