aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-tests/src/test/java/util/selenium/LazyDomElement.java
blob: 6f5eca2de142b973ced3c61dd1fc011bec8a7e43 (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
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package util.selenium;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import javax.annotation.Nullable;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

class LazyDomElement {
  private final SeleniumDriver driver;
  private final By selector;
  private final ElementFilter filter;
  private final Retry retry;

  LazyDomElement(SeleniumDriver driver, By selector) {
    this(driver, selector, Retry._30_SECONDS);
  }

  LazyDomElement(SeleniumDriver driver, By selector, Retry retry) {
    this(driver, selector, ElementFilter.any(), retry);
  }

  private LazyDomElement(SeleniumDriver driver, By selector, ElementFilter filter, Retry retry) {
    this.driver = driver;
    this.selector = selector;
    this.filter = filter;
    this.retry = retry;
  }

  public LazyDomElement withText(final String text) {
    String fullDescription = " with text [" + text + "]";

    return with(new ElementFilter(fullDescription, new Function<Collection<WebElement>, Collection<WebElement>>() {
      @Override
      public Collection<WebElement> apply(Collection<WebElement> stream) {
        return FluentIterable.from(stream).filter(new Predicate<WebElement>() {
          @Override
          public boolean apply(@Nullable WebElement element) {
//            return Objects.equals(element.getText(), text);
            return element.getText().contains(text);
          }
        }).toList();
      }
    }));
  }

  public LazyShould should() {
    return new LazyShould(this, Retry._30_SECONDS, true);
  }

  public void fill(final CharSequence text) {
    execute("fill(" + text + ")", new Consumer<WebElement>() {
      @Override
      public void accept(WebElement element) {
        element.clear();
        element.sendKeys(text);
      }
    });
  }

  public void pressEnter() {
    execute("pressEnter", new Consumer<WebElement>() {
      @Override
      public void accept(WebElement element) {
        element.sendKeys(Keys.ENTER);
      }
    });
  }

  public void select(final String text) {
    executeSelect("select(" + text + ")", new Consumer<Select>() {
      @Override
      public void accept(Select select) {
        select.selectByVisibleText(text);
      }
    });
  }

  public void executeSelect(String description, final Consumer<Select> selectOnElement) {
    execute(description, new Consumer<WebElement>() {
      @Override
      public void accept(WebElement element) {
        selectOnElement.accept(new Select(element));
      }
    });
  }

  public void click() {
    execute("click", new Consumer<WebElement>() {
      @Override
      public void accept(WebElement element) {
        element.click();
      }
    });
  }

  public void check() {
    execute("check", new Consumer<WebElement>() {
      @Override
      public void accept(WebElement element) {
        if (!element.isSelected()) {
          element.click();
        }
      }
    });
  }

  public void execute(Consumer<WebElement> action) {
    execute("execute(" + action + ")", action);
  }

  private LazyDomElement with(ElementFilter filter) {
    return new LazyDomElement(driver, selector, this.filter.and(filter), retry);
  }

  private void execute(String message, Consumer<WebElement> action) {
    System.out.println(" - " + Text.toString(selector) + filter.getDescription() + "." + message);

    Supplier<Optional<WebElement>> findOne = new Supplier<Optional<WebElement>>() {
      @Override
      public Optional<WebElement> get() {
        List<WebElement> elements = stream();
        if (elements.isEmpty()) {
          return Optional.empty();
        }
        return Optional.of(elements.get(0));
      }
    };

    try {
      retry.execute(findOne, action);
    } catch (NoSuchElementException e) {
      throw new AssertionError("Element not found: " + Text.toString(selector));
    }
  }

  List<WebElement> stream() {
    return FluentIterable.from(filter.getFilter().apply(driver.findElements(selector))).toList();
  }

  @Override
  public String toString() {
    return Text.toString(selector) + filter.getDescription();
  }
}