简体   繁体   English

单击带有href属性的元素是否始终可以工作

[英]Will an element with an href attribute always work when clicked

I am writing a suite of automated UI tests. 我正在编写一套自动化的UI测试。 I have a set of tests that verifies the links in a navbar work correctly, they take an annoying long time because it's loading 2 pages per test and there are many links in the nav bar. 我有一组测试来验证导航栏中的链接是否正常工作,它们花费了很长的时间,因为每个测试加载2页,并且导航栏中有很多链接。 I am wondering if it is necessary to actually click the links? 我想知道是否有必要实际单击链接?

One of the links would look like this, they're all basically the same, all contained inside a list of of <li> elements: 其中一个链接看起来像这样,它们基本相同,都包含在<li>元素列表中:

<a href="/projects/7d9162e5-e59c-452e-b9f5-684a2e0f2924/home" data-reactid=".0.2.0.0.0.$0.0">
  <span class="icon icon-home" data-reactid=".0.2.0.0.0.$0.0.0"></span>
  <span class="label" data-reactid=".0.2.0.0.0.$0.0.1">Home</span>
</a>

I could grab the content from the href attribute and request the page programmatically (don't load it in the browser) to assert that the href is correct and this would be significantly faster. 我可以从href属性中获取内容,并以编程方式请求页面(不要将其加载到浏览器中)来断言href是正确的,这将大大加快速度。

Is there any chance that an element could have an href attribute that points to the page as expected, but for whatever reason clicking on this element could be broken? 一个元素是否有可能具有按预期指向页面的href属性,但是出于某种原因,单击该元素可能会中断?

This might be the solution that you are looking for: 这可能是您正在寻找的解决方案:

<a href="#" onclick="return false;">Link to Page</a>

That code would append the href attribute as text to the body every time a link was clicked but not actually go to that link. 每次单击链接但实际上不转到该链接时,该代码会将href属性作为文本附加到正文中。 The return false; 返回false; part of that code prevents the browser from performing the default action for that link. 该代码的一部分阻止了浏览器对该链接执行默认操作。 That exact thing could be written like this: 确切的内容可以这样写:

$("a").click(function(e) {
   $("body").append($(this).attr("href"));
   e.preventDefault();
}

By taking the href content, you might risk that your automation passes test even though the navbar link does not work. 通过使用href内容,即使navbar链接不起作用,您也可能会冒自动化通过测试的风险。 It could be that navbar link was disabled by mistake, but as the link is still present in the DOM your automation will not capture it. 可能是导航栏链接被错误地禁用,但是由于该链接仍然存在于DOM中,因此您的自动化将无法捕获它。

Just my 10 cents... 只是我的10美分...

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

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