我的情况是我想为我的字段定义各种行为。
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();
}
//...
}
这里我有一个SomePage类,它是一个页面对象(使用页面工厂模式),由字段(WebElement
)组成。
基于字段名称的后缀,我需要创建多个方法。如果后缀为*Select
,则该字段将具有多个方法,这些方法对于所有select field
可以是相似的。如果后缀为*Button
,则它将具有button field
所需的方法。
我可以根据suffix of field names
生成自定义方法。
注意:IntelliJ支持修改自定义Getter和Setter,但不允许每个字段使用多个方法。它限制为每个字段1个Getter和1个Setter。根据示例并非总是如此。我有一个多种二传手(一种二传手)或一个场上的吸气剂。
答案 0 :(得分:0)
在IntelliJ IDEA中生成Getters和Setter:
答案 1 :(得分:0)
可以生成自定义类,但动态类生成是一个相当高级的功能,从您的问题的声音来看,它是不必要的复杂。
我认为您应该为具有相同功能的这些对象创建一个新类。您应该阅读面向对象编程,因为这是一个相当简单的概念。也许您的示例很差,但为什么要为每个按钮重新定义String函数?这是一个不同设计的例子:
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;
}
}
然后宣布一堆新课。
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();
}
答案 2 :(得分:-4)
您无法自动生成自定义方法。对于自定义方法,您必须定义它们,这就是它被称为自定义的原因。 你能得到的最好的只是基本的getter和setter方法,我想你已经有了这些 要么 你可以这样做 class Button { 字符串按钮 }
class buttonProperties {
int buttonLength; int buttonSize;
public int getbuttonSize(){
return buttonSize;
} 。 。
}