为什么我应该使用 CSS 选择器而不是 XPath 进行自动化测试? [英] Why should I ever use CSS selectors as opposed to XPath for automated testing?

查看:29
本文介绍了为什么我应该使用 CSS 选择器而不是 XPath 进行自动化测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我理解为什么使用 CSS 选择器甚至是自动化测试的一个选项.我一直在我的工作场所使用 Ghost Inspector 工具为我们的东西创建大量自动化测试.此工具为您提供了使用 CSS 选择器而不是 XPath 的选项.为什么?

XPath 比 CSS 更耐用.任何给定 UI 上的 CSS 几乎每周都会在某些项目/功能上发生变化.这使得测试非常脆弱,并且容易经常被破坏.

是不是因为大多数新的测试编写者不想学习任何 XPath 知识而希望坚持基础知识?CSS 选择器看起来比 XPath 语法更漂亮?请说服我.谢谢.

解决方案

JeffC 的此处的答案 可以总结每种定位器策略的优缺点.但我会专门解决你的问题.

首先,没有必要让任何人说服你选择器更好,因为从纯粹的功能角度来看,它们根本不是(我说这是一个拥有黄金的人 标签徽章和近 1000 个问题的答案标签,所以你知道我没有偏见).如果您更喜欢 XPath,请使用它 — 在功能和您可以做什么方面,XPath 非常 优越,确实没有竞争.而且,正如您正确指出的,性能不再是问题(如果曾经是).

选择器适用于快速而简单的用例,以及来自 HTML 和 CSS 代码库的用户,例如网络开发人员,他们希望开始使用自动化测试而无需学习其他 DSL.如果您负责自己网站的 CSS,您还可以轻松地将选择器从样式表复制到测试中,具体取决于您要测试的内容.

另一方面,如果您来自 XML/XSLT/XPath 背景,那太好了,您可以继续使用您熟悉和喜爱的 XPath1

<块引用>

是的,Xpath 比 CSS 更持久,因为它可以调用包含特定内容的功能.

具有内容包含功能并不会使 XPath 更加耐用"——而是使其更加通用.如果您仅依赖元素的内容并且该内容可能会更改或移动,那么您的 XPath 与仅依赖元素的属性或其在 DOM 树中的位置的选择器一样脆弱.

您可以通过多种方式使您的 XPath 或选择器或多或少变得脆弱,但这只是表明 DSL 的通用性如何,而不是它本质有多脆弱.><小时>

1 取决于您使用的 XPath 版本.

Please help me understand why using CSS selectors are even an option for automated testing. I've been using the tool Ghost Inspector some in my workplace for creating lots of automated tests for our stuff. This tool gives you the option of using CSS selectors intead of XPath. Why?

XPath is SO much more durable than CSS. The CSS on any given UI is subject to change almost weekly on some projects/features. This make the tests extremely brittle and prone to being broken regularly.

Is it because most new test writers don't want to learn about anything XPath and wish to stick to the basics? CSS selectors look prettier than XPath syntax? Please convince me. thanks.

解决方案

JeffC's answer here does plenty to sum up the pros and cons of each locator strategy. But I'll address your points specifically.

First off, there is no need for anyone to convince you that selectors are better, because from a purely functional standpoint, they simply aren't (and I'm saying this as someone with a gold tag badge and almost 1000 answers to questions with that tag, so you know I'm not biased). If you're more comfortable with XPath, use it — in terms of features and what you can do, XPath is vastly superior, there really is no contest there. And, as you correctly state, performance is no longer an issue (if it ever was).

Selectors are there for quick and simple use cases and for users coming from HTML and CSS codebases, such as web developers, who want to get started with automated tests without having to learn another DSL. If you're responsible for the CSS of your own site you can also easily copy selectors from your stylesheet into your tests depending on what exactly you're testing.

If on the other hand you're coming from an XML/XSLT/XPath background, wonderful, you get to keep using the XPath you know and love1!

Yes, Xpath is way more durable than CSS because it can invoke specific content contains functionality.

Having a content contains feature doesn't make XPath more "durable" — it makes it more versatile. If you rely solely on an element's content and that content can potentially change or move around, your XPath becomes no less brittle than a selector that relies solely on an element's attributes or its position in the DOM tree.

You can do any of a number of things to make your XPath or selector more or less brittle, but that's an indicator of how versatile the DSL is, not how brittle it inherently is.


1 Depending on what version of XPath you're used to.

这篇关于为什么我应该使用 CSS 选择器而不是 XPath 进行自动化测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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