Skip to content

Commit 5cad7ae

Browse files
committed
Simplify test condition to avoid "stale element" exception
1 parent 43ed45c commit 5cad7ae

File tree

1 file changed

+154
-181
lines changed

1 file changed

+154
-181
lines changed

annis-kickstarter/src/test/java/annis/gui/AcceptanceTest.java

Lines changed: 154 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -51,185 +51,158 @@
5151
*
5252
* @author thomas
5353
*/
54-
public class AcceptanceTest
55-
{
56-
private static final Logger log = LoggerFactory.getLogger(AcceptanceTest.class);
57-
58-
private static KickstartRunner runner;
59-
private static WebDriver driver;
60-
61-
private WebDriverWait wait;
62-
63-
private final static int WEB_PORT = 8086;
64-
private final static int SERVICE_PORT = 5722;
65-
66-
private static final Set<String> corpora = new LinkedHashSet<>();
67-
68-
@BeforeClass
69-
public static void runKickstarter()
70-
{
71-
try
72-
{
73-
runner = new KickstartRunner(WEB_PORT, SERVICE_PORT);
74-
75-
runner.startService();
76-
runner.startJetty();
77-
78-
// get all installed corpora
79-
for(AnnisCorpus c : runner.getCorpora())
80-
{
81-
corpora.add(c.getName());
82-
}
83-
84-
DesiredCapabilities caps = new DesiredCapabilities();
85-
caps.setCapability("takesScreenshot", true);
86-
87-
88-
driver = new PhantomJSDriver(caps);
89-
driver.manage().window().setSize(new Dimension(1024, 768));
90-
91-
}
92-
catch (Exception ex)
93-
{
94-
log.error(null, ex);
95-
runner = null;
96-
}
97-
}
98-
99-
@Before
100-
public void setup()
101-
{
102-
Assume.assumeNotNull(driver);
103-
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
104-
wait = new WebDriverWait(driver, 30);
105-
106-
driver.get("http://localhost:" + WEB_PORT + "/annis-gui/");
107-
108-
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("v-app")));
109-
}
110-
111-
protected void takeScreenshot(File outputFile) {
112-
if(driver instanceof TakesScreenshot)
113-
{
114-
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
115-
try
116-
{
117-
Files.copy(screenshot, outputFile);
118-
}
119-
catch (IOException ex)
120-
{
121-
log.error("Could not create screenshot", ex);
122-
}
123-
}
124-
}
125-
126-
@Test
127-
public void testAboutWindow()
128-
{
129-
driver.findElement(By.id("SearchView:MainToolbar:btAboutAnnis")).click();
130-
Assert.assertTrue(driver.findElement(By.id("AboutWindow:VerticalLayout:btClose")).isDisplayed());
131-
driver.findElement(By.id("AboutWindow:VerticalLayout:btClose")).click();
132-
}
133-
134-
@Test
135-
public void testOpenSourceWindow()
136-
{
137-
driver.findElement(By.id("SearchView:MainToolbar:btOpenSource")).click();
138-
Assert.assertTrue(driver.findElement(By.id("HelpUsWindow:VerticalLayout:btClose")).isDisplayed());
139-
driver.findElement(By.id("HelpUsWindow:VerticalLayout:btClose")).click();
140-
}
141-
142-
@Test
143-
public void testTokenSearchPcc2() throws IOException
144-
{
145-
JavascriptExecutor js = (JavascriptExecutor) driver;
146-
147-
// only execute this test if pcc2 corpus is imported
148-
Assume.assumeTrue(corpora.contains("pcc2"));
149-
150-
// execute a "tok" search on pcc2
151-
WebElement codeMirror = driver.findElement(By.xpath("//div[@id='SearchView:ControlPanel:QueryPanel']//div[contains(@class,'CodeMirror')]"));
152-
153-
// activate the code mirror field (so we can leave it later)
154-
codeMirror.click();
155-
// set text by javascript
156-
js.executeScript("arguments[0].CodeMirror.setValue('tok');", codeMirror);
157-
158-
// filter pcc2 corpus via text field to ensure it is visible in the table
159-
WebElement filterInput = driver.findElement(By.id("SearchView:ControlPanel:TabSheet:CorpusListPanel:txtFilter"));
160-
filterInput.click();
161-
filterInput.sendKeys("pcc2");
162-
163-
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath(
164-
"//div[@id='SearchView:ControlPanel:TabSheet:CorpusListPanel:tblCorpora']"
165-
+ "//table[contains(@class, 'v-table-table')]//tr"), "pcc2"));
166-
167-
List<WebElement> corpusTableElements = driver.findElements(By.xpath("//div[@id='SearchView:ControlPanel:TabSheet:CorpusListPanel:tblCorpora']//table[contains(@class, 'v-table-table')]//tr"));
168-
WebElement tdPcc = null;
169-
170-
for(WebElement elem : corpusTableElements)
171-
{
172-
// get div
173-
WebElement div = elem.findElement(By.tagName("div"));
174-
if(div != null && "pcc2".equals(div.getText()))
175-
{
176-
tdPcc = elem;
177-
}
178-
}
179-
180-
Assert.assertNotNull(tdPcc);
181-
tdPcc.click();
182-
183-
driver.findElement(By.id("SearchView:ControlPanel:QueryPanel:btShowResult")).click();
184-
185-
// wait until the result is loaded
186-
By byGridTable = By.xpath("//div[@id='SearchView:TabSheet:ResultViewPanel:Panel:resultLayout:SingleResultPanel.1']/div[2]//table");
187-
wait.until(ExpectedConditions.visibilityOfElementLocated(byGridTable));
188-
189-
WebElement gridTable = driver.findElement(byGridTable);
190-
List<WebElement> firstRow = gridTable.findElements(By.xpath(".//tr[1]/td"));
191-
Assert.assertEquals(7, firstRow.size());
192-
Assert.assertEquals("Feigenblatt", firstRow.get(0).getText());
193-
Assert.assertEquals("Die", firstRow.get(1).getText());
194-
Assert.assertEquals("Jugendlichen", firstRow.get(2).getText());
195-
Assert.assertEquals("in", firstRow.get(3).getText());
196-
Assert.assertEquals("Zossen", firstRow.get(4).getText());
197-
Assert.assertEquals("wollen", firstRow.get(5).getText());
198-
Assert.assertEquals("ein", firstRow.get(6).getText());
199-
}
200-
201-
/**
202-
* Make sure the "Show in ANNIS search interface" link is shown in embedded visualizer.
203-
* Regression test for
204-
* https://github.com/korpling/ANNIS/issues/509
205-
* (Link from embedded visualization to search UI is gone in 3.4.0)
206-
*/
207-
@Test
208-
public void testRegression509()
209-
{
210-
// only execute this test if pcc2 corpus is imported
211-
Assume.assumeTrue(corpora.contains("pcc2"));
212-
213-
driver.get("http://localhost:" + WEB_PORT + "/annis-gui/embeddedvis/grid?embedded_ns=exmaralda&embedded_instance=&embedded_salt=http%3A%2F%2Flocalhost%3A" + SERVICE_PORT + "%2Fannis%2Fquery%2Fsearch%2Fsubgraph%3Fmatch%3Dsalt%3A%2Fpcc2%2F11299%2F%2523tok_1%26left%3D5%26right%3D5&embedded_interface=http://localhost:8084/annis-gui/%23_q%3DdG9r%26_c%3DcGNjMg%26cl%3D5%26cr%3D5%26s%3D0%26l%3D10%26m%3D0");
214-
215-
// wait until page was loaded
216-
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("v-app")));
217-
// wait until visualization is actually there
218-
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className(
219-
"v-app-loading")));
220-
221-
WebElement link = driver.findElement(By.xpath("//div[@id='VerticalLayout:Link']/a/span[2]"));
222-
Assert.assertEquals("Show in ANNIS search interface", link.getText());
223-
224-
}
225-
226-
@AfterClass
227-
public static void cleanup()
228-
{
229-
if(driver != null)
230-
{
231-
driver.quit();
232-
}
233-
}
234-
54+
public class AcceptanceTest {
55+
private static final Logger log = LoggerFactory.getLogger(AcceptanceTest.class);
56+
57+
private static KickstartRunner runner;
58+
private static WebDriver driver;
59+
60+
private WebDriverWait wait;
61+
62+
private final static int WEB_PORT = 8086;
63+
private final static int SERVICE_PORT = 5722;
64+
65+
private static final Set<String> corpora = new LinkedHashSet<>();
66+
67+
@BeforeClass
68+
public static void runKickstarter() {
69+
try {
70+
runner = new KickstartRunner(WEB_PORT, SERVICE_PORT);
71+
72+
runner.startService();
73+
runner.startJetty();
74+
75+
// get all installed corpora
76+
for (AnnisCorpus c : runner.getCorpora()) {
77+
corpora.add(c.getName());
78+
}
79+
80+
DesiredCapabilities caps = new DesiredCapabilities();
81+
caps.setCapability("takesScreenshot", true);
82+
83+
driver = new PhantomJSDriver(caps);
84+
driver.manage().window().setSize(new Dimension(1024, 768));
85+
86+
} catch (Exception ex) {
87+
log.error(null, ex);
88+
runner = null;
89+
}
90+
}
91+
92+
@Before
93+
public void setup() {
94+
Assume.assumeNotNull(driver);
95+
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
96+
wait = new WebDriverWait(driver, 30);
97+
98+
driver.get("http://localhost:" + WEB_PORT + "/annis-gui/");
99+
100+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("v-app")));
101+
}
102+
103+
protected void takeScreenshot(File outputFile) {
104+
if (driver instanceof TakesScreenshot) {
105+
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
106+
try {
107+
Files.copy(screenshot, outputFile);
108+
} catch (IOException ex) {
109+
log.error("Could not create screenshot", ex);
110+
}
111+
}
112+
}
113+
114+
@Test
115+
public void testAboutWindow() {
116+
driver.findElement(By.id("SearchView:MainToolbar:btAboutAnnis")).click();
117+
Assert.assertTrue(driver.findElement(By.id("AboutWindow:VerticalLayout:btClose")).isDisplayed());
118+
driver.findElement(By.id("AboutWindow:VerticalLayout:btClose")).click();
119+
}
120+
121+
@Test
122+
public void testOpenSourceWindow() {
123+
driver.findElement(By.id("SearchView:MainToolbar:btOpenSource")).click();
124+
Assert.assertTrue(driver.findElement(By.id("HelpUsWindow:VerticalLayout:btClose")).isDisplayed());
125+
driver.findElement(By.id("HelpUsWindow:VerticalLayout:btClose")).click();
126+
}
127+
128+
@Test
129+
public void testTokenSearchPcc2() throws IOException {
130+
JavascriptExecutor js = (JavascriptExecutor) driver;
131+
132+
// only execute this test if pcc2 corpus is imported
133+
Assume.assumeTrue(corpora.contains("pcc2"));
134+
135+
// execute a "tok" search on pcc2
136+
WebElement codeMirror = driver.findElement(
137+
By.xpath("//div[@id='SearchView:ControlPanel:QueryPanel']//div[contains(@class,'CodeMirror')]"));
138+
139+
// activate the code mirror field (so we can leave it later)
140+
codeMirror.click();
141+
// set text by javascript
142+
js.executeScript("arguments[0].CodeMirror.setValue('tok');", codeMirror);
143+
144+
// filter pcc2 corpus via text field to ensure it is visible in the table
145+
WebElement filterInput = driver
146+
.findElement(By.id("SearchView:ControlPanel:TabSheet:CorpusListPanel:txtFilter"));
147+
filterInput.click();
148+
filterInput.sendKeys("pcc2");
149+
150+
By pccTrXPath = By.xpath("//div[@id='SearchView:ControlPanel:TabSheet:CorpusListPanel:tblCorpora']"
151+
+ "//table[contains(@class, 'v-table-table')]//tr//div[contains(text(), 'pcc2')]");
152+
153+
wait.until(ExpectedConditions.visibilityOfElementLocated(pccTrXPath));
154+
155+
driver.findElement(pccTrXPath).click();
156+
157+
driver.findElement(By.id("SearchView:ControlPanel:QueryPanel:btShowResult")).click();
158+
159+
// wait until the result is loaded
160+
By byGridTable = By.xpath(
161+
"//div[@id='SearchView:TabSheet:ResultViewPanel:Panel:resultLayout:SingleResultPanel.1']/div[2]//table");
162+
wait.until(ExpectedConditions.visibilityOfElementLocated(byGridTable));
163+
164+
WebElement gridTable = driver.findElement(byGridTable);
165+
List<WebElement> firstRow = gridTable.findElements(By.xpath(".//tr[1]/td"));
166+
Assert.assertEquals(7, firstRow.size());
167+
Assert.assertEquals("Feigenblatt", firstRow.get(0).getText());
168+
Assert.assertEquals("Die", firstRow.get(1).getText());
169+
Assert.assertEquals("Jugendlichen", firstRow.get(2).getText());
170+
Assert.assertEquals("in", firstRow.get(3).getText());
171+
Assert.assertEquals("Zossen", firstRow.get(4).getText());
172+
Assert.assertEquals("wollen", firstRow.get(5).getText());
173+
Assert.assertEquals("ein", firstRow.get(6).getText());
174+
}
175+
176+
/**
177+
* Make sure the "Show in ANNIS search interface" link is shown in embedded
178+
* visualizer. Regression test for https://github.com/korpling/ANNIS/issues/509
179+
* (Link from embedded visualization to search UI is gone in 3.4.0)
180+
*/
181+
@Test
182+
public void testRegression509() {
183+
// only execute this test if pcc2 corpus is imported
184+
Assume.assumeTrue(corpora.contains("pcc2"));
185+
186+
driver.get("http://localhost:" + WEB_PORT
187+
+ "/annis-gui/embeddedvis/grid?embedded_ns=exmaralda&embedded_instance=&embedded_salt=http%3A%2F%2Flocalhost%3A"
188+
+ SERVICE_PORT
189+
+ "%2Fannis%2Fquery%2Fsearch%2Fsubgraph%3Fmatch%3Dsalt%3A%2Fpcc2%2F11299%2F%2523tok_1%26left%3D5%26right%3D5&embedded_interface=http://localhost:8084/annis-gui/%23_q%3DdG9r%26_c%3DcGNjMg%26cl%3D5%26cr%3D5%26s%3D0%26l%3D10%26m%3D0");
190+
191+
// wait until page was loaded
192+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("v-app")));
193+
// wait until visualization is actually there
194+
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("v-app-loading")));
195+
196+
WebElement link = driver.findElement(By.xpath("//div[@id='VerticalLayout:Link']/a/span[2]"));
197+
Assert.assertEquals("Show in ANNIS search interface", link.getText());
198+
199+
}
200+
201+
@AfterClass
202+
public static void cleanup() {
203+
if (driver != null) {
204+
driver.quit();
205+
}
206+
}
207+
235208
}

0 commit comments

Comments
 (0)