Доброго времени суток. Пишу свой маленький framework. Придерживаюсь паттерна PageObject. Пытаюсь вынести все действия c WebElement (click, findElement, sendKeys) в отдельный класс BaseElement, для избегания дублирования и макаронного кода. Возникла проблема, с тем, что класс получается слишком большой (тут еще много чего добавлять можно для работы).Помогите с тем, как можно вынести методы в отдельные классы Click, Wait … без сильного ущерба для гибкости (подразумевается работа с одним элементом, но разными способами)
Сам класс
import framework.ConfigReader;
import framework.Log;
import framework.webdriver.Browser;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class BaseElement {
private WebDriver driver;
private WebDriverWait wait;
private WebElement element=null;
private List<BaseElement> baseElementList;
private List<WebElement> elementsList;
private By locator=null;
private String propertyElement ="";
public BaseElement(By locator){
driver= Browser.getInstance().getDriver();
wait=new WebDriverWait(driver,ConfigReader.getTimeOuts(),1500);
this.locator=locator;
}
public BaseElement(By locator,String propertyElement){
this(locator);
this.propertyElement = propertyElement;
}
public BaseElement(WebElement element){
this(null,"");
this.element=element;
}
public void click(){
click(propertyElement);
}
public void click(String message){
if(isClickble()){
Log.info("Select: "+message);
element.click();
}else {
Log.info("Don't select: " + message);
}
}
public boolean isClickble(){
try {
if (element==null)
element = waitElement(ExpectedConditions.elementToBeClickable(locator));
return element != null && element.isDisplayed();
} catch ( StaleElementReferenceException | IndexOutOfBoundsException e) {
return false;
}
}
public boolean isEnabled(){
try {
if (element==null)
element = waitElement(ExpectedConditions.visibilityOfElementLocated(locator));
return element != null && element.isEnabled();
} catch ( StaleElementReferenceException | IndexOutOfBoundsException | TimeoutException e) {
return false;
}
}
private WebElement initWebElement(ExpectedCondition<WebElement> expectedConditions){
element=wait.until(expectedConditions);
return element;
}
private WebElement waitElement(final ExpectedCondition<WebElement> expectedConditions){
new FluentWait<WebElement>(initWebElement(expectedConditions))
.withTimeout(ConfigReader.getTimeOuts(),TimeUnit.SECONDS)
.pollingEvery(1,TimeUnit.SECONDS)
.until(new com.google.common.base.Function<WebElement, Boolean>() {
@Override
public Boolean apply(WebElement input) {
try{
initWebElement(expectedConditions);
}catch (StaleElementReferenceException | NoSuchElementException e){
return false;
}
return true;
}
});
return initWebElement(expectedConditions);
}
public void jsClick(){
JavascriptExecutor executor = (JavascriptExecutor) Browser.getInstance().getDriver();
executor.executeScript("arguments[0].click();", waitElement(ExpectedConditions.elementToBeClickable(locator)));
}
private void findElements(){
waitElement(ExpectedConditions.visibilityOfElementLocated(locator));
elementsList=driver.findElements(locator);
}
public List<BaseElement> getBaseElementList(){
findElements();
baseElementList=new ArrayList<BaseElement>(elementsList.size());
for (WebElement anElementsList : elementsList) {
baseElementList.add(new BaseElement(anElementsList));
}
return baseElementList;
}
public String getText(){
if(isEnabled()) {
return element.getText();
}
return "Don't search element";
}
public void sendKeys(String key){
if(isEnabled()){
Log.info("Sendkeys: "+key);
element.sendKeys(key);
}else{
Log.info("Don't send "+key);
}
}
public WebElement getWebElement(){
if(isEnabled()){
return element;
}
return null;
}
}
Применение данного класса
BaseElement button=new BaseElement(By.xpath("//*[@class=\"cui-button\"]"));
public void selectButton(){
button.click();
}
Благодарю за внимание и помощь.