Tôi có một tình huống mà tôi muốn xác định nhiều hành vi khác nhau cho các trường của mình.java: cách tự động tạo phương thức tùy chỉnh cho biến số
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();
}
//...
}
Ở đây tôi có một lớp SomePage là đối tượng trang (Sử dụng mẫu nhà máy trang) bao gồm các trường (WebElement
).
Dựa trên hậu tố của tên trường tôi cần tạo số phương thức. Nếu hậu tố là *Select
trường sẽ có số phương thức có thể tương tự cho tất cả select field
. Nếu hậu tố là *Button
, nó sẽ có phương thức cần thiết cho button field
.
Tôi có thể tạo Phương thức tùy chỉnh dựa trên suffix of field names
hay không.
LƯU Ý: IntelliJ Hỗ trợ sửa đổi Getters and Setters tùy chỉnh nhưng không cho phép nhiều phương thức cho mỗi trường. Nó giới hạn thành 1 Getter và 1 Setter trên mỗi trường. Mà không phải lúc nào cũng đúng như ví dụ. Tôi có nhiều setter (loại setter) hoặc getter cho một lĩnh vực.
Bạn có thể muốn có một cái nhìn tại [Project Lombok] (https://projectlombok.org/index.html) –
Và propably AOP. –
Có lý do nào khiến bạn không thể tạo Radio/Button lớp có các chức năng được xác định không? Bạn thậm chí có thể làm một siêu lớp và mở rộng nó, và sau đó tất cả các bạn muốn sử dụng trong lớp Temp sẽ là getters và setters cho nút/radio riêng lẻ và sau đó gọi phương thức getter/setter của nó. – Compass