Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Generic select #68

Merged
merged 6 commits into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions documentation/chapters/page-object-functional.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public class Headline extends PageObject {
- `<input type="password"/>`
- `<input type="number"/>`

## GenericSelect
**Extends:** PageObject

**HTML Tags:**

- `<select/>`

## Headline
**Extends:** PageObject

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package info.novatec.testit.webtester.pageobjects;

import org.openqa.selenium.WebElement;

import java.util.LinkedList;
import java.util.List;

import info.novatec.testit.webtester.api.annotations.Mapping;
import info.novatec.testit.webtester.api.callbacks.PageObjectCallbackWithReturnValue;
import info.novatec.testit.webtester.pageobjects.utils.EnhancedSelect;


@Mapping(tag = "select")
public class GenericSelect<T extends GenericSelect<T>> extends PageObject {

/**
* Returns a {@link List linked list} with the texts of all options of
* a {@link GenericSelect generic select}. If there are no option the
* returned list will be empty. The returned list is in the same order
* as the options of the {@link GenericSelect generic select}.
*
* @return the visible texts of all available options
* @since 1.2
*/
public List<String> getAllOptionTexts() {
return executeAction(new PageObjectCallbackWithReturnValue<List<String>>() {
@Override
public List<String> execute(PageObject arg) {
List<String> texts = new LinkedList<>();
for(WebElement option : getAllOptions()) {
texts.add(option.getText());
}
return texts;
}
});
}

/**
* Returns a {@link List linked list} with all available option's values.
* If there are no option the returned list will be empty. The returned
* list is in the same order as the options of the
* {@link GenericSelect generic select}.
*
* @return the values of all available options
* @since 1.2
*/
public List<String> getAllOptionValues() {
return executeAction(new PageObjectCallbackWithReturnValue<List<String>>() {
@Override
public List<String> execute(PageObject arg) {
List<String> values = new LinkedList<>();
for(WebElement option : getAllOptions()) {
values.add(option.getAttribute("value"));
}
return values;
}
});
}

/**
* Returns the number of all available options.
*
* @return the number of all available options
* @since 1.2
*/
public Integer getNumberOfOptions() {
return executeAction(new PageObjectCallbackWithReturnValue<Integer>() {

@Override
public Integer execute(PageObject pageObject) {
return getAllOptions().size();
}

});
}

public List<WebElement> getAllOptions() {
return getSelect().getOptions();
}

public EnhancedSelect getSelect() {
return new EnhancedSelect(getWebElement());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package info.novatec.testit.webtester.pageobjects;

import org.junit.Test;
import org.mockito.InjectMocks;

import info.novatec.testit.webtester.AbstractPageObjectTest;
import info.novatec.testit.webtester.api.exceptions.WrongElementClassException;


public class GenericSelectTest extends AbstractPageObjectTest {

@InjectMocks
GenericSelect cut;

@Test
public void testCorrectnessOfClassForWebElement_selectTag() {
stubWebElementTag("select");
cut.validate(webElement);
}

@Test(expected = WrongElementClassException.class)
public void testCorrectnessOfClassForWebElement_otherTag() {
stubWebElementTag("other");
cut.validate(webElement);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.File;

import info.novatec.testit.webtester.pageobjects.PageObject;
import org.junit.Before;
import org.junit.BeforeClass;

Expand Down Expand Up @@ -54,6 +55,10 @@ protected String getHTMLFilePath() {

// UTILITY

protected void assertPageObjectCanBeInitialized(PageObject object) {
assertThat(object.isPresent(), is( true));
}

protected static Browser getBrowser() {
return browser;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package integration.pageobjects;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.core.Is.is;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import integration.AbstractWebTesterIntegrationTest;

import info.novatec.testit.webtester.api.annotations.IdentifyUsing;
import info.novatec.testit.webtester.api.exceptions.WrongElementClassException;
import info.novatec.testit.webtester.pageobjects.GenericSelect;
import info.novatec.testit.webtester.pageobjects.PageObject;


public class GenericSelectIntegrationTest extends AbstractWebTesterIntegrationTest {

GenericSelectTestPage page;

@Before
public void initPage() {
page = getBrowser().create(GenericSelectTestPage.class);
}

@Override
protected String getHTMLFilePath() {
return "html/pageobjects/genericSelect.html";
}

/* get all option texts */

@Test
public final void testThatListWithOptionTextsIsReturnedCorrectly_singleSelect() {
List<String> texts = page.singleSelect.getAllOptionTexts();
assertThat(texts.get(0), is("one"));
assertThat(texts.get(1), is("two"));
assertThat(texts.get(2), is("three"));
}

@Test
public final void testThatListWithOptionTextsIsReturnedCorrectly_multiSelect() {
List<String> texts = page.multiSelect.getAllOptionTexts();
assertThat(texts.get(0), is("one"));
assertThat(texts.get(1), is("two"));
assertThat(texts.get(2), is("three"));
}

@Test
public final void testThatListWithOptionTextsIsReturnedCorrectly_emptySelect() {
List<String> optionTexts = page.emptySelect.getAllOptionTexts();
assertThat(optionTexts, is(empty()));
}

/* get all option values */

@Test
public final void testThatListWithOptionValuesIsReturnedCorrectly_singleSelect() {
List<String> values = page.singleSelect.getAllOptionValues();
assertThat(values.get(0), is("1"));
assertThat(values.get(1), is("2"));
assertThat(values.get(2), is("3"));
}

@Test
public final void testThatListWithOptionValuesIsReturnedCorrectly_multiSelect() {
List<String> values = page.multiSelect.getAllOptionValues();
assertThat(values.get(0), is("1"));
assertThat(values.get(1), is("2"));
assertThat(values.get(2), is("3"));
}


@Test
public final void testThatListWithOptionValuesIsReturnedCorrectly_emptySelect() {
List<String> optionValues = page.emptySelect.getAllOptionValues();
assertThat(optionValues, is(empty()));
}

/* number of options */

@Test
public final void testThatCorrectNumberOfOptionsIsReturned_singelSelect() {
Integer numberOfOptions = page.singleSelect.getNumberOfOptions();
assertThat(numberOfOptions, is(3));
}

@Test
public final void testThatCorrectNumberOfOptionsIsReturned_multiSelect() {
Integer numberOfOptions = page.multiSelect.getNumberOfOptions();
assertThat(numberOfOptions, is(3));
}

/* validation of mapping */

@Test
public final void testValidationOfMapping_singleSelect() {
assertPageObjectCanBeInitialized(page.singleSelect);
}

@Test
public final void testValidationOfMapping_multiSelect() {
assertPageObjectCanBeInitialized(page.multiSelect);
}

@Test(expected = WrongElementClassException.class)
public final void testValidationOfClass_noSelect() {
assertPageObjectCanBeInitialized(page.notASelect);
}

/* utilities */

private static class GenericSelectTestPage extends PageObject{
@IdentifyUsing("singleSelect")
GenericSelect singleSelect;

@IdentifyUsing("multiSelect")
GenericSelect multiSelect;

@IdentifyUsing("emptySelect")
GenericSelect emptySelect;

@IdentifyUsing("notASelect")
GenericSelect notASelect;

}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html>
<body>

<h1>GenericSelect Test Page</h1>
<h3>This page contains elements for testing the functionality of the GenericSelect page object class.</h3>

<hr>
<br><br>

<table>
<tbody>
<tr>
<td>A single select without any pre-selection</td>
<td>
<select id="singleSelect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
</td>
</tr>
<tr>
<td>A multi select without any pre-selection</td>
<td>
<select id="multiSelect" multiple="multiple">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
</td>
</tr>
<tr>
<td>A single select without any options</td>
<td>
<select id="emptySelect"> </select>
</td>
</tr>
<tr>
<td>A non select</td>
<td><span id="notASelect">This is not a Select</span></td>
</tr>
</tbody>
</table>

<br><br>
<hr>

</body>
</html>