1

实际的 HTML 代码如下,我不知道如何让 watir-webdriver 上传文件表单创建的数组。到目前为止,脚本正在访问上传表单并停止。

HTML 上传表单代码:

<form id="fileupload" enctype="multipart/form-data" method="POST" action="/projects/155/documents/upload">
<p> … </p><table role="presentation"> … </table><span id="add-files" class="btn" style="">
          Add file(s)...
        </span><input class="hidden" type="file" multiple="" name="files[]"></input>
<input class="btn" type="submit" value="Start upload" style="display: none;"></input>
<a class="btn" href="/projects/155"> … </a></form>

到目前为止的 Ruby 脚本文件:

require "rubygems"
require "watir-webdriver"
require "cucumber/formatter/unicode"
require "ruby-progressbar"

@b = Watir::Browser.new :chrome, :switches => %w[--disable-plugins --ignore-certificate-errors --disable-popup-blocking --disable-translate --disable-translate]

require_relative "1_login.rb"

@b.div(:class, "main-content-container").link(:text, "TestProject").click
@b.div(:id, "documentsContainer").div(:class, "float-right").link(:class, "btn").click


kolekcija = []
current_dir = "C:/Users/Edgars/Desktop/RubyScripts"
Dir.chdir("C:/Users/Edgars/Desktop/RubyScripts/Testfiles")
#dirname = "C:/Users/edgars.karkis/Desktop/AUTOTESTI/letsmt_UserTest_automation" # <-- JĀMAINA UZ DIREKTORIJU, KUR ATRODAS SKRIPTS

Dir.glob( (current_dir + File.join("DOC","*.*")).force_encoding("UTF-8") ).each { |f| kolekcija.store(f, "doc|docx") }
Dir.glob( (current_dir + File.join("DOCX","*.*")).force_encoding("UTF-8") ).each { |f| kolekcija.store(f, "dox|docx") }
Dir.glob( (current_dir + File.join("HTM","*.*")).force_encoding("UTF-8")).each { |f| kolekcija.store(f, "htm") }

puts ""
puts "Tiek veikta atlasīto failu ielāde..."
progress = ProgressBar.create(:title => "Failu ielāde:", :progress_mark => "|", :format => "%t [%B] %p%%", :total => kolekcija.length)

kolekcija.each do |file|
@file = files[0]
    @b.form(:id, "fileupload").span(:id, "add-files").when_present.click
    @b.form(:id, "fileupload").frame(:index, 0).file_field(:name, "files[]").set(@file) # File absolute path
    @b.form(:id, "fileupload").frame(:index, 0).button(:class, "btn").click
    sleep 5
    progress.increment
end

是的,我正在尝试让脚本从不同的目录上传多个存储在数组中的文件。

该脚本没有上传任何内容。我已经设法让它打开上传窗口并且不选择文件夹和文件名。

C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir-webdriver/elements/file_field.rb:13:in `set': No such file or directory -  (Errno::ENOENT)
        from test.rb:21:in `<main>'

不带数组但仅用于单个文件上传的代码:

require "rubygems"
require "watir-webdriver"
require "cucumber/formatter/unicode"
require "ruby-progressbar"

@b = Watir::Browser.new :chrome, :switches => %w[--disable-plugins --ignore-certificate-errors --disable-popup-blocking --disable-translate --disable-translate]

require_relative "1_login.rb"

@b.div(:class, "main-content-container").link(:text, "TestProject").click
#@b.div(:class, "btn").link(:text, "Add file(s)").click
@b.div(:id, "documentsContainer").div(:class, "float-right").link(:class, "btn").click

current_dir = "C:\Users\edgars.karkis\Desktop\TaaS\Automatization"
Dir.chdir("\\\\projekti\\testapps\\AUTOTESTS\\TaaS\\TaaS_testa_kopa\\DOC")

fails = "file1.en.doc"

@b.form(:id, "fileupload").span(:id, "add-files").when_present.click
@b.form(:id, "fileupload").frame(:index, 0).file_field(:name, "files[]").set("#{@fails}")
@b.form(:id, "fileupload").frame(:index, 0).span(:class, "btn").click

单个文件上传尝试:

# encoding: utf-8
require "rubygems"
require "watir-webdriver"
require "cucumber/formatter/unicode"
require "ruby-progressbar"

@b = Watir::Browser.new :chrome, :switches => %w[--disable-plugins --ignore-certificate-errors --disable-popup-blocking --disable-translate --disable-translate]

require_relative "1_login.rb"

@b.div(:class, "main-content-container").link(:text, "TestProject").click
#@b.div(:class, "btn").link(:text, "Add file(s)").click
@b.div(:id, "documentsContainer").div(:class, "float-right").link(:class, "btn").click

current_dir = "C:\Users\edgars.karkis\Desktop\TaaS\Automatization"
Dir.chdir("\\\\projekti\\testapps\\AUTOTESTS\\TaaS\\TaaS_testa_kopa\\DOC")

fails = "file1.en.doc"

@b.form(:id, "fileupload").span(:id, "add-files").when_present.click
@b.form(:id, "fileupload").frame(:index, 0).file_field(:name, "files[]").set("#{@fails}")
@b.form(:id, "fileupload").frame(:index, 0).span(:class, "btn").click
4

0 回答 0