aboutsummaryrefslogtreecommitdiffstats
path: root/testbench-api/src/main/java/com/vaadin/testbench/elements/GridElement.java
blob: 945678ac2d4545e6c1d0b1a55b63ee8de157e488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 * Copyright 2000-2016 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.testbench.elements;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.By;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elementsbase.AbstractElement;
import com.vaadin.testbench.elementsbase.ServerClass;

/**
 * TestBench Element API for Grid
 *
 * @author Vaadin Ltd
 */
@ServerClass("com.vaadin.ui.Grid")
public class GridElement extends AbstractComponentElement {

    public static class GridCellElement extends AbstractElement {

        private static final String FOCUSED_CELL_CLASS_NAME = "-cell-focused";
        private static final String FROZEN_CLASS_NAME = "frozen";

        public boolean isFocused() {
            return getAttribute("class").contains(FOCUSED_CELL_CLASS_NAME);
        }

        public boolean isFrozen() {
            return getAttribute("class").contains(FROZEN_CLASS_NAME);
        }
    }

    public static class GridRowElement extends AbstractElement {

        private static final String FOCUSED_CLASS_NAME = "-row-focused";
        private static final String SELECTED_CLASS_NAME = "-row-selected";

        public boolean isFocused() {
            return getAttribute("class").contains(FOCUSED_CLASS_NAME);
        }

        @Override
        public boolean isSelected() {
            return getAttribute("class").contains(SELECTED_CLASS_NAME);
        }

        public GridCellElement getCell(int columnIndex) {
            TestBenchElement e = (TestBenchElement) findElement(
                    By.xpath("./td[" + (columnIndex + 1) + "]"));
            return e.wrap(GridCellElement.class);
        }
    }

    public static class GridEditorElement extends AbstractElement {

        private GridElement grid;

        private GridEditorElement setGrid(GridElement grid) {
            this.grid = grid;
            return this;
        }

        /**
         * Gets the editor field for column in given index.
         *
         * @param colIndex
         *            the column index
         * @return the editor field for given location
         *
         * @throws NoSuchElementException
         *             if {@code isEditable(colIndex) == false}
         */
        public TestBenchElement getField(int colIndex) {
            return grid.getSubPart("#editor[" + colIndex + "]");
        }

        /**
         * Gets whether the column with the given index is editable, that is,
         * has an associated editor field.
         *
         * @param colIndex
         *            the column index
         * @return {@code true} if the column has an editor field, {@code false}
         *         otherwise
         */
        public boolean isEditable(int colIndex) {
            return grid
                    .isElementPresent(By.vaadin("#editor[" + colIndex + "]"));
        }

        /**
         * Checks whether a field is marked with an error.
         *
         * @param colIndex
         *            column index
         * @return <code>true</code> iff the field is marked with an error
         */
        public boolean isFieldErrorMarked(int colIndex) {
            return getField(colIndex).getAttribute("class").contains("error");
        }

        /**
         * Saves the fields of this editor.
         * <p>
         * <em>Note:</em> that this closes the editor making this element
         * useless.
         */
        public void save() {
            findElement(By.className("v-grid-editor-save")).click();
        }

        /**
         * Cancels this editor.
         * <p>
         * <em>Note:</em> that this closes the editor making this element
         * useless.
         */
        public void cancel() {
            findElement(By.className("v-grid-editor-cancel")).click();
        }

        /**
         * Gets the error message text, or <code>null</code> if no message is
         * present.
         */
        public String getErrorMessage() {
            WebElement messageWrapper = findElement(
                    By.className("v-grid-editor-message"));
            List<WebElement> divs = messageWrapper
                    .findElements(By.tagName("div"));
            if (divs.isEmpty()) {
                return null;
            } else {
                return divs.get(0).getText();
            }
        }
    }

    /**
     * Scrolls Grid element so that wanted row is displayed
     *
     * @param index
     *            Target row
     */
    public void scrollToRow(int index) {
        try {
            getSubPart("#cell[" + index + "]");
        } catch (NoSuchElementException e) {
            // Expected, ignore it.
        }
    }

    /**
     * Gets cell element with given row and column index.
     *
     * @param rowIndex
     *            Row index
     * @param colIndex
     *            Column index
     * @return Cell element with given indices.
     */
    public GridCellElement getCell(int rowIndex, int colIndex) {
        scrollToRow(rowIndex);
        return getSubPart("#cell[" + rowIndex + "][" + colIndex + "]")
                .wrap(GridCellElement.class);
    }

