想要动态使用枚举文件名来获取常量
我有一个配置文件,其键值对为 语言=“IN”
我有多个页面对象枚举文件,名称为 PageObject_US,PageObject_UK,PageObject_IN
每个页面对象枚举文件都有可以使用例如访问的常量 PageObjects_US.String.lable
但我想要实现的是一种创建类似下面的内容的方法,
- 从配置文件中获取参数,将其存储在字符串中 如 String language = "IN"
- 然后使用 "PageObjects_" + language 连接得到 (PageObjects_IN),
- 这样返回值就可以用来从 PageObjects_IN.String.label 中获取常量。
以下是代码块:
if(!ENV.equalsIgnoreCase("development") && VALIDATION.equalsIgnoreCase("yes")) {
Elements.ByTitle(webDriver,PageObjects_IN.GREAT.label);
Elements.ByID(webDriver, PageObjects_IN.COUNTER.label);
}
在上面我想在运行时使用枚举文件 PageObjects_IN 因为我有很多枚举文件,
下面是枚举
public enum PageObjects_IN {
// Text
GREAT("great"),
COUNTER("counter");
public final String lable;
PageObjects_IN(final String lable) {
this.lable = lable;
}
}
I have a config file with key value pair aslanguage = "IN"
and i have multiple page object enum files with name asPageObject_US,PageObject_UK,PageObject_IN
every page object enum file has constants that can be accessed using for examplePageObjects_US.String.lable
but what i want to achieve is a way to create something like below
- take the parameter from config file store it in a string
like String language = "IN" - Then concatenate using "PageObjects_" + language to get (PageObjects_IN)
- so that the returned value can be used to fetch the constants from PageObjects_IN.String.label.
following is the code block:
if(!ENV.equalsIgnoreCase("development") && VALIDATION.equalsIgnoreCase("yes")) {
Elements.ByTitle(webDriver,PageObjects_IN.GREAT.label);
Elements.ByID(webDriver, PageObjects_IN.COUNTER.label);
}
In the above i want to use enum file PageObjects_IN at run time as i have many enum files
below is the enum
public enum PageObjects_IN {
// Text
GREAT("great"),
COUNTER("counter");
public final String lable;
PageObjects_IN(final String lable) {
this.lable = lable;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的(使用反射),但强烈不推荐,因为它消除了 Java 语言构造的效率。
不推荐的方式
假设您有一个存储包
click.webelement.cucumber.po
,
然后要获取一个值,您可以执行如下操作:
这是容易出错的方法,您不会从编译阶段受益。
推荐方法 - 1
使用类而不是使用
enum
。因此
您的测试会更简单
推荐方法 - 2
您可以将 PageFactory 工具用于页面对象并使用 这个库来参数化你的定位器,例如(如果你使用 test ng):
你的页面将是这样的:
This is possible (using reflection) but strongly not recommended as it eliminates the efficiency of Java language constructs.
Not recommended way
Say you have a package
click.webelement.cucumber.po
where you storeand
Then to take a value you can do something like this:
This is error-prone approach and you would not have benefit from compile phase.
Recommended approach - 1
Instead of having
enum
use classes.and
and
so your test would be much simpler
Recommended approach - 2
You can utilize PageFactory harness for your page objects and use this lib to parametrize your locators, like (if you use test ng):
Where your page would be like this: