2012-06-09 25 views

Trả lời

33

Lấy cảm hứng từ những gì tôi đã tìm thấy here, tôi đã đưa ra giải pháp sử dụng javascript để đặt cả dữ liệu trên ẩn textarea và đối tượng CKEditor. Dường như không đủ, tùy thuộc vào hoàn cảnh.

def fill_in_ckeditor(locator, opts) 
    content = opts.fetch(:with).to_json # convert to a safe javascript string 
    page.execute_script <<-SCRIPT 
    CKEDITOR.instances['#{locator}'].setData(#{content}); 
    $('textarea##{locator}').text(#{content}); 
    SCRIPT 
end 

# Example: 
fill_in_ckeditor 'email_body', :with => 'This is my message!' 
+1

Cảm ơn bạn vì điều này, nó thực sự giúp tôi tiết kiệm rất nhiều thời gian. Một chỉnh sửa nhỏ: trong capybara 2.1.0, 'browser.execute_script' ở trên phải là' page.execute_script'. –

+0

@BalintErdi: Đã sửa lỗi! –

+0

phương pháp tuyệt vời tại đây. làm việc như một sự quyến rũ ... chỉ cần sao chép và dán. – nfriend21

1

RSpec + Capybara hỗ trợ tập tin để làm việc với các trường hợp ckeditor

module Ckeditor 
    class Instance 
    attr_reader :capybara 
    private :capybara 

    def initialize(instance_id, capybara) 
     @instance_id, @capybara = instance_id, capybara 
    end 

    def val(value) 
     capybara.execute_script "jQuery('textarea##{@instance_id}').val('#{value}')" 
    end 

    def reload_all 
     capybara.execute_script "for(var instance in CKEDITOR.instances) { if(CKEDITOR.instances.hasOwnProperty(instance)) {CKEDITOR.instances[instance].destroy(true);} }" 
     capybara.execute_script "CKEDITOR.replaceAll();" 
    end 
    end 
end 

# usage 
# rte = Ckeditor::Instance.new(:my_instance_id, page) 
# rte.val 'foo' 
# rte.reload_all 
# NOTE: page is provided by Capybara 

https://gist.github.com/3308129

8

Một bổ sung nhỏ để Marc-André's awesome answer

Nếu bạn đang sử dụng hình thức lồng nhau hoặc nhiều textareas trong cùng ID được tạo trang khá xấu và khó viết vào các bài kiểm tra (ví dụ: person_translations_attributes_2_biography) với phần bổ sung nhỏ này để hi s phương pháp bạn có thể xác định vị trí ckeditors sử dụng nhãn của họ thay vì ID

# Used to fill ckeditor fields 
# @param [String] locator label text for the textarea or textarea id 
def fill_in_ckeditor(locator, params = {}) 
    # Find out ckeditor id at runtime using its label 
    locator = find('label', text: locator)[:for] if page.has_css?('label', text: locator) 
    # Fill the editor content 
    page.execute_script <<-SCRIPT 
     var ckeditor = CKEDITOR.instances.#{locator} 
     ckeditor.setData('#{params[:with]}') 
     ckeditor.focus() 
     ckeditor.updateElement() 
    SCRIPT 
end 

Bằng cách này thay vì điều này

fill_in_ckeditor 'person_translations_attributes_2_biography', with: 'Some text' 

bạn có thể viết này

fill_in_ckeditor 'Biography', with: 'Some text' 
+0

Tôi sắp viết một cái gì đó chính xác như thế này khi tôi đọc câu trả lời của Marc-André. Tôi rất vui vì đã tiếp tục đọc. Cảm ơn bạn! –

+0

nếu bạn sử dụng ckeditor.updateElement() để đặt giá trị của phần tử văn bản ẩn (thay vì đặt trực tiếp bằng jquery, như trong câu trả lời của Marc-André), bạn có thể gặp phải các kiểm tra thất bại ngẫu nhiên, khi capybara không đợi js của bạn để hoàn thành việc thực thi. khi bạn thêm trang.nên có_field (định vị, với: params [: with], visible: false) capybara đợi cho đến khi phần tử văn bản ẩn có giá trị mong đợi. – wnm

0

Đối với tôi Marc-André's answer chuyển bối cảnh khung nội tuyến trong trình điều khiển webkit. Xem this capybara-webkit issue

tôi tìm thấy một cách khác để điền vào đầu vào ckeditor mà không thay đổi bối cảnh iframe:

def fill_in_ckeditor(id, with:) 
    within_frame find("#cke_#{id} iframe") do 
     find('body').base.send_keys with 
    end 
    end 

và gọi nó là

fill_in_ckeditor 'comment', with: 'This is my message!'

trình cả với webkit và selen trình điều khiển

Lấy cảm hứng từ this post

Các vấn đề liên quan