[英]java: how to auto-generate a custom method for a variable
I've a situation where i want to define variety of behaviour for my fields. 我有一种情况,我想为我的领域定义各种行为。
class SomePage {
//...
@FindBy(id = "selectEthinicity")
private WebElement ethinicitySelect;
@FindBy(id = "someId")
private WebElement usernameInputField;
@FindBy(id = "buttonSubmit")
private WebElement submitButton;
public void selectEthnicity(String param) throws Exception {
new Select(ethnicitySelect).selectByVisibleText(param);
}
public void selectEthnicityByIndex(String param) throws Exception {
new Select(ethnicitySelect).selectByIndex(Integer.parseInt(param));
}
public int getSelectEthinicityNumberOfOptions() {
new Select(ethnicitySelect).getOptions().size();
}
public void waitForOptionOfSelectEthnicityToLoad() {
//logic to wait for an options of Ethinicity to load
}
public void selectEthnicity_Wait(String param) throws Exception {
//wait
//select Ethinicity
}
public void selectEthnicityByIndex_Wait(String param) throws Exception {
//wait and
//Select Ethnicity by einde
}
//custom method for input Field
public String getUsernameInputFieldValue() {
usernameInputField.getAttribute("value");
}
public void enterUsernameInputField(String param) {
usernameInputField.sendKeys(param);
}
//custom method for button
public void clickSubmitButton() throws PageValidationException {
submitButton.click();
}
//...
}
Here I have a class SomePage which is a Page Object (Using Page Factory Pattern) which consist of fields ( WebElement
). 这里我有一个SomePage类,它是一个由对象组成的页面对象(使用页面工厂模式)(
WebElement
)。
Based on the suffix of field names I need to create number of method. 基于字段名称的后缀我需要创建多个方法。 If suffix is
*Select
the field will have number of methods which can be Similar for all select field
. 如果后缀为
*Select
该字段将具有多个方法,对于所有select field
可以是相似的。 If suffix is *Button
it will have a method's that are needed for button field
. 如果后缀为
*Button
则它将具有button field
所需的方法。
Can I generate the Custom Method based upon the suffix of field names
. 我可以根据
suffix of field names
的suffix of field names
生成自定义方法。
NOTE: IntelliJ Supports the modification of custom Getters and Setters but it doesn't allow multiple methods per field.
注意:IntelliJ支持修改自定义Getter和Setter,但它不允许每个字段使用多个方法。 It restricts to 1 Getter and 1 Setter per field.
它限制为每个字段1个Getter和1个Setter。 Which is not always the case as per example.
根据示例并非总是如此。 I my have multiple setter ( kind of setter) or getter for a field.
我有一个场的多个二传手(一种二传手)或吸气剂。
Generating Getters and Setters in IntelliJ IDEA : 在IntelliJ IDEA中生成getter和setter:
It is possible to generate custom classes, but dynamic class generation is a fairly advanced feature, and from the sounds of your problem it's unnecessarily complex. 可以生成自定义类,但动态类生成是一个相当高级的功能,从您的问题的声音,它是不必要的复杂。
I think you should create a new class for these objects with the same functionality. 我认为你应该为具有相同功能的这些对象创建一个新类。 You should read about Object-Oriented Programming because this is a fairly simple concept.
您应该阅读面向对象编程,因为这是一个相当简单的概念。 Maybe your example is poor, but why are you redefining String functions for each button?
也许您的示例很差,但为什么要为每个按钮重新定义String函数? Here's an example of a different design:
这是一个不同设计的例子:
public class Button {
private String text;
public Button(String text) {
this.text = text;
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
Then just declare a bunch of your new class. 然后宣布一堆你的新班级。
class Temp {
Button abcButton = new Button("abc");
Button cdeButton = new Button("cde");
// Instead of setAbcButtonToLower()
abcButton.setText(abcButton.getText().toLowerCase());
// Instead of getAbcButtonIndexOfChar()
abcButton.getText().indexOf(c);
// Instead of getAbcButtonSize()
abcButton.getText().length();
}
You cant generate custom methods automatically . 您无法自动生成自定义方法。 For custom methods you have to define them that's why it is called custom .
对于自定义方法,您必须定义它们,这就是它被称为自定义的原因。 best you can get are only basic getter and setter methods, I think you already got those
你能得到的最好的只是基本的getter和setter方法,我想你已经有了
or you can do it like this class Button{ string button } 或者你可以像这样的类按钮{string button}
class buttonProperties{ class buttonProperties {
int buttonLength; int buttonLength; int buttonSize;
int buttonSize;
public int getbuttonSize(){ public int getbuttonSize(){
return buttonSize;
} . }。 .
。 .
。
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.