使用Capybara / RSpec测试链接的target属性 [英] Testing a link's target attribute with Capybara/RSpec

查看:83
本文介绍了使用Capybara / RSpec测试链接的target属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rails / RSpec / Capybara有点陌生,所以这可能是一个新手问题,但是Google并没有提供我所希望的帮助。我希望可以使用

I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but Google didn't help as much as I would have hoped. I was hoping that I could use

it { should have_link('link text', href: 'url', target: '_blank') }

为应该在新窗口中打开的链接编写测试,但是似乎不起作用。我还尝试过将选项哈希用大括号括起来:

to write a test for a link that should open in a new window, but that doesn't seem to work. I've also tried wrapping the options hash in curly-braces:

it { should have_link('link text', {href: 'url', target: '_blank'}) }

无论实际页面和链接中目标属性的存在或值。更改href属性确实会导致测试失败。我认为have_link的选项哈希是要测试的属性列表。显然我错了,但是测试目标属性的单个链接的最佳方法是什么?希望不要使用XPath搜索...

The test will always succeed regardless of the presence or value of the target attribute in the actual page and link being tested. Changing the href attribute does cause a test failure as expected. I thought the options hash for have_link was a list of attributes to test for. Apparently I'm wrong, but what's the best way to test a single link for it's target attribute? Hopefully it's not to use an XPath search...

推荐答案

我不确定您的代码为什么不起作用。您是否尝试过:

I am not sure why your code does not work. Have you tried this:

find_link('link_text')[:href].should == 'url'
find_link('link_text')[:target].should == '_blank'

这篇关于使用Capybara / RSpec测试链接的target属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