Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
add util method to extract table rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Jun 1, 2012
1 parent df2001d commit 6bea2c3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 17 deletions.
Expand Up @@ -57,7 +57,7 @@ public AddLanguagePage enableLanguageByDefault()

public ManageLanguagePage saveLanguage()
{
saveButton.click();
clickSaveAndCheckErrors(saveButton);
return new ManageLanguagePage(getDriver());
}
}
22 changes: 19 additions & 3 deletions functional-test/src/main/java/org/zanata/page/ProjectsPage.java
Expand Up @@ -27,9 +27,13 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.zanata.util.TableRow;
import org.zanata.util.WebElementUtil;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;

public class ProjectsPage extends AbstractPage
{
Expand Down Expand Up @@ -69,8 +73,20 @@ public List<String> getProjectNamesOnCurrentPage()
{
return Collections.emptyList();
}
WebElement table = getDriver().findElement(By.className("rich-table"));
List<WebElement> rows = table.findElements(By.xpath(".//tbody/tr/td[1]"));
return WebElementUtil.elementsToText(rows);

List<TableRow> tableRows = WebElementUtil.getTableRows(getDriver(), By.className("rich-table"));

return ImmutableList.copyOf(Collections2.transform(tableRows, new Function<TableRow, String>()
{
@Override
public String apply(TableRow from)
{
// first column is name
return from.getCellContents().get(0);
}
}));
// WebElement table = getDriver().findElement(By.className("rich-table"));
// List<WebElement> rows = table.findElements(By.xpath(".//tbody/tr/td[1]"));
// return WebElementUtil.elementsToText(rows);
}
}
36 changes: 36 additions & 0 deletions functional-test/src/main/java/org/zanata/util/TableRow.java
@@ -0,0 +1,36 @@
package org.zanata.util;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class TableRow
{
private final WebElement row;

public TableRow(WebElement row)
{
this.row = row;
}

public List<WebElement> getCells()
{
return row.findElements(By.xpath(".//td"));
}

public List<String> getCellContents()
{
return WebElementUtil.elementsToText(getCells());
}

@Override
public String toString()
{
return row.toString();
}

}
49 changes: 36 additions & 13 deletions functional-test/src/main/java/org/zanata/util/WebElementUtil.java
Expand Up @@ -23,11 +23,13 @@
import java.util.Collection;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;

Expand All @@ -37,18 +39,9 @@ private WebElementUtil()
{
}

private static final Function<WebElement, String> ELEMENT_TO_TEXT_FUNCTION = new Function<WebElement, String>()
{
@Override
public String apply(WebElement from)
{
return from.getText();
}
};

public static List<String> elementsToText(Collection<WebElement> webElements)
{
return ImmutableList.copyOf(Collections2.transform(webElements, ELEMENT_TO_TEXT_FUNCTION));
return ImmutableList.copyOf(Collections2.transform(webElements, WebElementToTextFunction.FUNCTION));
}

public static String getInnerHTML(WebDriver driver, WebElement element)
Expand All @@ -58,14 +51,23 @@ public static String getInnerHTML(WebDriver driver, WebElement element)

public static List<String> elementsToInnerHTML(WebDriver driver, Collection<WebElement> webElements)
{
return ImmutableList.copyOf(Collections2.transform(webElements, new WebElementInnerHTMLFunction(driver)));
return ImmutableList.copyOf(Collections2.transform(webElements, new WebElementToInnerHTMLFunction(driver)));
}

static class WebElementInnerHTMLFunction implements Function<WebElement, String>
public static List<TableRow> getTableRows(WebDriver driver, By byQueryForTable)
{
WebElement table = driver.findElement(byQueryForTable);
Preconditions.checkArgument(table.getTagName().equalsIgnoreCase("table"), "argument must be a table");

List<WebElement> rows = table.findElements(By.xpath(".//tbody/tr"));
return ImmutableList.copyOf(Collections2.transform(rows, WebElementTableRowFunction.FUNCTION));
}

private static class WebElementToInnerHTMLFunction implements Function<WebElement, String>
{
private final WebDriver driver;

private WebElementInnerHTMLFunction(WebDriver driver)
public WebElementToInnerHTMLFunction(WebDriver driver)
{
this.driver = driver;
}
Expand All @@ -77,4 +79,25 @@ public String apply(WebElement from)
}

}

private static enum WebElementTableRowFunction implements Function<WebElement, TableRow>
{
FUNCTION;
@Override
public TableRow apply(WebElement element)
{
return new TableRow(element);
}
}

public static enum WebElementToTextFunction implements Function<WebElement, String>
{
FUNCTION;

@Override
public String apply(WebElement from)
{
return from.getText();
}
}
}

0 comments on commit 6bea2c3

Please sign in to comment.