/
WebServerAcceptanceSteps.java
161 lines (136 loc) · 6.77 KB
/
WebServerAcceptanceSteps.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
package jmri.web;
import cucumber.api.java8.En;
import java.io.File;
import java.util.LinkedHashSet;
import java.util.List;
import jmri.InstanceManager;
import jmri.ConfigureManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Sets.newLinkedHashSet;
/**
* Cucumber step definitions for Web Server Acceptance tests.
*
* @author Paul Bender Copyright (C) 2017
*/
public class WebServerAcceptanceSteps implements En {
private EventFiringWebDriver webDriver;
String[] firefoxtags = {"@webtest", "@firefox"};
String[] chrometags = {"@webtest", "@chrome"};
String[] tags = {"@webtest"};
String[] paneltags = {"@webpanel"};
public WebServerAcceptanceSteps(jmri.InstanceManager instance) {
Given("^I am using firefox$", () -> {
webDriver = jmri.util.web.BrowserFactory.getBrowser("Firefox");
});
Given("^I am using chrome$", () -> {
webDriver = jmri.util.web.BrowserFactory.getBrowser("Chrome");
});
When("^I ask for the url (.*)$", (String url) -> {
webDriver.get(url);
});
Given("^panel (.*) is loaded$", (String path) -> {
InstanceManager.getDefault(ConfigureManager.class)
.load(new File(path));
});
Then("^a page with title (.*) is returned$", (String pageTitle) -> {
waitLoad();
assertThat(webDriver.getTitle()).isEqualTo(pageTitle);
});
Then("^either (.*) or (.*) is returned as the title$", (String pageTitle,String formatedPageTitle) -> {
waitLoad();
assertThat(webDriver.getTitle()).isIn(newLinkedHashSet(pageTitle,formatedPageTitle));
});
After(paneltags, () -> {
// navigate back home to prevent the webpage from reloading.
webDriver.get("http://localhost:12080/");
jmri.util.JUnitUtil.closeAllPanels();
});
Then("^(.*) has item (.*) with state (.*)$", (String table, String item, String state) -> {
webDriver.get("http://localhost:12080/");
waitLoad();
// navigate to the table.
(webDriver.findElement(By.linkText("Tables"))).click();
(webDriver.findElement(By.linkText(table))).click();
waitLoad();
// wait for the table to load.
WebDriverWait wait = new WebDriverWait(webDriver, 10 );
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("table")));
WebElement webTable = webDriver.findElement(By.xpath("//div[@id='wrap']//div[@class='container']//table"));
// find the table body.
WebElement tableBody = webTable.findElement(By.tagName("tbody"));
List<WebElement> rows = tableBody.findElements(By.tagName("tr"));
// we make an assumption that the first column is the systemName and
// the last column is the state
int i;
for(i =0; i< rows.size(); i++){
List<WebElement> cols = rows.get(i).findElements(By.tagName("td"));
if(cols.size()>0 && cols.get(0).getText().equals(item)){
assertThat(cols.get(cols.size()-1).getText()).isEqualTo(state);
break;
}
}
assertThat(rows.size()).isNotEqualTo(i).withFailMessage("item not found");
});
//find the specified cell in the table, click on it and verify new value is as expected
//uses item names and column names
Then("^table (.*) has row (.*) column (.*) with text (.*) after click (.*)$",
(String table, String row, String column, String text, String after) -> {
webDriver.get("http://localhost:12080/");
waitLoad();
// navigate to the table.
(webDriver.findElement(By.linkText("Tables"))).click();
(webDriver.findElement(By.linkText(table))).click();
waitLoad();
// wait for the table to load.
WebDriverWait wait = new WebDriverWait(webDriver, 10 );
String tablePath = "//table[@id='jmri-data']";
String cellPath = tablePath + "//tr[@data-name='" + row + "']//td[@class='" + column +"']";
String cellAfterPath = cellPath + "[text()='" + after +"']";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(tablePath)));
//look for the requested cell using tr and td attributes, getting any matches
List<WebElement> cells = webDriver.findElements(By.xpath(cellPath));
//must be only one cell that matches
assertThat((Integer)cells.size()).isEqualTo(1);
//cell text must match expected value
assertThat(cells.get(0).getText()).isEqualTo(text);
//click on the target cell
cells.get(0).click();
//wait for cell to be updated with the "after" value
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(cellAfterPath)));
//refresh the reference since the json update replaced the cell
cells = webDriver.findElements(By.xpath(cellPath));
//check that "after" value is correct
assertThat(cells.get(0).getText()).isEqualTo(after);
});
}
private void waitLoad(){
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(new ExpectedCondition<Boolean>() {
// this ExpectedCondition code is derived from code posted by user
// Jeff Vincent to
// https://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds-in-java
@Override
public Boolean apply(WebDriver driver) {
String script =
"if (typeof window != 'undefined' && window.document) { return window.document.readyState; } else { return 'notready'; }";
Boolean result = Boolean.FALSE;
if (driver != null) {
try {
result = ((JavascriptExecutor) driver).executeScript(script).equals("complete");
} catch (Exception ex) {
// nothing to do, but silence the error
}
}
return result;
}
});
}
}