java 寻找 WebElements,最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17454154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding WebElements, best practices
提问by Brian
In our current automation (using Selenium/WebDriver/Java), we use @FindBy
veryextensively. For example:
在我们目前的自动化(使用 Selenium/WebDriver/Java)中,我们使用@FindBy
非常广泛。例如:
@FindBy(css="a[name='bcrumb']") protected List<WebElement> breadCrumbLinks;
@FindBy(id="skuError") protected WebElement skuError;
@FindBy(className="reducedPrice") protected List<WebElement> reducedPrice;
@FindBy(partialLinkText="Injinji RUN 2.0") protected WebElement playButton;
@FindBy(linkText="annual member refund") protected WebElement annualMemberRefund;
@FindBy(xpath="//li[@itemprop='price']") protected WebElement productPrice;
By definition, @FindBy
can locate a selector using the following: using, id, name, className, css, tagName, linkText, partialLinkText and xpath.
根据定义,@FindBy
可以使用以下内容定位选择器:using、id、name、className、css、tagName、linkText、partialLinkText 和 xpath。
Recently, our front-end devs proposed that we implement an new attribute class that begins with 'test='. I think this is a great idea since we could find WebElements by just looking for that blurb of text, rather than the values that @FindBy
inherently uses. My question is, would it be better to extend the existing functionalityof @FindBy
OR, create a new way of searching for the WebElements we use in our tests?
最近,我们的前端开发人员提议我们实现一个以“test=”开头的新属性类。我认为这是一个好主意,因为我们可以通过只查找文本的模糊而不是@FindBy
固有使用的值来找到 WebElements 。我的问题是,这将是更好地扩大现有功能的@FindBy
OR,创建搜索,我们在我们的测试中使用WebElements的一种新的方式?
回答by Jim Holmes
First off, there are no "best practices," just ones that work well in your particular context. Sorry, that's an old gripe of mine...
首先,没有“最佳实践”,只有在您的特定环境中运行良好的那些。对不起,这是我的老抱怨...
I wouldn't spend the effort for custom attributes unless you can't work with an existing approach. I prefer using existing locators (find logic) where possible.
除非您无法使用现有方法,否则我不会为自定义属性花费精力。我更喜欢尽可能使用现有的定位器(查找逻辑)。
Whenever possible, use ID attributes. If the page is valid HTML, then IDs are unique on the page. They're extraordinarily fast for resolution in every browser, and the UI can change dramatically but your script will still locate the element.
尽可能使用 ID 属性。如果页面是有效的 HTML,则 ID 在页面上是唯一的。它们在每个浏览器中的解析速度都非常快,并且 UI 可能会发生巨大变化,但您的脚本仍将定位该元素。
Sometimes IDs aren't the right choice. Dynamically generated IDs are almost always the wrongchoice when you're working with something like a grid control. You rely on an id that is likely tied to the specific row position and then you're screwed if your row changes.
有时,ID 不是正确的选择。当您使用网格控件之类的东西时,动态生成的 ID 几乎总是错误的选择。您依赖于可能与特定行位置相关联的 id,然后如果您的行发生变化,您就会被搞砸。
In some of these cases your devs can help you out by appending or prepending constant values to a dynamically generated ID value. ASP.NET Webforms does crazy stuff with dynamically generated values, so I've used a suffix to my advantage several times.
在某些情况下,您的开发人员可以通过将常量值附加或预先添加到动态生成的 ID 值来帮助您。ASP.NET Webforms 用动态生成的值做了一些疯狂的事情,所以我多次使用后缀来发挥我的优势。
Link text, name attribute values, and CSS selectors (JQuery style) are great second choices when you can't get a stable, reliable ID, or one's just not available.
链接文本、名称属性值和 CSS 选择器(JQuery 样式)是您无法获得稳定、可靠的 ID 或不可用时的绝佳第二选择。
XPath is my last choice in nearly all situations. It's slow, can be extremely brittle, and is hard to deal with when it's a complex XPath. That said, if you need to move up and down the page DOM for your locators, then it's the only choice.
在几乎所有情况下,XPath 都是我最后的选择。它很慢,可能非常脆弱,并且当它是复杂的 XPath 时很难处理。也就是说,如果您需要为定位器在页面 DOM 上上下移动,那么这是唯一的选择。
Using one of the existing FindBy methods means you'll be using a well-understood, well-supported locator strategy. That's a big bonus when you're trying to figure out an old test, or when onboarding someone new to your team.
使用现有的 FindBy 方法之一意味着您将使用易于理解、得到充分支持的定位器策略。当您试图找出旧测试或为团队中的新成员入职时,这是一个很大的好处。
That's my $0.02.
那是我的 0.02 美元。
回答by Stormy
I think this will help Selenium Locators Best Practices
我认为这将有助于Selenium Locators 最佳实践
Tastiest Locators:ID Name Class Link Text or Partial Text
最美味的定位器:ID 名称类链接文本或部分文本
Yummy LocatorsIndex XPath Child Elements CSS Properties Edible Locators JS Events DOM Elements KeyStrokes Coordinates
Yummy LocatorsIndex XPath Child Elements CSS Properties Edible Locators JS Events DOM Elements KeyStrokes Coordinates
回答by user2525437
If I understood you correctly you can continue using @FindBy annotation with css selectors such as css = "[test='...']".
如果我理解正确,您可以继续将 @FindBy 注释与 css 选择器一起使用,例如 css = "[test='...']"。