简体   繁体   English

如何为Vaadin应用程序的UI组件生成唯一的ID?

[英]How to generate unique IDs for UI components of Vaadin applications?

Normally a Vaadin application sets a sequential ID for each user interface component of the application. 通常,Vaadin应用程序为该应用程序的每个用户界面组件设置一个顺序ID。 Unfortunately these IDs are not very useful for the purpose of test automation as they are generated dynamically and may change during runtime (per session or when new components are added etc.). 不幸的是,这些ID对于自动测试而言不是很有用,因为它们是动态生成的,并且可能会在运行时(每个会话或添加新组件等时)发生变化。

For test automation one need (at least per application) unique and static ID for each component. 对于测试自动化,每个组件(至少对于每个应用程序)需要一个唯一的静态ID。

A) Can Vaadin's setId() method [1] be utilized to generate test automation friendly component IDs? A)可以利用Vaadin的setId()方法[1]来生成测试自动化友好的组件ID吗?

B) Can Vaadin's addStyleName() or setStyleName() be useful by generating a custom CSS style which later could be "abused" as an ID? B)Vaadin的addStyleName()setStyleName()是否可以通过生成自定义CSS样式(以后可以作为ID“滥用”)来使用?

Some design ideas as discussed in [3]: [3]中讨论的一些设计思想:

  • separate id creation from id assignment 将ID创建与ID分配分开
  • use a naming strategy to create the id 使用命名策略创建ID
  • assign the id at component attach time 在组件连接时分配ID

[1] https://vaadin.com/api/7.7.3/com/vaadin/ui/Component.html#setId-java.lang.String- [1] https://vaadin.com/api/7.7.3/com/vaadin/ui/Component.html#setId-java.lang.String-

[2] https://vaadin.com/api/7.7.3/com/vaadin/ui/Component.html#setStyleName-java.lang.String- [2] https://vaadin.com/api/7.7.3/com/vaadin/ui/Component.html#setStyleName-java.lang.String-

[3] https://vaadin.com/forum#!/thread/278068 [3] https://vaadin.com/forum#!/thread/278068

A) Can Vaadin's setId() method [1] be utilized to generate test automation friendly component IDs? A)可以利用Vaadin的setId()方法[1]来生成测试自动化友好的组件ID吗?

Yes, it replaced the deprecated setDebugId() . 是的,它替换了已弃用的setDebugId() From its javadoc: 从其javadoc:

Description copied from interface: com.vaadin.ui.Component 从接口com.vaadin.ui.Component复制的说明

Adds an unique id for component that is used in the client-side for testing purposes. 为在客户端中用于测试目的的组件添加唯一的ID。 Keeping identifiers unique is the responsibility of the programmer. 保持标识符唯一是程序员的责任。

We did not use it with any kind of naming strategy automation "framework" as it's discussed in your 3rd link. 我们没有将它与任何命名策略自动化“框架”一起使用 ,这已在您的第3个链接中进行了讨论。 We manually set component IDs according to an internal agreement. 我们根据内部协议手动设置组件ID。 Please be aware that not all components can be assigned IDs, eg menu items . 请注意,并非所有组件都可以分配ID,例如菜单项


B) Can Vaadin's addStyleName() or setStyleName() be useful by generating a custom CSS style which later could be "abused" as an ID? B)Vaadin的addStyleName()或setStyleName()是否可以通过生成自定义CSS样式(以后可以作为ID“滥用”)来使用?

You could, but it's more or less the same with A). 您可以,但与A)大致相同。 You can then find elements using findElement(By.className("some-class")) ; 然后,您可以使用findElement(By.className("some-class"))找到元素;

public static By className(@NotNull String className) 公共静态按className(@NotNull字符串className)

Finds elements based on the value of the "class" attribute. 根据“ class”属性的值查找元素。 If an element has many classes then this will match against each of them. 如果一个元素有很多类,那么它将与它们中的每一个匹配。 For example if the value is "one two onone", then the following "className"s will match: "one" and "two" 例如,如果值是“ one two onone”,则以下“ className”将匹配:“ one”和“ two”


Another option would be using xpath, eg findElement(By.xpath("//*[contains(@class, 'column-hiding-toggle')]/span/div[text()='Name']")) . 另一种选择是使用xpath,例如findElement(By.xpath("//*[contains(@class, 'column-hiding-toggle')]/span/div[text()='Name']"))

Although very powerful and flexible (if I'm not mistaking both By.id & By.className are implemented with xpaths underneath the hood), its downside is that it can become high maintenance if the structure of your user interfaces changes frequently. 尽管功能非常强大且灵活(如果我不误解By.idBy.className都是在By.id By.className来实现的),但其缺点是,如果用户界面的结构频繁更改,它可能会成为高维护性的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM