Sha256: 4c13d02c4e50f7e6daddb4e9b17706abce240d8641ef8a13513801abd78456dc

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

module Polyblock
  module CapybaraHelpers
    def fill_in_polyblock(locator, params={})
      # Find out ckeditor id at runtime using its label
      # if page.has_css? "##{locator}"
      locator = locator.to_s if locator.is_a? Symbol
      if !locator.include?(' ') && page.evaluate_script("$('##{locator}').is('*')")
        # Locator is fine as is for capybara.
      # elsif page.has_css? 'label', text: locator
      elsif page.evaluate_script("$(\"label:contains('#{locator}')\").is('*')")
        locator = find('label', text: locator)[:for]
      elsif page.evaluate_script("$(\"label:contains('#{locator.titleize}')\").is('*')")
        locator = find('label', text: locator.titleize)[:for]
      elsif page.evaluate_script((
        sel = "$(\"label[for$='_#{locator.underscore}_attributes_content']\")"
                   ) + '.is("*")')
        locator = find('label', text: page.evaluate_script(sel+'.text()'))[:for]
      else
        raise "Locator #{locator} not found!!"
      end
      # Fill the editor content
      page.execute_script <<-SCRIPT
          var ckeditor = CKEDITOR.instances['#{locator}'];
          ckeditor.setData('#{params[:with].gsub("'", "\\\\'")}');
          ckeditor.focus();
          setTimeout(function(){
            ckeditor.updateElement();
          }, 1);
      SCRIPT
      sleep 0.1
    end

    def fill_in_inline_polyblock(name, params={})
      page.execute_script <<-SCRIPT
          var element = $('.polyblock[data-pbname="#{name}"]');
          var ckeditor = CKEDITOR.instances[element.attr('id')];
          setTimeout(function(){
            ckeditor.fire('focus');
            ckeditor.setData('#{params[:with].gsub("'", "\\\'")}');
            ckeditor.focus();
            ckeditor.updateElement();
          }, 1);
      SCRIPT
      sleep 0.1
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
polyblock-1.2.6 lib/polyblock/capybara_helpers.rb
polyblock-1.2.5 lib/polyblock/capybara_helpers.rb
polyblock-1.2.4 lib/polyblock/capybara_helpers.rb
polyblock-1.2.3 lib/polyblock/capybara_helpers.rb