Skip to content

Commit

Permalink
Import element tests from TestBench (#8117)
Browse files Browse the repository at this point in the history
Contains some additional tests and element changes based on review.
  • Loading branch information
Artur- authored and hesara committed Jan 11, 2017
1 parent aae3619 commit f7e33df
Show file tree
Hide file tree
Showing 115 changed files with 6,588 additions and 30 deletions.
Expand Up @@ -67,6 +67,9 @@ public String getCaption() {


@Override @Override
public void click() { public void click() {
if (isReadOnly()) {
throw new ReadOnlyException();
}
WebElement input = getInputElement(); WebElement input = getInputElement();
if (isFirefox()) { if (isFirefox()) {
// When using Valo, the input element is covered by a // When using Valo, the input element is covered by a
Expand Down
Expand Up @@ -55,26 +55,27 @@ public void selectByText(String text) throws ReadOnlyException {
} }


/** /**
* Return value of the selected option in the option group * Return list of the selected options in the checkbox group
* *
* @return value of the selected option in the option group * @return list of the selected options in the checkbox group
*/ */
public String getValue() { public List<String> getSelection() {
List<String> values = new ArrayList<>();
List<WebElement> options = findElements(bySelectOption); List<WebElement> options = findElements(bySelectOption);
for (WebElement option : options) { for (WebElement option : options) {
WebElement checkedItem; WebElement checkedItem;
checkedItem = option.findElement(By.tagName("input")); checkedItem = option.findElement(By.tagName("input"));
String checked = checkedItem.getAttribute("checked"); String checked = checkedItem.getAttribute("checked");
if (checked != null if (checked != null
&& checkedItem.getAttribute("checked").equals("true")) { && checkedItem.getAttribute("checked").equals("true")) {
return option.findElement(By.tagName("label")).getText(); values.add(option.findElement(By.tagName("label")).getText());
} }
} }
return null; return values;
} }


/** /**
* Select option in the option group with the specified value * Select option in the option group with the specified value.
* *
* @param chars * @param chars
* value of the option in the option group which will be selected * value of the option in the option group which will be selected
Expand Down
Expand Up @@ -44,6 +44,10 @@ public class ComboBoxElement extends AbstractSelectElement {
* the text of the option to select * the text of the option to select
*/ */
public void selectByText(String text) { public void selectByText(String text) {
if (isReadOnly()) {
throw new ReadOnlyException();
}

if (!isTextInputAllowed()) { if (!isTextInputAllowed()) {
selectByTextFromPopup(text); selectByTextFromPopup(text);
return; return;
Expand All @@ -57,7 +61,7 @@ public void selectByText(String text) {
/** /**
* Selects, without filtering, the first option in the ComboBox which * Selects, without filtering, the first option in the ComboBox which
* matches the given text. * matches the given text.
* *
* @param text * @param text
* the text of the option to select * the text of the option to select
*/ */
Expand Down Expand Up @@ -96,7 +100,7 @@ private boolean isReadOnly(WebElement elem) {


/** /**
* Checks if text input is allowed for the combo box. * Checks if text input is allowed for the combo box.
* *
* @return <code>true</code> if text input is allowed, <code>false</code> * @return <code>true</code> if text input is allowed, <code>false</code>
* otherwise * otherwise
*/ */
Expand Down
Expand Up @@ -40,7 +40,7 @@ protected void init() {
} }


/** /**
* Selects the option(s) with the given text. * Selects the option with the given text.
* <p> * <p>
* For a ListSelect in multi select mode, adds the given option(s) to the * For a ListSelect in multi select mode, adds the given option(s) to the
* current selection. * current selection.
Expand All @@ -49,6 +49,10 @@ protected void init() {
* the text of the option * the text of the option
*/ */
public void selectByText(String text) { public void selectByText(String text) {
if (isReadOnly()) {
throw new ReadOnlyException();
}

select.selectByVisibleText(text); select.selectByVisibleText(text);
if (isPhantomJS() && select.isMultiple()) { if (isPhantomJS() && select.isMultiple()) {
// Phantom JS does not fire a change event when // Phantom JS does not fire a change event when
Expand All @@ -64,6 +68,9 @@ public void selectByText(String text) {
* the text of the option * the text of the option
*/ */
public void deselectByText(String text) { public void deselectByText(String text) {
if (isReadOnly()) {
throw new ReadOnlyException();
}
select.deselectByVisibleText(text); select.deselectByVisibleText(text);
if (isPhantomJS() && select.isMultiple()) { if (isPhantomJS() && select.isMultiple()) {
// Phantom JS does not fire a change event when // Phantom JS does not fire a change event when
Expand Down
Expand Up @@ -15,10 +15,7 @@
*/ */
package com.vaadin.testbench.elements; package com.vaadin.testbench.elements;


import java.util.List;

import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;


import com.vaadin.testbench.By; import com.vaadin.testbench.By;
import com.vaadin.testbench.elementsbase.ServerClass; import com.vaadin.testbench.elementsbase.ServerClass;
Expand All @@ -27,27 +24,13 @@
public class SliderElement extends AbstractFieldElement { public class SliderElement extends AbstractFieldElement {
/** /**
* Get value of the slider * Get value of the slider
* *
* Warning! This method cause slider popup to appear on the screen. To hide * Warning! This method cause slider popup to appear on the screen. To hide
* this popup just focus any other element on the page. * this popup just focus any other element on the page.
*/ */
public String getValue() { public String getValue() {
List<WebElement> popupElems = findElements(By.vaadin("#popup")); WebElement popupElem = findElement(By.vaadin("#popup"));
// SubPartAware was implemented after 7.2.6, not sure in which release return popupElem.getAttribute("textContent");
// it will be included
if (popupElems.isEmpty()) {
throw new UnsupportedOperationException(
"Current version of vaadin doesn't support geting values from sliderElement");

}
WebElement popupElem = popupElems.get(0);

if (BrowserType.IE.equals(getCapabilities().getBrowserName())
&& "8".equals(getCapabilities().getVersion())) {
return popupElem.getAttribute("innerText");
} else {
return popupElem.getAttribute("textContent");
}


} }


Expand Down
Expand Up @@ -62,7 +62,17 @@ private void deselectAll() {
} }
} }


/**
* Deselects the option with the given option text, i.e. removes it from the
* right side column.
*
* @param text
* the text of the option to deselect
*/
public void deselectByText(String text) { public void deselectByText(String text) {
if (isReadOnly()) {
throw new ReadOnlyException();
}
selectedOptions.deselectAll(); selectedOptions.deselectAll();
selectedOptions.selectByVisibleText(text); selectedOptions.selectByVisibleText(text);
deselButton.click(); deselButton.click();
Expand Down Expand Up @@ -98,7 +108,18 @@ public List<String> getAvailableOptions() {
return getOptionsFromSelect(options); return getOptionsFromSelect(options);
} }


/**
* Selects the option with the given option text, i.e. adds it to the right
* side column.
*
* @param text
* the text of the option to select
*/
public void selectByText(String text) { public void selectByText(String text) {
if (isReadOnly()) {
throw new ReadOnlyException();
}

options.deselectAll(); options.deselectAll();
options.selectByVisibleText(text); options.selectByVisibleText(text);
selButton.click(); selButton.click();
Expand All @@ -113,7 +134,9 @@ private List<String> getOptionsFromSelect(Select select) {
} }


/** /**
* Return first selected item (item in the right part of component) * Return first selected item (item in the right part of component).
*
* @return the option text for the item
*/ */
public String getValue() { public String getValue() {
String value = ""; String value = "";
Expand Down
@@ -0,0 +1,173 @@
package com.vaadin.tests.elements;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;

import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.v7.data.Property.ValueChangeEvent;
import com.vaadin.v7.data.Property.ValueChangeListener;
import com.vaadin.v7.ui.AbstractSelect;
import com.vaadin.v7.ui.AbstractTextField;
import com.vaadin.v7.ui.CheckBox;
import com.vaadin.v7.ui.ComboBox;
import com.vaadin.v7.ui.DateField;
import com.vaadin.v7.ui.Label;
import com.vaadin.v7.ui.ListSelect;
import com.vaadin.v7.ui.NativeSelect;
import com.vaadin.v7.ui.OptionGroup;
import com.vaadin.v7.ui.PasswordField;
import com.vaadin.v7.ui.Slider;
import com.vaadin.v7.ui.TextArea;
import com.vaadin.v7.ui.TextField;
import com.vaadin.v7.ui.TwinColSelect;

/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

/**
* UI test for getValue() method of components: TextField, TextArea,
* PasswordField, ComboBox, ListSelect, NativeSelect, OptionGroup, CheckBox,
* DateField, TwinColSelect
*
* @since
* @author Vaadin Ltd
*/
public class CompatibilityComponentElementGetValue extends AbstractTestUI {

public static final String TEST_STRING_VALUE = "item 2";
public static final int TEST_SLIDER_VALUE = 42;
public static final float TEST_FLOAT_VALUE = 0.42f;
public static final Date TEST_DATE_VALUE = Calendar.getInstance().getTime();
DateField df;
final Label valueChangeLabel = new Label("Initial value");

// These constants are used to check that change value event was
// called
public static final String[] FIELD_VALUES = { "textFieldValueChange",
"textAreaValueChange", "passwordValueChange" };
public static final String CHECKBOX_VALUE_CHANGE = "checkboxValueChange";
public static final String DATEFIELD_VALUE_CHANGE = "dateFieldValueChange";
public static final String TWINCOL_VALUE_CHANGE = "twinColValueChange";

@Override
protected void setup(VaadinRequest request) {

AbstractTextField[] fieldComponents = { new TextField(), new TextArea(),
new PasswordField()

};

AbstractSelect[] selectComponents = { new ComboBox(), new ListSelect(),
new NativeSelect(), new OptionGroup() };

for (AbstractSelect comp : selectComponents) {
comp.addItem("item 1");
comp.addItem(TEST_STRING_VALUE);
comp.addItem("item 3");
comp.setValue(TEST_STRING_VALUE);
addComponent(comp);
}
addComponent(createTwinColSelect());
for (int i = 0; i < fieldComponents.length; i++) {
AbstractTextField field = fieldComponents[i];
field.setValue(TEST_STRING_VALUE);
ValueChangeListener listener = new MyValueChangeListener(
FIELD_VALUES[i]);
field.addValueChangeListener(listener);
addComponent(field);
}

addComponent(createCheckBox());
addComponent(createSlider());
addComponent(createDateField());
valueChangeLabel.setId("valueChangeLabel");
addComponent(valueChangeLabel);
}

private DateField createDateField() {
DateField df = new DateField();
df.setDateFormat("yyyy-MM-dd");
df.setValue(TEST_DATE_VALUE);
df.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
valueChangeLabel.setValue(DATEFIELD_VALUE_CHANGE);
}
});
return df;
}

private Slider createSlider() {
Slider sl = new Slider(0, 100);
sl.setWidth("100px");
sl.setValue(new Double(TEST_SLIDER_VALUE));
return sl;
}

private CheckBox createCheckBox() {
CheckBox cb = new CheckBox();
cb.setValue(true);
cb.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
valueChangeLabel.setValue(CHECKBOX_VALUE_CHANGE);
}
});
return cb;
}

@Override
protected String getTestDescription() {
return "Field elements getValue() should return test value";
}

@Override
protected Integer getTicketNumber() {
return 13455;
}

private TwinColSelect createTwinColSelect() {
TwinColSelect tab = new TwinColSelect("");
tab.addItems("item 1", TEST_STRING_VALUE, "item 3", "item 4");
// Preselect a few items by creating a set
tab.setValue(new HashSet<String>(Arrays.asList(TEST_STRING_VALUE)));
tab.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
valueChangeLabel.setValue(TWINCOL_VALUE_CHANGE);
}
});

return tab;
}

private class MyValueChangeListener implements ValueChangeListener {
String value;

public MyValueChangeListener(String value) {
this.value = value;
}

@Override
public void valueChange(ValueChangeEvent event) {
valueChangeLabel.setValue("");
valueChangeLabel.setValue(value);
}
}
}

0 comments on commit f7e33df

Please sign in to comment.