Java: how to automatically generate a custom method for a variable

I have a situation where I want to define a variety of behavior 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 SomePage class, which is a page object (using the Factory Pattern page), which consists of fields ( WebElement ).

Based on the field name suffix, I need to create a number of methods. If the suffix is *Select , the field will have a number of methods that may be similar for all select field . If the suffix is *Button , it will have the method that is needed for the button field .

Is it possible to create a custom method based on suffix of field names .

NOTE. IntelliJ Supports modification of custom Getters and Setters, but does not allow the use of multiple methods for each field. It limits 1 getter and 1 setter per field. For example, this is not always the case. I have multiple setter (setter type) or getter for a field.

+6
source share
3 answers

Generating Getters and Setters in IntelliJ IDEA:

  • In the main menu, select "Code". In addition, right-click on the editor and select "Create in the context menu" or use the key combination "Alt + Insert".
  • In the pop-up list that appears in the editor, select one of the following options: getter Access method to get the current value of the selected fields. setter Mutator for setting setpoints in selected fields. Getter and Setter Both methods for selected fields.
  • In the Select Fields to Generate Recipients and Setters dialog box, select the fields you want. You can add a custom setter or getter by clicking browseButton and accessing the Getter / Setter Templates window. If getters and setters for a field already exist, this field is not on the list.
  • Click OK. For more information, click here .
0
source

You can create custom classes, but the dynamic generation of classes is a fairly advanced function, and from the sounds of your problem it is unnecessarily complicated.

I think you should create a new class for these objects with the same functionality. You should read about object-oriented programming because it is a pretty simple concept. Your example may be bad, but why are you overriding the String functions for each button? Here is an example of another construction:

 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 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(); } 
0
source

Cannot create custom methods automatically. For custom methods, you must define them why it is called custom. best of all, you can get only the basic getter and setter methods, I think you already have those, or you can do it like this class Button {string}

class buttonProperties {

int buttonLength; int buttonSize;

public int getbuttonSize () {

 return buttonSize; 

},,.

}

-4
source

All Articles