javascript - DOM object not yet created (TypeError: undefined) with Capybara, PhantomJS test -


i have written javascript code when button clicked, following code (this part of method, there's code before , after line. video_id set somewhere above line).

var data_url = $('.brightcoveexperience').attr('data').replace(/(videoplayer=)[^\&]+/, '$1' + video_id); 

the code works when tested in browser. there's no console error.

however, automated cucumber test (capybara phantomjs) fails following error message

one or more errors raised in javascript code on page. if don't care these errors, can ignore them setting js_errors: false in poltergeist configuration (see documentation details).  typeerror: 'undefined' not object (evaluating '$('.brightcoveexperience').attr('data').replace')     

basically $('.brightcoveexperience').attr('data') returns undefined in automated test reason.

if rewrite code following, test pass.

var brightcove_url = $('.brightcoveexperience').attr('data') if (typeof brightcove_url !== 'undefined') {     brightcove_url = brightcove_url.replace(/(videoplayer=)[^\&]+/, '$1' + video_id); } 

i believe caused race condition. somehow in automated test, when click on button, in click event code, dom object $('.brightcoveexperience') not created yet. kind of delay or wait should introduced.

but how?

thanks

try check length of element like,

if($('.brightcoveexperience').length){     var brightcove_url = $('.brightcoveexperience').attr('data')     // code here } 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -