View Javadoc

1   package br.com.caelum.seleniumdsl.table;
2   
3   import br.com.caelum.seleniumdsl.search.RowMatcher;
4   import br.com.caelum.seleniumdsl.search.RowVisitor;
5   import br.com.caelum.seleniumdsl.search.TableCriteria;
6   import br.com.caelum.seleniumdsl.table.layout.TableLayout;
7   import br.com.caelum.seleniumdsl.table.layout.TableLayoutChooser;
8   
9   import com.thoughtworks.selenium.Selenium;
10  import com.thoughtworks.selenium.SeleniumException;
11  
12  public class DefaultTable implements Table {
13  	private final Selenium selenium;
14  
15  	private final String id;
16  
17  	/**
18  	 * The property that holds this table's key, usually "id"
19  	 */
20  	private final String type;
21  
22  	private final TableLayout layout;
23  
24  	public DefaultTable(Selenium selenium, String id) {
25  		this(selenium, id, "id");
26  	}
27  
28  	public DefaultTable(Selenium selenium, String value, String type) {
29  		this.selenium = selenium;
30  		this.id = value;
31  		this.type = type;
32  
33  		layout = new TableLayoutChooser(selenium, value, type).choose();
34  	}
35  
36  	public String getType() {
37  		return type;
38  	}
39  
40  	public Column column(int columnIndex) {
41  		return new DefaultColumn(this, selenium, columnIndex);
42  	}
43  
44  	public Column column(String columnName) {
45  		return column(findColumn(columnName));
46  	}
47  
48  	public int getColCount() {
49  		return layout.getColCount();
50  	}
51  
52  	public int getRowCount() {
53  		return layout.getRowCount();
54  	}
55  
56  	public int getContentCount() {
57  		return layout.getContentCount();
58  	}
59  
60  	public Cell cell(int row, int col) {
61  		return new DefaultCell(selenium, this, row, col);
62  	}
63  
64  	public Cell cell(int row, String col) {
65  		return new DefaultCell(selenium, this, row, findColumn(col));
66  	}
67  
68  	public String getId() {
69  		return id;
70  	}
71  
72  	public boolean exists() {
73  		return selenium.isElementPresent(getId());
74  	}
75  
76  	public void iterate(RowVisitor visitor) {
77  		int count = getRowCount();
78  		for (int row = 1; row <= count; row++)
79  			visitor.visit(new DefaultRow(this, selenium, row));
80  	}
81  
82  	public Row header() {
83  		return row(1);
84  	}
85  
86  	public Row row(Integer row) {
87  		return new DefaultRow(this, selenium, row);
88  	}
89  
90  	public Integer findColumn(String columnName) {
91  		Row row = new DefaultRow(this, selenium, 1);
92  		int colCount = getColCount();
93  		for (int i = 0; i < colCount; i++) {
94  			String current;
95  			try {
96  				current = row.cell(i + 1)
97  						.headerValue();
98  			} catch (SeleniumException e) {
99  				current = row.cell(i + 1)
100 						.value();
101 			}
102 			if (columnName.equals(current))
103 				return i + 1;
104 		}
105 		throw new IllegalArgumentException("Column " + columnName + " not found");
106 	}
107 
108 	public RowMatcher select(RowMatcher matcher) {
109 		matcher.setTable(this);
110 		return matcher;
111 	}
112 
113 	public TableCriteria createCriteria() {
114 		return new TableCriteria(this);
115 	}
116 
117 	public TableLayout getLayout() {
118 		return layout;
119 	}
120 
121 	public boolean contains(String col, String content) {
122 		return layout.contains(this, col, content);
123 	}
124 
125 }