简体   繁体   English

如何测试是否已将类添加到Rspec中的nav标签中

[英]How to test if class has been added to nav tag in Rspec

When a user scrolls down on a webpage I have, a class gets added to the nav tag and it changes positions on the page. 当用户向下滚动我拥有的网页时,将一个类添加到nav标签中,并且该类会更改页面上的位置。 When the user scrolls back to the top, the class is removed and it moves back to the original position. 当用户滚动回到顶部时,该类将被删除,然后将其移回到原始位置。 I know this feature works and the class when I inspect the element in a browser. 当我在浏览器中检查元素时,我知道此功能可以正常使用,并且可以使用类。 I'm trying to write an Rspec test to test this feature. 我正在尝试编写Rspec测试来测试此功能。 I've been trying to use Capybara without success. 我一直在尝试使用水豚,但没有成功。 The scrolling part of the test works but searching the HTML and CSS for the added class isn't. 该测试的滚动部分有效,但无法在HTML和CSS中搜索添加的类。

The nav id is "views" and the class being added is "on_nav". 导航ID为“视图”,添加的类为“ on_nav”。 This is the test so far: 到目前为止,这是测试:

  scenario 'Scroll down' do
    visit '/'
    page.execute_script "window.scrollBy(0,1000)"
    expect(page.html).to include('class="on_nav"')
  end

The error message is that it cannot find 'class=on_nav"' on the page, even though when I inspect the element in the browser I can see it. These are a few of the different random commands I've tried, from answers I've looked at online when trying to Google this, that all give me the same error or they say my syntax is wrong and I can't find how to fix it: 错误消息是,即使我在浏览器中检查元素也能看到它,但在页面上找不到“ class = on_nav”,这是我尝试过的几种随机命令,来自答案我在尝试使用Google时已经在网上查看过,这些都给了我相同的错误,或者他们说我的语法错误,而且我找不到解决方法:

    expect(page.html).to have_selector("#on_nav")
    expect(page).to have_css("nav#views.on_nav")
    expect(page.has_css?(".on_nav")).to eq(true)

I am completely new to writing web tests, but I do know the answers I have found online (for example this question about checking the CSS and this article about testing elements with Capybara) haven't worked for me. 我完全新的编写Web测试,但我知道我已经找到答案在线(例如这个问题有关检查CSS和这篇文章关于测试元素与水豚)都没有为我工作。 It might be giving me problems because I'm trying to test the nav tag whereas all the examples I've found online talk about either div or input? 这可能给我带来了问题,因为我正在尝试测试nav标签,而我在网上找到的所有示例都在谈论div或输入? Is it even possible? 可能吗?

Doing expect(page.html).to include('class="on_nav"') should never be done, and you should ignore everything from any article/tutorial that suggested that. 不要expect(page.html).to include('class="on_nav"') ,并且您应该忽略任何建议这样做的文章/教程中的所有内容。 By getting the page source as a string you are completely disabling Capybaras ability to wait/retry for the given condition to be met. 通过将页面源作为字符串获取,您将完全禁用Capybaras等待/重试满足给定条件的功能。

With regards to your other attempts 关于您的其他尝试

By default the selector type is :css, so expect(page.html).to have_selector("#on_nav") would look for an element with an id of 'on_nav' which isn't what you want, and by calling page.html you've again disabled waiting/retrying behavior since the string gets parsed back into a static document. 默认情况下,选择器类型为:css,因此expect(page.html).to have_selector("#on_nav")会查找ID为“ on_nav”(不是您想要的元素expect(page.html).to have_selector("#on_nav")的元素,并调用page.html由于字符串被解析回静态文档,因此您再次禁用了等待/重试行为。

expect(page.has_css?(".on_nav")).to eq(true) is getting closer to what you want but will not provide useful error messages since your expectation is just for true or false expect(page.has_css?(".on_nav")).to eq(true)距离越来越近,但由于您的期望只是对还是错,因此不会提供有用的错误消息

expect(page).to have_css("nav#views.on_nav") is the correct way to verify an elements existence on the page. expect(page).to have_css("nav#views.on_nav")是验证页面上元素是否存在的正确方法。 It will look for a visible <nav> element with an id of views and a class of on_nav and wait/retry up to Capybara.default_max_wait_time seconds for that element to exist. 它将查找具有views ID和on_nav类的可见<nav>元素,并等待/重试on_nav秒以使该元素存在。 If that isn't working for you then either the element isn't visible on the page, the selector doesn't actually match the element, or your JS that's adding/removing the class isn't working when you call scrollBy. 如果这对您不起作用,则该元素在页面上不可见,选择器实际上与该元素不匹配,或者当您调用scrollBy时添加/删除类的JS不起作用。 If you're using selenium, pause the driver after calling scrollBy and inspect the element in the browser to ensure it's adding/removing the class as expected, and if it is then add the actual HTML of the nav to your question. 如果您使用的是硒,请在调用scrollBy之后暂停驱动程序,并在浏览器中检查该元素,以确保它按预期添加/删除了该类,如果已添加,则将导航的实际HTML添加到您的问题中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM