1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Capybaraで要素のattributeを取得する

Last updated at Posted at 2023-07-28

find('#hoge')などで取得できるCapybara::Node::ElementのインスタンスはCapybara::Node::Element['attribute']でattributeを取得できるらしい。

ex) idがhogeというdiv要素にスクロールを持たせており、1番下までスクロールしているか確認するテストの実装が必要な場合、下記のようになる。

# スクロールの最大値は(scrollHeight - clientHeight)で取得できる
expect(find('#hoge')['scrollTop']).to eq find('#hoge')['scrollHeight'] - find('#hoge')['clientHeight']

Capybaraでテストしているとattributeを取得したいケースが稀にあるので、Capybara::Node::Element['attribute']でattributeを取得できるのはかなり使い勝手が良いと思う。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?