Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional parameter source to Get Table Cell Value Keyword #71

Merged
merged 1 commit into from
Jul 29, 2015
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.robotframework.swing.keyword.table;

import java.awt.*;
import java.util.Map;

import org.junit.Assert;
Expand All @@ -29,9 +30,11 @@
import org.robotframework.swing.common.IdentifierSupport;
import org.robotframework.swing.comparator.EqualsStringComparator;
import org.robotframework.swing.factory.OperatorFactory;
import org.robotframework.swing.table.CellValueExtractor;
import org.robotframework.swing.table.TableOperator;
import org.robotframework.swing.table.TableOperatorFactory;


@RobotKeywords
public class TableKeywords extends IdentifierSupport {
private final OperatorFactory<TableOperator> operatorFactory = new TableOperatorFactory();
Expand Down Expand Up @@ -113,22 +116,49 @@ public void tableCellShouldNotBeSelected(String identifier, String row, String c
}

@RobotKeyword("Returns cell's value from a table.\n\n"
+ "Starting from SwingLibrary 1.1.4, value from cell rendered with check box is string true/false.\n\n"
+ "Starting from SwingLibrary 1.1.4, value from cell rendered with check box is string true/false.\n"
+ "Optional parameter _source_ allows to override text extraction strategy. "
+ "Available values are _auto_ (default, will try to get text from cell component"
+ "first and then from table model) and _model_ (will only try to get text from table model).\n\n"
+ "Example:\n"
+ "| ${cellValue}= | Get Table Cell Value | _myTable_ | _0_ | _2_ |\n"
+ "| Should Be Equal | _tuesday_ | | _${cellValue}_ | |\n")
@ArgumentNames({"identifier", "row", "columnIdentifier"})
@ArgumentNames({"identifier", "row", "columnIdentifier", "source=auto"})
public String getTableCellValue(String identifier, String row, String columnIdentifier, String source) {
TableOperator operator = createTableOperator(identifier);
Component component = operator.getSource();
CellValueExtractor.TextSource textSource = textExtractionSourceFromText(source);
return operator.getCellValue(row, columnIdentifier, textSource);
}

private CellValueExtractor.TextSource textExtractionSourceFromText(String text) {
if (text.toLowerCase().equals("model"))
return CellValueExtractor.TextSource.MODEL;
else
return CellValueExtractor.TextSource.AUTO;
}

@RobotKeywordOverload
public String getTableCellValue(String identifier, String row, String columnIdentifier) {
return createTableOperator(identifier).getCellValue(row, columnIdentifier).toString();
return getTableCellValue(identifier, row, columnIdentifier, "auto");
}

@RobotKeyword("Returns selected cell's value from a table.\n\n"
+ "Optional parameter _source_ allows to override text extraction strategy. "
+ "Available values are _auto_ (default, will try to get text from cell component"
+ "first and then from table model) and _model_ (will only try to get text from table model).\n\n"
+ "Example:\n"
+ "| ${cellValue}= | Get Selected Table Cell Value | _myTable_ |\n"
+ "| Should Be Equal | _tuesday_ | _${cellValue}_ |\n")
@ArgumentNames({"identifier"})
@ArgumentNames({"identifier", "source=auto"})
public Object getSelectedTableCellValue(String identifier, String source) {
CellValueExtractor.TextSource textSource = textExtractionSourceFromText(source);
return createTableOperator(identifier).getSelectedCellValue(textSource).toString();
}

@RobotKeywordOverload
public Object getSelectedTableCellValue(String identifier) {
return createTableOperator(identifier).getSelectedCellValue().toString();
return getSelectedTableCellValue(identifier, "auto");
}

@RobotKeyword("Sets cell value in a table.\n\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,44 @@
import org.robotframework.swing.common.SmoothInvoker;

public class CellValueExtractor {
public enum TextSource {AUTO, MODEL}

private JTableOperator jTableOperator;

public CellValueExtractor(JTableOperator jTableOperator) {
this.jTableOperator = jTableOperator;
}

public String textOf(int row, int col, TextSource source) {
if (source == TextSource.MODEL)
return getTextFromTableModel(row, col);
else
return getTextWithDefaultStrategy(row, col);
}

public String textOf(int row, int col) {
return textOf(row, col, TextSource.AUTO);
}

public String getTextWithDefaultStrategy(int row, int col) {
try {
Component cellRendererComponent = getCellRendererComponent(row, col);
if (isButtonBasedRenderer(cellRendererComponent))
return new Boolean(((AbstractButton) cellRendererComponent).isSelected()).toString();
return coerceToWithText(cellRendererComponent).getText();
return getTextFromCellComponent(row, col);
} catch (AllMethodsNotImplementedException e) {
return wrapElementToWithText(row, col).getText();
return getTextFromTableModel(row, col);
}
}

private String getTextFromTableModel(int row, int col) {
return wrapElementToWithText(row, col).getText();
}

private String getTextFromCellComponent(int row, int col) {
Component cellRendererComponent = getCellRendererComponent(row, col);
if (isButtonBasedRenderer(cellRendererComponent))
return new Boolean(((AbstractButton) cellRendererComponent).isSelected()).toString();
return coerceToWithText(cellRendererComponent).getText();
}

public Component getCellRendererComponent(int row, int column) {
TableCellRenderer renderer = jTableOperator.getCellRenderer(row, column);
JTable table = (JTable) jTableOperator.getSource();
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/robotframework/swing/table/TableOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public TableHeaderOperator headerOperator() {
return new TableHeaderOperator(jTableOperator.getHeaderOperator());
}

public String getCellValue(String row, String columnIdentifier) {
public String getCellValue(String row, String columnIdentifier, CellValueExtractor.TextSource source) {
Point cell = findCell(row, columnIdentifier);
return cellValueExtractor.textOf(cell.y, cell.x);
return cellValueExtractor.textOf(cell.y, cell.x, source);
}

public boolean isCellEditable(String row, String columnIdentifier) {
Expand Down Expand Up @@ -144,10 +144,14 @@ public int findCellRow(String text, String columnIdentifier) {
return jTableOperator.findCellRow(text, col, 0);
}

public Object getSelectedCellValue() {
public Object getSelectedCellValue(CellValueExtractor.TextSource source) {
int selectedRow = jTableOperator.getSelectedRow();
int selectedColumn = jTableOperator.getSelectedColumn();
return cellValueExtractor.textOf(selectedRow, selectedColumn);
return cellValueExtractor.textOf(selectedRow, selectedColumn, source);
}

public Object getSelectedCellValue() {
return getSelectedCellValue(CellValueExtractor.TextSource.AUTO);
}

public void callPopupMenuItemOnSelectedCells(String menuPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private Object[][] getTestTableData() {
{"column one", "one/one", "two/one", "three/one", "four/one"},
{"column two", "one/two", "two/two", "three/two", "four/two"},
{"column three", "one/three", "two/three", "three/three",
"four/three"},
"four/three!!!"},
{"column four", Boolean.TRUE, Boolean.TRUE, Boolean.FALSE,
Boolean.FALSE}};
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/robotframework/swing/testapp/TestTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
Expand Down Expand Up @@ -65,6 +66,9 @@ public void mouseClicked(MouseEvent e) {
}
});

TableColumn col2 = table.getColumnModel().getColumn(2);
col2.setCellRenderer(new CustomRenderer());

return table;
}

Expand All @@ -80,6 +84,16 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
}
}

static class CustomRenderer extends DefaultTableCellRenderer {
public void setValue(Object value) {
String s = value.toString();
if (s.equals("four/three!!!"))
setText("four/three");
else
setText(s);
}
}

public static class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.robotframework.jdave.mock.MockSupportSpecification;
import org.robotframework.swing.comparator.EqualsStringComparator;
import org.robotframework.swing.factory.OperatorFactory;
import org.robotframework.swing.table.CellValueExtractor;
import org.robotframework.swing.table.TableOperator;


Expand Down Expand Up @@ -128,7 +129,7 @@ public TableKeywords create() {

public void getsSelectedTableCellValue() {
checking(new Expectations() {{
one(tableOperator).getSelectedCellValue();
one(tableOperator).getSelectedCellValue(CellValueExtractor.TextSource.AUTO);
will(returnValue(cellValue));
}});

Expand Down
17 changes: 13 additions & 4 deletions src/test/resources/robot-tests/tablekeywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@ Get Selected Table Cell Value
${cellValue}= getSelectedTableCellValue ${tableName}
shouldBeEqual four/three ${cellValue}

Get Selected Table Cell Value From Model
selectTableCell ${tableName} 3 2
${cellValue}= getSelectedTableCellValue ${tableName} source=MODEL
shouldBeEqual four/three!!! ${cellValue}

Get Table Cell Value By Index
${cellValue}= getTableCellValue ${tableName} 2 1
shouldBeEqual three/two ${cellValue}
${cellValue}= getTableCellValue ${tableName} 3 2
shouldBeEqual four/three ${cellValue}

Get Table Cell Value From Model By Index
${cellValue}= getTableCellValue ${tableName} 3 2 source=MODEL
shouldBeEqual four/three!!! ${cellValue}

Clear Table Cell
clearTableCell ${tableName} 2 1
Expand Down Expand Up @@ -131,13 +140,13 @@ Get Table Row Count By Name
Find Table Row
${row}= findTableRow ${tableName} one/one
shouldBeEqualAsIntegers 0 ${row}
${row}= findTableRow ${tableName} four/three
${row}= findTableRow ${tableName} four/three!!!
shouldBeEqualAsIntegers 3 ${row}

Find Table Row With Column
${row}= findTableRow ${tableName} one/one column one
shouldBeEqualAsIntegers 0 ${row}
${row}= findTableRow ${tableName} four/three column three
${row}= findTableRow ${tableName} four/three!!! column three
shouldBeEqualAsIntegers 3 ${row}
${row}= findTableRow TableWithSingleValue foo
shouldBeEqualAsIntegers 0 ${row}
Expand Down