Sha256: 073ae3e60668feac32dedccfb7892f5a9b48d2b27e8caa4e6ff6e29969fb62c7

Contents?: true

Size: 1.88 KB

Versions: 26

Compression:

Stored size: 1.88 KB

Contents

module TestCentricity
  module Elements
    class FileField < UIElement
      def initialize(name, parent, locator, context)
        super
        @type = :filefield
      end

      def file_upload(file_path)
        obj, = find_element(visible = false)
        obj.send_keys(file_path)
      end

      # :nocov:

      def file_attach(file_path)
        Capybara.ignore_hidden_elements = false
        page.attach_file(@locator, file_path)
        Capybara.ignore_hidden_elements = true
      end

      def drop_files(files)
        js_script = 'fileList = Array();'
        files.count.times do |i|
          # generate a fake input selector
          page.execute_script("if ($('#seleniumUpload#{i}').length == 0) { seleniumUpload#{i} = window.$('<input/>').attr({id: 'seleniumUpload#{i}', type:'file'}).appendTo('body'); }")
          # attach file to the fake input selector through Capybara
          attach_file("seleniumUpload#{i}", files[i])
          # create the fake js event
          js_script = "#{js_script} fileList.push(seleniumUpload#{i}.get(0).files[0]);"
        end
        # trigger the fake drop event
        page.execute_script("#{js_script} e = $.Event('drop'); e.originalEvent = {dataTransfer : { files : fileList } }; $('#{@locator}').trigger(e);")
      end

      def ng_drop_file(file_path)
        # generate a fake input selector
        page.execute_script("fakeFileInput = window.$('<input/>').attr({ id: 'fileFileInput', type: 'file' }).appendTo('body');")
        # attach file to the fake input selector through Capybara
        page.attach_file('fakeFileInput', file_path)
        # create the fake js event
        js_script = "var scope = angular.element('#{@locator}').scope();"
        js_script = "#{js_script} scope.files = [fakeFileInput.get(0).files[0]];"
        # trigger the fake drop event
        page.execute_script(js_script)
      end

      # :nocov:
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
testcentricity_web-4.6.0 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.15 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.14 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.13 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.12 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.11 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.10 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.9.1 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.9 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.7 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.6 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.5 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.4 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.3 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.2 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.1 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.5.0 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.4.7 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.4.6 lib/testcentricity_web/web_elements/file_field.rb
testcentricity_web-4.4.5 lib/testcentricity_web/web_elements/file_field.rb