How can I check that a form field is prefilled correctly using capybara?

后端 未结 5 1544
不知归路
不知归路 2021-01-30 01:48

I have a field with a proper label that I can fill in with capybara without a problem:

fill_in \'Your name\', with: \'John\'

I\'d like to check

5条回答
  •  臣服心动
    2021-01-30 02:42

    You can use an xpath query to check if there's an input element with a particular value (e.g. 'John'):

    expect(page).to have_xpath("//input[@value='John']")
    

    See http://www.w3schools.com/xpath/xpath_syntax.asp for more info.

    For perhaps a prettier way:

    expect(find_field('Your name').value).to eq 'John'
    

    EDIT: Nowadays I'd probably use have_selector

    expect(page).to have_selector("input[value='John']")
    

    If you are using the page object pattern(you should be!)

    class MyPage < SitePrism::Page
      element :my_field, "input#my_id"
    
      def has_secret_value?(value)
        my_field.value == value
      end
    end
    
    my_page = MyPage.new
    
    expect(my_page).to have_secret_value "foo"
    

提交回复
热议问题