Rspec + Capybara:使用xpath

时间:2016-03-23 15:22:15

标签: xpath rspec capybara

我有一个HTML文档(有一个ID为“countries”的DIV,它包含下面的代码),我需要 -

  1. 验证选择中的选项。我尝试了这个,但这是无效的。

      

    expect(page).to have_xpath(('// * [@ id =“countries”] // select')[1],   :options => ['US CAN GER POL'])

  2. 验证第二个选择是否已禁用

  3. 确认在第一个选择

  4. 中禁用了CAN
  5. 确认在第一个选择

  6. 中选择了POL
  7. 在第一个选择

    中将所选选项更改为GER   
      <li>
        <fieldset>
          <select>
            <option value="US">USA</option>
            <option value="CAN" disabled>Canada</option>
            <option value="GER">Germany</option>
            <option value="POL" selected>Poland</option>
          </select>
        <fieldset>
      <li>
      <li>
        <fieldset>
          <select disabled>
            <option value="US">USA</option>
            <option value="CAN">Canada</option>
            <option value="GER">Germany</option>
            <option value="POL">Poland</option>
          </select>
        <fieldset>
      <li>
      
       

      感谢您提供的任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

警告,我个人仍然使用rspec的should语法,我也使用css选择器与xpath。我觉得它们更容易阅读。

(1)因为你有两个下拉列表,没有任何特定的ID或类名来识别另一个,我会使用all来限制你期望的背景

all('#countries select')[0].should have_text('USA Canada Germany Poland')

(2)与上述相同的概念,限制范围。第二个字段集应包含禁用的下拉列表。

all('#countries fieldset')[1].should have_css('select[disabled]')

(3)all('#countries select')[0].should have_css('option[disabled]', :text => 'Canada')

(4)与#3相同,但具有不同的属性和文本

(5)all('#countries select')[0].find('option', :text => 'Germany').click