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

Commit

Permalink
Merge pull request #41 from slu-it/select-performance
Browse files Browse the repository at this point in the history
Performance enhancement for the Select PageObject
  • Loading branch information
Drakojin authored Aug 25, 2016
2 parents 3abe7d7 + 6ca4dbb commit e46a484
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import info.novatec.testit.webtester.eventsystem.events.pageobject.SelectedByTextEvent;
import info.novatec.testit.webtester.eventsystem.events.pageobject.SelectedByValueEvent;
import info.novatec.testit.webtester.utils.Asserts;
import info.novatec.testit.webtester.pageobjects.utils.EnhancedSelect;


/**
Expand Down Expand Up @@ -84,8 +85,9 @@ public void selectByValue(final String... values) {
public void execute(PageObject pageObject) {
Asserts.assertEnabledAndVisible(pageObject);
deselectIfMultiple();
EnhancedSelect select = getSelect();
for (String value : values) {
getSelect().selectByValue(value);
select.selectByValue(value);
logger.debug(logMessage("selected option with value: {}"), value);
fireEventAndMarkAsUsed(new SelectedByValueEvent(pageObject, value));
}
Expand All @@ -112,8 +114,9 @@ public void selectByIndex(final int... indices) {
public void execute(PageObject pageObject) {
Asserts.assertEnabledAndVisible(pageObject);
deselectIfMultiple();
EnhancedSelect select = getSelect();
for (int index : indices) {
getSelect().selectByIndex(index);
select.selectByIndex(index);
logger.debug(logMessage("selected option with index: {}"), index);
fireEventAndMarkAsUsed(new SelectedByIndexEvent(pageObject, index));
}
Expand Down Expand Up @@ -225,11 +228,8 @@ public Integer getFirstSelectedIndex() {
@Override
public Integer execute(PageObject pageObject) {
try {
List<WebElement> allOptions = getAllOptions();
if (allOptions.isEmpty()) {
return null;
}
return allOptions.indexOf(getFirstSelectedOption());
String indexAsString = getFirstSelectedOption().getAttribute("index");
return Integer.valueOf(indexAsString);
} catch (NoSuchElementException e) {
logger.warn(logMessage(NOTHING_SELECTED_INDEX));
return null;
Expand All @@ -252,11 +252,9 @@ public List<Integer> getAllSelectedIndices() {
@Override
public List<Integer> execute(PageObject pageObject) {
List<Integer> selectedIndices = new LinkedList<Integer>();
List<WebElement> allOptions = getAllOptions();
if (!allOptions.isEmpty()) {
for (WebElement option : getAllSelectedOptions()) {
selectedIndices.add(allOptions.indexOf(option));
}
for (WebElement option : getAllSelectedOptions()) {
String indexAsString = option.getAttribute("index");
selectedIndices.add(Integer.valueOf(indexAsString));
}
return selectedIndices;
}
Expand Down Expand Up @@ -353,7 +351,7 @@ public Integer execute(PageObject pageObject) {
* @return true if it is, false otherwise
* @since 0.9.0
*/
public Boolean isMultselect() {
public Boolean isMultiSelect() {
return executeAction(new PageObjectCallbackWithReturnValue<Boolean>() {

@Override
Expand All @@ -364,8 +362,8 @@ public Boolean execute(PageObject pageObject) {
});
}

private org.openqa.selenium.support.ui.Select getSelect() {
return new org.openqa.selenium.support.ui.Select(getWebElement());
private EnhancedSelect getSelect() {
return new EnhancedSelect(getWebElement());
}

private List<WebElement> getAllOptions() {
Expand All @@ -381,7 +379,7 @@ private WebElement getFirstSelectedOption() {
}

private void deselectIfMultiple() {
if (isMultselect()) {
if (isMultiSelect()) {
getSelect().deselectAll();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package info.novatec.testit.webtester.pageobjects.utils;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;


/**
* This implementation of Selenium's {@link Select} helper class fixes some issues with the original implementation.
* <p>
* <b>Fixed Issues:</b>
* <ul>
* <li>{@link #getFirstSelectedOption()} has very bad performance in case of great number of options</li>
* <li>{@link #getAllSelectedOptions()} has very bad performance in case of great number of options</li>
* </ul>
*
* @since 1.2
*/
public class EnhancedSelect extends Select {

private final WebElement element;

public EnhancedSelect(WebElement element) {
super(element);
this.element = element;
}

@Override
public WebElement getFirstSelectedOption() {
return element.findElement(By.cssSelector("option:checked"));
}

@Override
public List<WebElement> getAllSelectedOptions() {
return element.findElements(By.cssSelector("option:checked"));
}

}
15 changes: 15 additions & 0 deletions webtester-support-marionette/src/license/THIRD-PARTY.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by org.codehaus.mojo.license.AddThirdPartyMojo
#-------------------------------------------------------------------------------
# Already used licenses in project :
# - ASL, version 2
# - Apache License, Version 2.0
# - LGPL, version 2.1
# - MIT License
# - The Apache License, Version 2.0
# - The Apache Software License, Version 2.0
#-------------------------------------------------------------------------------
# Please fill the missing licenses for dependencies :
#
#
#Fri Jul 29 18:18:37 CEST 2016
cglib--cglib-nodep--2.1_3=The Apache Software License, Version 2.0

0 comments on commit e46a484

Please sign in to comment.