ruby - 无法使用 Selenium cucumber 通过 capybara 点击评级图像

标签 ruby selenium testing cucumber capybara

这是下面的 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 在 cucumber 中尝试了以下代码,但我无法单击评级图像

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

selector = 正确的 xpath 被传递

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

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

最佳答案

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

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

所以在 cucumber 步骤中会是这样的

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

关于ruby - 无法使用 Selenium cucumber 通过 capybara 点击评级图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50408815/

相关文章:

java - 将testNG xml文件放在ANT java web项目中的什么地方

ruby-on-rails - 如何在 Ruby on rails 中按余额对项目进行排名

ruby - Sidekiq Mechanize 覆盖实例

python - 如何在python中使用selenium查找html类中是否存在元素

perl - 如何模拟 Perl 模块 Time::HiRes

javascript - 应返回的数字是数组中的第一个或最后一个

ruby-on-rails - 三个模型之间的关联

ruby-on-rails - RSpec 异常后继续

java - 如何在 Selenium 中定位具有特定文本的跨度? (使用Java)

java - 如何在testng中实现设置属性