寻找WebElements,最佳实践 [英] Finding WebElements, best practices

查看:175
本文介绍了寻找WebElements,最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们目前的自动化(使用Selenium / webdriver的/ Java的),我们使用 @FindBy 非常的广泛应用。例如:

In our current automation (using Selenium/WebDriver/Java), we use @FindBy very extensively. For example:

@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;

根据定义, @FindBy 可以使用下面的定位选择:采用,ID,名称,类名,CSS,标签名,LINKTEXT,partialLinkText和XPath

By definition, @FindBy can locate a selector using the following: using, id, name, className, css, tagName, linkText, partialLinkText and xpath.

最近,我们的前端开发者提出,我们实施以'测试='开头的新属性类。我认为这是一个好主意,因为我们可以通过只是想找文的导语,而不是 @FindBy 本身使用的值找到WebElements。我的问题是,这将是最好的扩展现有的功能 @FindBy 或者,创建寻找我们使用WebElements的新方法我们的测试?

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 functionality of @FindBy OR, create a new way of searching for the WebElements we use in our tests?

推荐答案

首先,有没有最佳做法,只是在特定情况下正常工作的。对不起,这是我的一个老抱怨...

First off, there are no "best practices," just ones that work well in your particular context. Sorry, that's an old gripe of mine...

有关自定义属性,除非你不能用现有的方法工作,我不会花力气。我preFER使用现有的定位器(找到逻辑)在可能的情况。

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.

尽可能使用ID属性。如果页面是有效的HTML,那么的ID在页面上是独一无二的。他们在每一个浏览器的分辨率非常快,用户界面​​可以显着改变,但你的脚本将仍然定位的元素。

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是不是正确的选择。动态生成的ID是几乎总是的错误的,当你喜欢的东西网格控制工作的选择。你依赖很可能绑定到特定行的位置,然后你,如果你的行更改拧一个ID。

Sometimes IDs aren't the right choice. Dynamically generated IDs are almost always the wrong choice 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.

在某些案件的开发人员,可以通过追加或prepending常量值动态生成的ID值帮助你。 ASP.NET Web表单做疯狂的事情与动态生成的值,所以我用了一个后缀为我的优势几次。

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.

链接文字,name属性值和CSS选择器(JQuery的风格)是伟大的第二选择,当你不能得到稳定的,可靠的ID,或者一个只是不可用。

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.

XPath是我最后的选择,在几乎所有的情况。它很慢,可能会非常脆,很难处理时,它是一个复杂的XPath。也就是说,如果你需要上下移动页面的DOM为您定位器,那么它是唯一的选择。

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.

使用现有FindBy方法之一,意味着你将使用一个很好理解的,受到良好支持定位策略。这就是当你试图找出一个旧的测试,或登入新的人你的团队时,一个巨大的奖赏。

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.

这是我的$ 0.02

That's my $0.02.

这篇关于寻找WebElements,最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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