    /**
     * Gets row element with given row index.
     *
     * @param index
     *            Row index
     * @return Row element with given index.
     */
    public GridRowElement getRow(int index) {
        scrollToRow(index);
        return getSubPart("#cell[" + index + "]").wrap(GridRowElement.class);
    }

    /**
     * Gets header cell element with given row and column index.
     *
     * @param rowIndex
     *            Row index
     * @param colIndex
     *            Column index
     * @return Header cell element with given indices.
     */
    public GridCellElement getHeaderCell(int rowIndex, int colIndex) {
        return getSubPart("#header[" + rowIndex + "][" + colIndex + "]")
                .wrap(GridCellElement.class);
    }

    /**
     * Finds the header cell element with the given caption. If there are
     * multiple headers with the same name, the first one is returned.
     *
     * @param caption
     *            The header caption
     * @return The first header cell element with a given caption.
     * @throws NoSuchElementException
     *             if there is no header row or no header cell with the given
     *             text.
     */
    public GridCellElement getHeaderCellByCaption(String caption) {
        List<WebElement> headerRows = findElement(By.vaadin("#header"))
                .findElements(By.xpath("./tr/th"));
        for (WebElement header : headerRows) {
            if (caption.equals(header.getText())) {
                return TestBenchElement
                        .wrapElement(header, getCommandExecutor())
                        .wrap(GridCellElement.class);
            }
        }
        String errorMessage = String
                .format("There is no header cell with %s caption. ", caption);
        throw new NoSuchElementException(errorMessage);
    }

    /**
     * Gets the header cell element with the given caption in the given header
     * row. If there are multiple headers with the same name, the first one is
     * returned.
     *
     * @param rowIndex
     *            The index of the header row
     * @param caption
     *            The header caption
     * @return The first header cell element with a given caption.
     * @throws NoSuchElementException
     *             if there is no header row or no header cell with the given
     *             text.
     */
    public GridCellElement getHeaderCellByCaption(int rowIndex,
            String caption) {
        List<GridCellElement> headerCells = getHeaderCells(rowIndex);
        for (GridCellElement cell : headerCells) {
            if (caption.equals(cell.getText())) {
                return cell;
            }
        }
        String errorMessage = String.format(
                "The row with index %d does not have header with %s caption. ",
                rowIndex, caption);
        throw new NoSuchElementException(errorMessage);
    }

    /**
     * Gets footer cell element with given row and column index.
     *
     * @param rowIndex
     *            Row index
     * @param colIndex
     *            Column index
     * @return Footer cell element with given indices.
     */
    public GridCellElement getFooterCell(int rowIndex, int colIndex) {
        return getSubPart("#footer[" + rowIndex + "][" + colIndex + "]")
                .wrap(GridCellElement.class);
    }

    /**
     * Gets list of header cell elements on given row.
     *
     * @param rowIndex
     *            Row index
     * @return Header cell elements on given row.
     */
    public List<GridCellElement> getHeaderCells(int rowIndex) {
        List<GridCellElement> headers = new ArrayList<GridCellElement>();
        for (TestBenchElement e : TestBenchElement.wrapElements(
                getSubPart("#header[" + rowIndex + "]").findElements(
                        By.xpath("./th")),
                getCommandExecutor())) {
            headers.add(e.wrap(GridCellElement.class));
        }
        return headers;
    }

    /**
     * Gets list of header cell elements on given row.
     *
     * @param rowIndex
     *            Row index
     * @return Header cell elements on given row.
     */
    public List<GridCellElement> getFooterCells(int rowIndex) {
        List<GridCellElement> footers = new ArrayList<GridCellElement>();
        for (TestBenchElement e : TestBenchElement.wrapElements(
                getSubPart("#footer[" + rowIndex + "]").findElements(
                        By.xpath("./td")),
                getCommandExecutor())) {
            footers.add(e.wrap(GridCellElement.class));
        }
        return footers;
    }

    /**
     * Get header row count
     *
     * @return Header row count
     */
    public int getHeaderCount() {
        return getSubPart("#header").findElements(By.xpath("./tr")).size();
    }

    /**
     * Get footer row count
     *
     * @return Footer row count
     */
    public int getFooterCount() {
        return getSubPart("#footer").findElements(By.xpath("./tr")).size();
    }

    /**
     * Get a header row by index
     *
     * @param rowIndex
     *            Row index
     * @return The th element of the row
     */
    public TestBenchElement getHeaderRow(int rowIndex) {
        return getSubPart("#header[" + rowIndex + "]");
    }

