Skip to content

Commit 062174a

Browse files
committed
Improved error message: show xpath for not existing element.
Constants with XPath of elements was moved from AbstractPageWithForm class to Form. No functional changes.
1 parent 7e36ba4 commit 062174a

File tree

3 files changed

+94
-49
lines changed

3 files changed

+94
-49
lines changed

src/test/java/ru/mystamps/web/tests/cases/WhenUserAtAnyPageWithForm.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ private void shouldHaveForm() {
5959

6060
private void shouldHaveFields() {
6161
for (final Field field : page.getForm().getFields()) {
62-
// TODO: improve message to showing input type.
63-
// Probably we need to call field.toString() there
6462
assertThat(page.isFieldExists(field))
65-
.overridingErrorMessage("field named '" + field.getName() + "' should exists")
63+
.overridingErrorMessage("field with XPath '" + field + "' should exists")
6664
.isTrue();
6765
}
6866
}

src/test/java/ru/mystamps/web/tests/page/AbstractPageWithForm.java

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
package ru.mystamps.web.tests.page;
2020

21-
import java.util.HashMap;
2221
import java.util.List;
23-
import java.util.Map;
2422

2523
import org.openqa.selenium.WebDriver;
2624
import org.openqa.selenium.support.ui.Select;
@@ -40,35 +38,13 @@
4038

4139
public abstract class AbstractPageWithForm extends AbstractPage {
4240

43-
private static final String FORM_LOCATOR = "//form";
44-
private static final String INPUT_FIELD_LOCATOR = "//input[@name=\"%s\"]";
45-
private static final String PASSWORD_FIELD_LOCATOR = "//input[@name=\"%s\"][@type=\"password\"]";
46-
private static final String CHECKBOX_FIELD_LOCATOR = "//input[@name=\"%s\"][@type=\"checkbox\"]";
47-
private static final String UPLOAD_FIELD_LOCATOR = "//input[@name=\"%s\"][@type=\"file\"]";
48-
private static final String SELECT_FIELD_LOCATOR = "//select[@name=\"%s\"]";
49-
private static final String TEXTAREA_FIELD_LOCATOR = "//textarea[@name=\"%s\"]";
50-
private static final String SUBMIT_BUTTON_LOCATOR = "//input[@type=\"submit\"]";
51-
private static final String SUBMIT_WITH_VALUE_LOCATOR = "//input[@type=\"submit\"][@value=\"%s\"]";
52-
5341
private static final String LABEL_LOCATOR = "//label[@for=\"%s\"]";
5442
private static final String FIELD_ERROR_LOCATOR = "//span[@id=\"%s.errors\"]";
5543
private static final String FIELD_REQUIRED_LOCATOR = "//span[@id=\"%s.required\"]";
5644
private static final String FORM_ERROR_LOCATOR = "//div[@id=\"form.errors\"]";
5745

58-
/// @see isFieldExists()
59-
private static final Map<String, String> fieldLocators = new HashMap<String, String>();
60-
6146
@Getter private Form form;
6247

63-
static {
64-
fieldLocators.put(InputField.class.getSimpleName(), INPUT_FIELD_LOCATOR);
65-
fieldLocators.put(CheckboxField.class.getSimpleName(), CHECKBOX_FIELD_LOCATOR);
66-
fieldLocators.put(UploadFileField.class.getSimpleName(), UPLOAD_FIELD_LOCATOR);
67-
fieldLocators.put(PasswordField.class.getSimpleName(), PASSWORD_FIELD_LOCATOR);
68-
fieldLocators.put(SelectField.class.getSimpleName(), SELECT_FIELD_LOCATOR);
69-
fieldLocators.put(TextareaField.class.getSimpleName(), TEXTAREA_FIELD_LOCATOR);
70-
}
71-
7248
public AbstractPageWithForm(final WebDriver driver, final String pageUrl) {
7349
super(driver, pageUrl);
7450
}
@@ -78,31 +54,44 @@ protected void hasForm(final Form form) {
7854
}
7955

8056
public boolean isFieldExists(final Field field) {
81-
final String fieldType = field.getClass().getSimpleName();
82-
if (!fieldLocators.containsKey(fieldType)) {
83-
throw new IllegalArgumentException("Internal error: unknown field type");
84-
}
85-
86-
final String fieldXpath =
87-
String.format(fieldLocators.get(fieldType), field.getName());
88-
89-
return elementWithXPathExists(fieldXpath);
57+
return elementWithXPathExists(field.toString());
9058
}
9159

9260
public boolean isFieldHasError(final String id) {
9361
return elementWithXPathExists(String.format(FIELD_ERROR_LOCATOR, id));
9462
}
9563

9664
public boolean isSubmitButtonExists(final SubmitButton button) {
97-
return elementWithXPathExists(String.format(SUBMIT_WITH_VALUE_LOCATOR, button.getValue()));
65+
return elementWithXPathExists(button.toString());
9866
}
9967

10068
public void submit() {
101-
getElementByXPath(SUBMIT_BUTTON_LOCATOR).submit();
69+
if (form == null) {
70+
throw new IllegalStateException(
71+
"You are trying to submit form at page which does not have form"
72+
);
73+
}
74+
75+
final List<SubmitButton> buttons = form.getSubmitButtons();
76+
if (buttons.isEmpty()) {
77+
throw new IllegalStateException(
78+
"You are trying to submit form at page which does not have submit button"
79+
);
80+
}
81+
82+
final String xpathOfFirstSubmitButton = buttons.get(0).toString();
83+
84+
getElementByXPath(xpathOfFirstSubmitButton).submit();
10285
}
10386

10487
public boolean formExists() {
105-
return elementWithXPathExists(FORM_LOCATOR);
88+
if (form == null) {
89+
throw new IllegalStateException(
90+
"You are trying to check form at page which does not has form"
91+
);
92+
}
93+
94+
return elementWithXPathExists(form.toString());
10695
}
10796

10897
public String getInputLabelValue(final String id) {
@@ -114,7 +103,15 @@ public boolean inputHasAsterisk(final String id) {
114103
}
115104

116105
public String getFieldValue(final String name) {
117-
return getElementByXPath(String.format(INPUT_FIELD_LOCATOR, name)).getAttribute("value");
106+
if (form == null) {
107+
throw new IllegalStateException(
108+
"You are trying to find field at page which does not have form"
109+
);
110+
}
111+
112+
final String xpathField = form.getField(name).toString();
113+
114+
return getElementByXPath(xpathField).getAttribute("value");
118115
}
119116

120117
public String getFieldError(final String id) {

src/test/java/ru/mystamps/web/tests/page/element/Form.java

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,32 @@
2222
import java.util.List;
2323

2424
import lombok.Getter;
25-
import lombok.RequiredArgsConstructor;
2625
import lombok.Setter;
2726

2827
public class Form {
2928
@Getter private List<Field> fields;
3029
@Getter private List<SubmitButton> submitButtons;
3130

31+
private static final String FORM_LOCATOR = "//form";
32+
private static final String INPUT_FIELD_LOCATOR = "//input[@name=\"%s\"]";
33+
private static final String PASSWORD_FIELD_LOCATOR = "//input[@name=\"%s\"][@type=\"password\"]";
34+
private static final String CHECKBOX_FIELD_LOCATOR = "//input[@name=\"%s\"][@type=\"checkbox\"]";
35+
private static final String UPLOAD_FIELD_LOCATOR = "//input[@name=\"%s\"][@type=\"file\"]";
36+
private static final String SELECT_FIELD_LOCATOR = "//select[@name=\"%s\"]";
37+
private static final String TEXTAREA_FIELD_LOCATOR = "//textarea[@name=\"%s\"]";
38+
private static final String SUBMIT_BUTTON_LOCATOR = "//input[@type=\"submit\"]";
39+
private static final String SUBMIT_WITH_VALUE_LOCATOR = "//input[@type=\"submit\"][@value=\"%s\"]";
40+
3241
private Form() {
3342
fields = new ArrayList<Field>();
3443
submitButtons = new ArrayList<SubmitButton>();
3544
}
3645

46+
@Override
47+
public String toString() {
48+
return FORM_LOCATOR;
49+
}
50+
3751
public static Form with(final Field... fields) {
3852
final Form form = new Form();
3953

@@ -66,6 +80,18 @@ public List<Field> getRequiredFields() {
6680
return requiredFields;
6781
}
6882

83+
public Field getField(final String name) {
84+
for (final Field field : fields) {
85+
if (field.getName().equals(name)) {
86+
return field;
87+
}
88+
}
89+
90+
throw new IllegalStateException(
91+
"Form does not have field with name '" + name + "'"
92+
);
93+
}
94+
6995
public Form and() {
7096
return this;
7197
}
@@ -107,9 +133,25 @@ public static SubmitButton submitButton(final String value) {
107133
// Inner classes
108134
//
109135

110-
@RequiredArgsConstructor
111136
public static class SubmitButton {
112137
@Getter private final String value;
138+
139+
private final String xpath;
140+
141+
public SubmitButton() {
142+
this.value = "";
143+
this.xpath = SUBMIT_BUTTON_LOCATOR;
144+
}
145+
146+
public SubmitButton(final String value) {
147+
this.value = value;
148+
this.xpath = String.format(SUBMIT_WITH_VALUE_LOCATOR, value);
149+
}
150+
151+
@Override
152+
public String toString() {
153+
return xpath;
154+
}
113155
}
114156

115157
public static class Field {
@@ -118,10 +160,13 @@ public static class Field {
118160
@Setter private boolean required;
119161
@Getter private String label;
120162
@Getter private String invalidValue;
163+
164+
private final String xpath;
121165
private boolean preserveInvalidValue = true;
122166

123-
protected Field(final String name) {
167+
protected Field(final String name, final String xpath) {
124168
this.name = name;
169+
this.xpath = String.format(xpath, name);
125170

126171
// Assume that name and id is similar by default
127172
this.id = name;
@@ -164,42 +209,47 @@ protected void preserveInvalidValue(boolean preserveInvalidValue) {
164209
public boolean shouldPreserveInvalidValue() {
165210
return preserveInvalidValue;
166211
}
212+
213+
@Override
214+
public String toString() {
215+
return xpath;
216+
}
167217
}
168218

169219
public static class InputField extends Field {
170220
public InputField(final String name) {
171-
super(name);
221+
super(name, INPUT_FIELD_LOCATOR);
172222
}
173223
}
174224

175225
public static class CheckboxField extends Field {
176226
public CheckboxField(final String name) {
177-
super(name);
227+
super(name, CHECKBOX_FIELD_LOCATOR);
178228
}
179229
}
180230

181231
public static class UploadFileField extends Field {
182232
public UploadFileField(final String name) {
183-
super(name);
233+
super(name, UPLOAD_FIELD_LOCATOR);
184234
}
185235
}
186236

187237
public static class PasswordField extends Field {
188238
public PasswordField(final String name) {
189-
super(name);
239+
super(name, PASSWORD_FIELD_LOCATOR);
190240
preserveInvalidValue(false);
191241
}
192242
}
193243

194244
public static class SelectField extends Field {
195245
public SelectField(final String name) {
196-
super(name);
246+
super(name, SELECT_FIELD_LOCATOR);
197247
}
198248
}
199249

200250
public static class TextareaField extends Field {
201251
public TextareaField(final String name) {
202-
super(name);
252+
super(name, TEXTAREA_FIELD_LOCATOR);
203253
}
204254
}
205255

0 commit comments

Comments
 (0)