【问题标题】:Testing upload watir and rspec测试上传 watir 和 rspec
【发布时间】:2016-02-05 22:01:32
【问题描述】:

我正在尝试使用 watir/watir-webdriver 测试在网页上上传图片/文档,但我尝试的所有方法都不起作用。

例如在Imgur上上传图片:

require 'watir'
require 'watir-webdriver'
require 'rspec'

describe "Upload test" do

  before(:all) do 
      @browser = Watir::Browser.new :firefox
      @browser.goto("https://imgur.com/")
  end

  context "Upload test" do
    it "can upload picture" do
      @browser.link(:text, "upload images").click
      @browser.file_field(:name, "img_path").set("img_path")
      @browser.button(:value,"Save").click
    end
  end

  after(:all) do
    @browser.close unless debugging?
  end

end

有一个错误。

Failures:

  1) Upload test Upload test can upload picture
     Failure/Error: @browser.link(:text, "upload images").click

     Watir::Exception::UnknownObjectException:
       unable to locate element, using {:text=>"upload images", :tag_name=>"a"}
     # /var/lib/gems/1.9.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:536:in `assert_element_found'
     # /var/lib/gems/1.9.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:508:in `assert_exists'
     # /var/lib/gems/1.9.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:114:in `click'
     # ./PROBA.rb:14:in `block (3 levels) in <top (required)>'

Finished in 11.47 seconds (files took 0.19261 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./PROBA.rb:13 # Upload test Upload test can upload picture

我曾尝试将其上传到其他网站,但我不能。任何人都可以告诉我使用 watir 上传文件的正确方法吗?我认为使用“flash”按钮有一些技巧,但我不确定?

【问题讨论】:

  • 会发生什么?如果抛出错误,请包含堆栈跟踪。此外,链接到 file_field 方法的 set 方法将目录路径(到图像)作为参数。
  • 我现在已经包含了错误堆栈跟踪..
  • 没有“上传图片”文字的链接。有一个跨度,虽然:&lt;span class="upload-btn-text"&gt;upload images&lt;/span&gt; 和一个带有初始大写文本的链接:&lt;a class="creation-dropdown-upload" href="javascript:;"&gt;Upload Images&lt;/a&gt;

标签: ruby rspec watir watir-webdriver


【解决方案1】:

该错误是说它在页面上没有看到带有“上传图片”文本的链接,这与文件上传无关。 话虽如此,如果文本字段已经可见,您应该这样做: @browser.file_field(name: 'img_path').set @file_path

【讨论】:

    【解决方案2】:

    您需要确保您的定位器准确无误。在您的示例中,您定位的文本不存在(即没有“上传图片”文本的链接)。

    require 'watir-webdriver'
    b = Watir::Browser.new 
    b.goto('http://imgur.com/')
    
    b.span(text: "upload images").click
    
    # alternately:
    # b.div(class: "selection icon icon-arrow-down").click
    # b.link(text: "Upload Images").click
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 2015-03-28
      • 2011-11-07
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多