18
18
19
19
package ru .mystamps .web .tests .page ;
20
20
21
- import java .util .HashMap ;
22
21
import java .util .List ;
23
- import java .util .Map ;
24
22
25
23
import org .openqa .selenium .WebDriver ;
26
24
import org .openqa .selenium .support .ui .Select ;
40
38
41
39
public abstract class AbstractPageWithForm extends AbstractPage {
42
40
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
-
53
41
private static final String LABEL_LOCATOR = "//label[@for=\" %s\" ]" ;
54
42
private static final String FIELD_ERROR_LOCATOR = "//span[@id=\" %s.errors\" ]" ;
55
43
private static final String FIELD_REQUIRED_LOCATOR = "//span[@id=\" %s.required\" ]" ;
56
44
private static final String FORM_ERROR_LOCATOR = "//div[@id=\" form.errors\" ]" ;
57
45
58
- /// @see isFieldExists()
59
- private static final Map <String , String > fieldLocators = new HashMap <String , String >();
60
-
61
46
@ Getter private Form form ;
62
47
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
-
72
48
public AbstractPageWithForm (final WebDriver driver , final String pageUrl ) {
73
49
super (driver , pageUrl );
74
50
}
@@ -78,31 +54,44 @@ protected void hasForm(final Form form) {
78
54
}
79
55
80
56
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 ());
90
58
}
91
59
92
60
public boolean isFieldHasError (final String id ) {
93
61
return elementWithXPathExists (String .format (FIELD_ERROR_LOCATOR , id ));
94
62
}
95
63
96
64
public boolean isSubmitButtonExists (final SubmitButton button ) {
97
- return elementWithXPathExists (String . format ( SUBMIT_WITH_VALUE_LOCATOR , button .getValue () ));
65
+ return elementWithXPathExists (button .toString ( ));
98
66
}
99
67
100
68
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 ();
102
85
}
103
86
104
87
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 ());
106
95
}
107
96
108
97
public String getInputLabelValue (final String id ) {
@@ -114,7 +103,15 @@ public boolean inputHasAsterisk(final String id) {
114
103
}
115
104
116
105
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" );
118
115
}
119
116
120
117
public String getFieldError (final String id ) {
0 commit comments