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

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

问题描述

请帮助我了解为什么使用CSS选择器甚至可以进行自动化测试.我在工作场所中一直使用Ghost Inspector工具为我们的东西创建许多自动化测试.该工具为您提供了使用XPath intead的CSS选择器的选项.为什么?

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

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

解决方案

JeffC's在此处回答可以很好地总结每种定位器策略的利弊.但是,我将具体说明您的观点.

首先,没有任何人需要说服选择器更好,因为从纯粹的功能角度来看,选择器根本不是(而且我说的是具有金色


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天全站免登陆