【问题标题】:Unable to click the rating image via capybara using selenium-cucumber无法使用 selenium-cucumber 通过 capybara 单击评级图像
【发布时间】:2018-10-28 17:56:04
【问题描述】:

这是下面的 html 代码。当鼠标悬停在星星图像上时,图像源将变为 star-on.png。如果您看到以下代码,则表示我给了两星评级。

<div id="user_rating" class="rating" style="cursor: pointer; width: 100px;">
<img src="/assets/jquery.raty/star-on.png" alt="1" title="">&nbsp;
<img src="/assets/jquery.raty/star-on.png" alt="2" title="">&nbsp;
<img src="/assets/jquery.raty/star-off.png" alt="3" title="">&nbsp;
<img src="/assets/jquery.raty/star-off.png" alt="4" title="">&nbsp;
<img src="/assets/jquery.raty/star-off.png" alt="5" title="">
<input type="hidden" name="score"></div>

我在黄瓜中使用 img src、all 和 values 尝试了以下代码,但我无法单击评级图像

img_src 作为“/assets/jquery.raty/star-off.png”传递

Then /^I click on the image with src "([^\"]*)"$/ do |img_src|
   find("img[src=\'#{img_src}\']", match: :prefer_exact).click
end

alt_text 作为“3”传递

Then /^I click on the image with alt "([^\"]*)"$/ do |alt_text|
  page.execute_script %Q[jQuery("img[alt=\'{alt_text}\']").first().mouseenter();]
  page.execute_script %Q[jQuery("img[alt=\'#{alt_text}\']").first().click();]
end

选择器 = 正确的 xpath 已通过

And /^I click by xpath "([^\"]*)"$/ do |selector|
  find(:xpath, selector, visible: true, match: :first).click
end

谁能告诉我一个正确的硒命令来点击评级

【问题讨论】:

  • 怎么知道没有点击?你处理javascript中的点击吗?您能否发布您的方案,以便我们接听您的电话?
  • 我运行了功能文件,并且该功能在下一步中失败,因为没有给出评级,页面没有提交。是的 Javascript 用于初始化评级
  • 你能把功能文件中的场景贴出来吗?

标签: ruby selenium testing cucumber capybara


【解决方案1】:

为了点击你需要做的特定星星

find("#user_rating img[alt='2']").hover.click # select star 2

所以在黄瓜步骤中,就像

Then /^I rate ([^ ]+) with (\d) stars$/ do |id, stars|
  find("##{id} img[alt='#{stars}']").hover.click
end

并被称为

Then I rate user_rating with 4 stars

【讨论】:

    【解决方案2】:

    尽量不要在前两个解决方案中转义单引号:\' =>'。这行得通吗?

    您的第二个解决方案中的 javascript 应该更改:

    page.execute_script %Q[jQuery("img[alt=\'#{alt_text}\']").first().trigger('click');]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 2011-02-17
      • 1970-01-01
      • 2020-08-10
      • 2011-05-27
      • 1970-01-01
      相关资源
      最近更新 更多