com.epam.jdi.uitests.mobile.appium.elements.composite.Search Maven / Gradle / Ivy
package com.epam.jdi.uitests.mobile.appium.elements.composite;
/*
* Copyright 2004-2016 EPAM Systems
*
* This file is part of JDI project.
*
* JDI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JDI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JDI. If not, see .
*/
import com.epam.jdi.uitests.core.interfaces.common.IButton;
import com.epam.jdi.uitests.core.interfaces.common.ITextField;
import com.epam.jdi.uitests.core.interfaces.complex.ISearch;
import com.epam.jdi.uitests.mobile.appium.elements.common.Button;
import com.epam.jdi.uitests.mobile.appium.elements.common.TextField;
import com.epam.jdi.uitests.mobile.appium.elements.complex.TextList;
import org.openqa.selenium.By;
import java.lang.reflect.Field;
import java.util.List;
import static com.epam.commons.ReflectionUtils.getFields;
import static com.epam.commons.ReflectionUtils.getValueField;
import static com.epam.jdi.uitests.core.settings.JDISettings.exception;
import static java.lang.String.format;
/**
* Created by Roman_Iovlev on 7/29/2015.
*/
public class Search extends TextField implements ISearch {
protected IButton searchButton;
protected TextList suggestions;
public Search() {
super();
}
public Search(By byLocator) {
super(byLocator);
}
public Search(By byLocator, By selectLocator) {
this(byLocator, selectLocator, null);
}
public Search(By byLocator, By selectLocator, By suggestionsListLocator) {
super(byLocator);
this.searchButton = new Button(selectLocator);
this.suggestions = new TextList(suggestionsListLocator);
}
protected void findAction(String text) {
getSearchField().newInput(text);
getSearchButton().click();
}
protected void chooseSuggestionAction(String text, String selectValue) {
getSearchField().input(text);
if (suggestions != null)
getSuggestionsList().getElement(selectValue).click();
else
throw exception("Select locator not specified for search. Use accordance constructor");
}
protected void chooseSuggestionAction(String text, int selectIndex) {
getSearchField().input(text);
getSuggestions().getElement(selectIndex).click();
}
protected List getSuggesionsAction(String text) {
getSearchField().input(text);
return getSuggestions().getLabels();
}
/**
* @param text Specify Text to search
* Input text in search field and press search button
*/
public final void find(String text) {
invoker.doJAction(format("Search text '%s'", text), () -> findAction(text));
}
/**
* @param text Specify Text to search
* @param selectValue Specify value to choose from suggested search result
* Input text in search and then select value from suggestions
*/
public final void chooseSuggestion(String text, String selectValue) {
invoker.doJAction(format("Search for text '%s' and choose suggestion '%s'", text, selectValue),
() -> chooseSuggestionAction(text, selectValue));
}
/**
* @param text Specify Text to search
* @param selectIndex Specify index to choose from suggested search result
* Input text in search and then select suggestions by index
*/
public final void chooseSuggestion(String text, int selectIndex) {
invoker.doJAction(format("Search for text '%s' and choose suggestion '%s'", text, selectIndex),
() -> chooseSuggestionAction(text, selectIndex));
}
/**
* @param text Specify Text to search
* @return Select all suggestions for text
*/
public final List getSuggesions(String text) {
return invoker.doJActionResult(format("Get all suggestions for input '%s'", text),
() -> getSuggesionsAction(text));
}
private TextList getSuggestions() {
if (suggestions != null)
return getSuggestionsList();
throw exception("Suggestions list locator not specified for search. Use accordance constructor");
}
private TextList getSuggestionsList() {
suggestions.setParent(getParent());
return suggestions;
}
private TextField getTextField() {
TextField textField = new TextField(getLocator());
textField.setParent(getParent());
return textField;
}
private IButton getFindButton() {
searchButton.setParent(getParent());
return searchButton;
}
private ITextField getSearchField() {
if (getLocator() != null)
return getTextField();
List fields = getFields(this, ITextField.class);
switch (fields.size()) {
case 0:
throw exception("Can't find any buttons on form '%s'.", toString());
case 1:
return (ITextField) getValueField(fields.get(0), this);
default:
throw exception("Form '%s' have more than 1 button. Use submit(entity, buttonName) for this case instead", toString());
}
}
protected IButton getSearchButton() {
if (searchButton != null)
return getFindButton();
List fields = getFields(this, IButton.class);
switch (fields.size()) {
case 0:
throw exception("Can't find any buttons on form '%s'.", toString());
case 1:
return (IButton) getValueField(fields.get(0), this);
default:
throw exception("Form '%s' have more than 1 button. Use submit(entity, buttonName) for this case instead", toString());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy