我正在点击隐藏在其顶部的隐藏元素。我试过点击两次,但这不起作用。我没有得到任何错误,但它只是不起作用。有人可以帮忙吗?
错误:
# Running tests:
#<Watir::LI:0x007fb213b01ec8>
(0)
F
Finished tests in 11.174010s, 0.0895 tests/s, 0.1790 assertions/s.
1) Failure:
Tests#test_debug [...Cucumber/features/debug. rb:27]:
Failed assertion, no message given.
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
[Finished in 12.3s with exit code 1]
违规的HTML:
<li title="Optic White">
<img id="swatch456180#7_0" class="colorSwatch"style="background:url('http://slimages.macys.com/is/image/MY/&$b=MCY/swatches/&layer=0&size=247,19&src=is{$b$0/optimized/351640_fpx.tif}&cropN=0,0,13,1&anchor=0,0&layer=1&size=19,19&src=is{$b$9/optimized/1200269_fpx.tif}&anchor=0,0&posN=0.077,0&layer=2&size=19,19&src=is{$b$0/optimized/1200270_fpx.tif}&anchor=0,0&posN=0.154,0&layer=3&size=19,19&src=is{$b$4/optimized/1611224_fpx.tif}&anchor=0,0&posN=0.231,0&layer=4&size=19,19&src=is{$b$2/optimized/1227542_fpx.tif}&anchor=0,0&posN=0.308,0&layer=5&size=19,19&src=is{$b$3/optimized/740793_fpx.tif}&anchor=0,0&posN=0.385,0&layer=6&size=19,19&src=is{$b$2/optimized/740792_fpx.tif}&anchor=0,0&posN=0.462,0&layer=7&size=19,19&src=is{$b$0/optimized/1016310_fpx.tif}&anchor=0,0&posN=0.538,0&layer=8&size=19,19&src=is{$b$9/optimized/1014329_fpx.tif}&anchor=0,0&posN=0.615,0&layer=9&size=19,19&src=is{$b$4/optimized/740794_fpx.tif}&anchor=0,0&posN=0.692,0&layer=10&size=19,19&src=is{$b$8/optimized/740798_fpx.tif}&anchor=0,0&posN=0.769,0&layer=11&size=19,19&src=is{$b$7/optimized/1014327_fpx.tif}&anchor=0,0&posN=0.846,0&layer=12&size=19,19&src=is{$b$1/optimized/1200271_fpx.tif}&anchor=0,0&posN=0.923,0&layer=13&op_sharpen=1&fmt=jpeg&qlt=90,0&hei=19') 133px 0 transparent;" src="http://assets.macys.com/navapp/img/spacer.gif" title="Optic White" alt="Optic White">
<input type="hidden" id="swatch456180#7_0_imgurl"value="2/optimized/740792_fpx.tif">
</li>
<img id="swatch456180#7_0" class="colorSwatch" style="background:url('http://slimages.macys.com/is/image/MCY/?&$b=MCY/swatches/&layer=0&size=247,19&src=is{$b$0/optimized/351640_fpx.tif}&cropN=0,0,13,1&anchor=0,0&layer=1&size=19,19&src=is{$b$9/optimized/1200269_fpx.tif}&anchor=0,0&posN=0.077,0&layer=2&size=19,19&src=is{$b$0/optimized/1200270_fpx.tif}&anchor=0,0&posN=0.154,0&layer=3&size=19,19&src=is{$b$4/optimized/1611224_fpx.tif}&anchor=0,0&posN=0.231,0&layer=4&size=19,19&src=is{$b$2/optimized/1227542_fpx.tif}&anchor=0,0&posN=0.308,0&layer=5&size=19,19&src=is{$b$3/optimized/740793_fpx.tif}&anchor=0,0&posN=0.385,0&layer=6&size=19,19&src=is{$b$2/optimized/740792_fpx.tif}&anchor=0,0&posN=0.462,0&layer=7&size=19,19&src=is{$b$0/optimized/1016310_fpx.tif}&anchor=0,0&posN=0.538,0&layer=8&size=19,19&src=is{$b$9/optimized/1014329_fpx.tif}&anchor=0,0&posN=0.615,0&layer=9&size=19,19&src=is{$b$4/optimized/740794_fpx.tif}&anchor=0,0&posN=0.692,0&layer=10&size=19,19&src=is{$b$8/optimized/740798_fpx.tif}&anchor=0,0&posN=0.769,0&layer=11&size=19,19&src=is{$b$7/optimized/1014327_fpx.tif}&anchor=0,0&posN=0.846,0&layer=12&size=19,19&src=is{$b$1/optimized/1200271_fpx.tif}&anchor=0,0&posN=0.923,0&layer=13&op_sharpen=1&fmt=jpeg&qlt=90,0&hei=19') 133px 0 transparent;" src="http://assets.macys.com/navapp/img/spacer.gif" title="Optic White" alt="Optic White">
测试:
def test_debug
before_items = @browser.span(:id => "itemCount")
assert before_items.text == "(0)"
@browser.goto "http://www1.macys.com/shop/product/hotel-collection-microcotton-luxe-bath-towel-collection?ID=456180&CategoryID=16853#fn=sp%3D1%26spc%3D205%26kws%3Dtowel%26slotId%3D1"
@browser.li(:xpath => "//*[@id=\"colorList457102\"]/li[3]").click
@browser.li(:title => "Optic White").click
bagbutton = @browser.image(:class => "addToBagButton")
puts bagbutton
after_items = @browser.span(:id => "itemCount")
puts after_items.text
assert after_items.text == "(1)"
end
答案 0 :(得分:1)
选择颜色
要选择颜色,点击需要针对图像元素而不是父列表项。
例如,而不是:
@browser.li(:xpath => "//*[@id=\"colorList457102\"]/li[3]").click
你需要这样做:
@browser.li(:xpath => "//*[@id=\"colorList457102\"]/li[3]").image.click
在找到颜色按钮时,您可能需要进行一些改进(参见下一节)。
点击添加按钮
您的断言也会失败,因为没有点击添加包按钮。为确保您单击用于色板颜色选择的相同添加按钮,我认为您应该在代表产品的父元素中工作。
结合2个想法,添加项目的脚本部分将是:
# The list of products,
# where each product is contained within a div of class memberProducts
product_list = @browser.div(:id => 'memberProductList')
# Lets assume we want the first product (the product of interest)
product = product_list.div(:class => 'memberProducts')
# Select the colour based on title (for the product of interest)
product.li(:title => 'Optic White').image.click
# Click the add to bag button (for the product of interest)
product.image(:id => /addToBagButton/).click
# Wait for the add to cart popup to be displayed (ie page finishes processing request)
@browser.div(:id => 'pdpMasterAddToBagPanel').wait_until_present