    /**
     * Get a footer row by index
     *
     * @param rowIndex
     *            Row index
     * @return The tr element of the row
     */
    public TestBenchElement getFooterRow(int rowIndex) {
        return getSubPart("#footer[" + rowIndex + "]");
    }

    /**
     * Get the vertical scroll element
     *
     * @return The element representing the vertical scrollbar
     */
    public TestBenchElement getVerticalScroller() {
        List<WebElement> rootElements = findElements(By.xpath("./div"));
        return (TestBenchElement) rootElements.get(0);
    }

    /**
     * Get the horizontal scroll element
     *
     * @return The element representing the horizontal scrollbar
     */
    public TestBenchElement getHorizontalScroller() {
        List<WebElement> rootElements = findElements(By.xpath("./div"));
        return (TestBenchElement) rootElements.get(1);
    }

    /**
     * Get the header element
     *
     * @return The thead element
     */
    public TestBenchElement getHeader() {
        return getSubPart("#header");
    }

    /**
     * Get the body element
     *
     * @return the tbody element
     */
    public TestBenchElement getBody() {
        return getSubPart("#cell");
    }

    /**
     * Get the footer element
     *
     * @return the tfoot element
     */
    public TestBenchElement getFooter() {
        return getSubPart("#footer");
    }

    /**
     * Get the element wrapping the table element
     *
     * @return The element that wraps the table element
     */
    public TestBenchElement getTableWrapper() {
        List<WebElement> rootElements = findElements(By.xpath("./div"));
        return (TestBenchElement) rootElements.get(2);
    }

    public GridEditorElement getEditor() {
        return getSubPart("#editor").wrap(GridEditorElement.class)
                .setGrid(this);
    }

    /**
     * Helper function to get Grid subparts wrapped correctly
     *
     * @param subPartSelector
     *            SubPart to be used in ComponentLocator
     * @return SubPart element wrapped in TestBenchElement class
     */
    private TestBenchElement getSubPart(String subPartSelector) {
        return (TestBenchElement) findElement(By.vaadin(subPartSelector));
    }

    /**
     * Gets the element that contains the details of a row.
     *
     * @since
     * @param rowIndex
     *            the index of the row for the details
     * @return the element that contains the details of a row. <code>null</code>
     *         if no widget is defined for the details row
     * @throws NoSuchElementException
     *             if the given details row is currently not open
     */
    public TestBenchElement getDetails(int rowIndex)
            throws NoSuchElementException {
        return getSubPart("#details[" + rowIndex + "]");
    }

    public void toggleColumnHidden(String toggleCaption) {
        if (!isElementPresent(By.className("v-grid-sidebar-content"))) {
            // Open sidebar menu
            WebElement sidebarButton = findElement(
                    By.className("v-grid-sidebar"))
                            .findElement(By.tagName("button"));
            sidebarButton.click();
        }
        Optional<WebElement> toggleButton = getDriver()
                .findElement(By.className("v-grid-sidebar-content"))
                .findElements(By.className("column-hiding-toggle")).stream()
                .filter(e -> e.getText().equals(toggleCaption)).findAny();
        if (toggleButton.isPresent()) {
            toggleButton.ifPresent(e -> e.click());
        } else {
            throw new IllegalArgumentException(
                    "No column hiding toggle with caption '" + toggleCaption
                            + "'");
        }
    }

    /**
     * Gets the total number of data rows in the grid.
     *
     * @return the number of data rows in the grid,
     */
    public long getRowCount() {
        Long res = (Long) getCommandExecutor()
                .executeScript("return arguments[0].getBodyRowCount()", this);
        if (res == null) {
            throw new IllegalStateException("getBodyRowCount returned null");
        }

        return res.longValue();
    }

    /**
     * Gets all the data rows in the grid.
     * <p>
     * Returns an iterable which will lazily scroll rows into views and lazy
     * load data as needed.
     *
     * @return an iterable of all the data rows in the grid.
     */
    public Iterable<GridRowElement> getRows() {
        return new Iterable<GridElement.GridRowElement>() {
            @Override
            public Iterator<GridRowElement> iterator() {
                return new Iterator<GridElement.GridRowElement>() {
                    int nextIndex = 0;

                    @Override
                    public GridRowElement next() {
                        return getRow(nextIndex++);
                    }

                    @Override
                    public boolean hasNext() {
                        try {
                            getRow(nextIndex);
                            return true;
                        } catch (Exception e) {
                            return false;
                        }
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException(
                                "remove not supported");
                    }

                };
            }
        };
    }
}