UI自动化中选择器的自我修复

时间:2019-03-19 13:34:31

标签: javascript dom xpath css-selectors automated-tests

我正在JS中进行UI自动化,因此我在其中基于XPATH或CSS选择器识别对象/元素。由于许多不希望的原因-由于在不同环境中XPATH的更改导致测试失败。

我正在寻找想法或方法-我的自动化脚本会自我修复以识别更改的XPATH / CSS选择器并进行更新或使用更改的选择器运行。

有没有办法-我可以在运行时实现此功能,以自我修复现有的Javascript自动化脚本。

1 个答案:

答案 0 :(得分:1)

First of all, automated UI tests inherently require maintenance for page changes that cause test failures. Most often these failures are a result of changes that make existing element selectors no longer work as desired. I don’t think any solution is available to change these selectors programmatically; you’d probably have to write this yourself.

In my view the best way to approach this is

  1. Adopt a locator strategy that uniquely identifies your elements while relying on as little of the DOM as possible. For me the ideal would be to have a unique Id or some other unique tag. Personally I very rarely use Xpath because it is most likely to depend on other elements, increasing the likelihood and frequency of tests needing maintenance, and it is the slowest way to locate an element.

  2. QA should be involved from the very beginning of the development process so that they can, as early as possible, identify any changes that need to be made to automated tests to accommodate upcoming features. Also, QA can collaborate with developers to establish the most “testable” product possible, usually including requests for HTML tags to help uniquely identify elements. Ideally the changes required to existing tests can be made as development occurs so that broken selectors do not delay the release cycle.

  3. Finally I think we just have to accept a certain level of flakiness and required maintenance for UI automation. Google’s target for “flaky” UI tests is 1% or less. Selenium is a great browser automation tool, but it takes a lot of planning and strategy to write reliable, maintainable, and robust UI tests. And I think considering what changes might break which selectors is essential when designing tests and locator strategies. A tool that automatically does this would be awesome, and I believe there are teams working on creating a way to do so with artificial intelligence, but nothing is publicly available yet.