2016-02-22 62 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,我建議清理問題的評論。 (*這個*評論將自毀於五...四...三...) –