This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
EditorPage.java
223 lines (203 loc) · 8.41 KB
/
EditorPage.java
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
/*
* Copyright 2014, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This 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 2.1 of the License, or (at your option)
* any later version.
*
* This software 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 software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.page.webtrans;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.zanata.page.BasePage;
import org.zanata.util.WebElementUtil;
import java.util.Collections;
import java.util.List;
/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Slf4j
public class EditorPage extends BasePage {
// first %d is row index, second %d is plural form index (i.e. 0 or 1)
private static final String SOURCE_ID_FMT =
"gwt-debug-%d-source-panel-%d-container";
private static final int SINGULAR = 0;
// first %d is row index, second %d is plural form index (i.e. 0-6)
private static final String TARGET_ID_FMT = "gwt-debug-%d-target-%d";
private final By glossaryTableBy = By.id("gwt-debug-glossaryResultTable");
private final By glossaryNoResultBy = By.id("gwt-debug-glossaryNoResult");
@FindBy(id = "gwt-debug-transUnitTable")
private WebElement transUnitTable;
public EditorPage(WebDriver driver) {
super(driver);
}
public EditorPage searchGlossary(final String term) {
waitForTenSec().until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
return input.findElements(glossaryNoResultBy).size() == 1
|| input.findElements(glossaryTableBy).size() == 1;
}
});
WebElement searchBox =
getDriver().findElement(By.id("gwt-debug-glossaryTextBox"));
searchBox.clear();
searchBox.sendKeys(term);
WebElement searchButton =
getDriver()
.findElement(By.id("gwt-debug-glossarySearchButton"));
searchButton.click();
return this;
}
/**
* First row is header: SourceTerm, TargetTerm, Action, Details.
*
* @return a table representing the searchResultTable
*/
public List<List<String>> getGlossaryResultTable() {
return waitForTenSec().until(
new Function<WebDriver, List<List<String>>>() {
@Override
public List<List<String>> apply(WebDriver input) {
if (input.findElements(glossaryNoResultBy).size() == 1) {
return Collections.emptyList();
}
List<List<String>> resultTable =
WebElementUtil.getTwoDimensionList(input,
glossaryTableBy);
log.info("glossary result: {}", resultTable);
return resultTable;
}
});
}
/**
* Get content of a text flow source at given row.
* This assumes the text flow has singular form (i.e. no plural).
* If a test requires to access plural content, this can be changed.
*
* @param rowIndex
* row index
* @return content of the source
*/
public String getTranslationSourceAtRowIndex(final int rowIndex) {
return getCodeMirrorContent(rowIndex, SOURCE_ID_FMT, SINGULAR);
}
/**
* Get content of a text flow target at given row.
* This assumes the text flow has singular form (i.e. no plural).
* If a test requires to access plural content, this can be changed.
*
* @param rowIndex
* row index
* @return content of the target
*/
public String getTranslationTargetAtRowIndex(final int rowIndex) {
return getCodeMirrorContent(rowIndex, TARGET_ID_FMT, SINGULAR);
}
private String getCodeMirrorContent(final long rowIndex,
final String idFormat, final int pluralIndex) {
return waitForTenSec().until(new Function<WebDriver, String>() {
@Override
public String apply(WebDriver input) {
// code mirror will turn text into list of <pre>.
List<WebElement> cmTextLines = input.findElement(
By.id(String.format(idFormat, rowIndex, pluralIndex)))
.findElements(By.tagName("pre"));
List<String> contents =
WebElementUtil.elementsToText(cmTextLines);
return Joiner.on("\n").skipNulls().join(contents);
}
});
}
public EditorPage setSyntaxHighlighting(boolean option) {
openConfigurationPanel();
if (getDriver().findElement(By.id("gwt-uid-144")).isSelected() != option) {
getDriver().findElement(By.id("gwt-uid-144")).click();
}
return new EditorPage(getDriver());
}
private Boolean openConfigurationPanel() {
getDriver().findElement(By.className("icon-cog")).click();
return waitForTenSec().until(new Function<WebDriver, Boolean>() {
@Override
public Boolean apply(WebDriver input) {
// code mirror will turn text into list of <pre>.
return input.findElement(By.className("gwt-TabLayoutPanelContentContainer")).isDisplayed();
}
});
}
public String getBasicTranslationTargetAtRowIndex(final int rowIndex) {
return getContentAtRowIndex(rowIndex, TARGET_ID_FMT, SINGULAR);
}
/**
* Get content from a target using the non-CodeMirror configuration
* @param rowIndex
* @return row target content
*/
private String getContentAtRowIndex(final long rowIndex,
final String idFormat,
final int pluralIndex) {
return waitForTenSec().until(new Function<WebDriver, String>() {
@Override
public String apply(WebDriver input) {
return input.findElement(By.id(String.format(idFormat, rowIndex, pluralIndex))).getAttribute("value");
}
});
}
/**
* Translate a target using the non-CodeMirror field
* @param rowIndex
* @param text
* @return updated EditorPage
*/
public EditorPage translateTargetAtRowIndex(final int rowIndex, String text) {
setTargetContent(rowIndex, text, TARGET_ID_FMT, SINGULAR);
return new EditorPage(getDriver());
}
private void setTargetContent(final long rowIndex, final String text,
final String idFormat, final int pluralIndex) {
WebElement we = waitForTenSec().until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver input) {
return input.findElement(
By.id(String.format(idFormat, rowIndex, pluralIndex)));
}
});
we.click();
we.sendKeys(text);
}
/**
* Press the Approve button for the currently selected translation row
* @return new Editor page object
*/
public EditorPage approveSelectedTranslation() {
WebElement approve = waitForTenSec().until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver input) {
return input.findElement(By.className("selected"))
.findElement(By.className("icon-install"));
}
});
approve.click();
return new EditorPage(getDriver());
}
}