如何测试内容是否可见,而不是Capybara / Selenium中的滚动条隐藏

时间:2023-01-22 21:25:33

I have some code that updates what portion of a timeline is displayed at one time. So if you are looking at the time range from Jan 1st, 2013 thought Dec 31st, 2013, you have the ability to say I want all 12 months shown at once, or I only want 6 months shown (for example). If you choose the latter, then Jan - Jun are displayed and you must scroll the timeline right to view the remaining months.

我有一些代码可以更新一次显示时间轴的哪个部分。因此,如果您正在查看从2013年1月1日到2013年12月31日的时间范围,您可以说我想要一次显示所有12个月,或者我只想要显示6个月(例如)。如果选择后者,则会显示Jan - Jun,您必须向右滚动时间轴才能查看剩余月份。

I am attempting to test this with Selenium & Capybara. For each month, the corresponding abbreviation appears at the top of the timeline (Jan, Feb, etc.). For the case where all 12 months are displayed, I currently have this...

我试图用Selenium和Capybara测试这个。对于每个月,相应的缩写出现在时间轴的顶部(Jan,Feb等)。对于显示所有12个月的情况,我目前有...

expected = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

for exp in expected do
  assert page.has_content?(exp), "Expected page to have #{exp}, it did not."
end

For the case where you choose to only display 6 months at a time, I tried to do the following:

对于您选择一次只显示6个月的情况,我尝试执行以下操作:

expected = ['Jan','Feb','Mar','Apr','May','Jun']
for exp in expected do
  assert page.has_content?(exp), "Expected page to have #{exp}, it did not."
end

not_expected = ['Jul','Aug','Sep','Oct','Nov','Dec']
for exp in not_expected do
  assert !page.has_content?(exp), "Expected page NOT to have #{exp}, but it did."
end

The problem is that the not_expected months, are still in the content of the page, they are just not visible. I looked into using :visible => true but had trouble figuring out how to use it. Additionally, the content is not being set to not be visible... It is just off the page. Is there any way to test this with Capybara/Selenium?

问题是,not_expected月份仍然在页面的内容中,它们只是不可见。我调查使用:visible => true但是很难弄清楚如何使用它。此外,内容未设置为不可见...它只是在页面外。有没有办法用Capybara / Selenium测试这个?

1 个解决方案

#1


0  

Have you tried using the find command, instead of page.has_content?

您是否尝试过使用find命令而不是page.has_content?

You should be able to pass in the abbreviations you already use and Capybara should (by default) ignore anything it cannot see (for example, if it is off the screen).

您应该能够传入已经使用的缩写,并且Capybara应该(默认情况下)忽略它看不到的任何东西(例如,如果它不在屏幕上)。

There's also a setting you can use to ensure that Capybara ignores hidden elements, although I believe this is the default behaviour:

还有一个设置可以用来确保Capybara忽略隐藏的元素,虽然我相信这是默认行为:

Capybara.ignore_hidden_elements = true

As an aside, I'd avoid using !page.has_content? anywhere in your tests as Capybara will wait for your default timeout to be reached before returning anything. There's a page.has_no_content? method that does just that and will return as soon as the content is not there, saving you valuable time.

顺便说一句,我会避免使用!page.has_content?在测试中的任何地方,因为Capybara会在返回任何内容之前等待您的默认超时。有一个page.has_no_content?这样做的方法,只要内容不存在就会返回,节省宝贵的时间。

#1


0  

Have you tried using the find command, instead of page.has_content?

您是否尝试过使用find命令而不是page.has_content?

You should be able to pass in the abbreviations you already use and Capybara should (by default) ignore anything it cannot see (for example, if it is off the screen).

您应该能够传入已经使用的缩写,并且Capybara应该(默认情况下)忽略它看不到的任何东西(例如,如果它不在屏幕上)。

There's also a setting you can use to ensure that Capybara ignores hidden elements, although I believe this is the default behaviour:

还有一个设置可以用来确保Capybara忽略隐藏的元素,虽然我相信这是默认行为:

Capybara.ignore_hidden_elements = true

As an aside, I'd avoid using !page.has_content? anywhere in your tests as Capybara will wait for your default timeout to be reached before returning anything. There's a page.has_no_content? method that does just that and will return as soon as the content is not there, saving you valuable time.

顺便说一句,我会避免使用!page.has_content?在测试中的任何地方,因为Capybara会在返回任何内容之前等待您的默认超时。有一个page.has_no_content?这样做的方法,只要内容不存在就会返回,节省宝贵的时间。