2016-10-17 51 views
0

我想多次調用一個dropzone水豚測試。但是,當我第二次打電話時,該ID已被使用。我試圖隨機化ID,以便它可以運行多次。重構水豚javascript dropzone測試

def drop_in_dropzone(file_path) 
    page.execute_script <<-JS 
    fakeFileInput = window.$('<input/>').attr(
     {id: 'fakeFileInput', type:'file'} 
    ).appendTo('body'); 
    JS 
    attach_file("fakeFileInput", file_path) 
    page.execute_script("var fileList = [fakeFileInput.get(0).files[0]]") 
    page.execute_script <<-JS 
    var e = jQuery.Event('drop', { dataTransfer : { files : [fakeFileInput.get(0).files[0]] } }); 
    $('.dropzone')[0].dropzone.listeners[0].events.drop(e); 
    JS 
end 

第二次調用時出錯。

Failure/Error: attach_file("fakeFileInput", file_path) 

Capybara::Ambiguous: 
Ambiguous match, found 2 elements matching file field "fakeFileInput" 

回答

0

你絕對可以只產生了輸入隨機ID號碼,但它可能會更容易僅僅只創造它,如果不存在的fakeFileInput。這隻有在你不使用輸入的情況下才能用於此方法以外的任何其他目的,但似乎這就是你正在做的。

page.execute_script <<-JS 
    fakeFileInput = fakeFileInput || window.$('<input/>').attr(
    {id: '#{fake_input_id}', type:'file'} 
).appendTo('body'); 
JS 

如果它已經存在,它不會再被創建,它會被重用。