1
1
package io .mosparo .client ;
2
2
3
3
import java .io .File ;
4
+ import java .io .IOException ;
4
5
import java .net .URL ;
5
6
import java .nio .charset .StandardCharsets ;
6
7
import java .time .Duration ;
7
8
import java .util .HashMap ;
8
9
import java .util .Map ;
9
10
import java .util .logging .Level ;
10
11
12
+ import org .junit .jupiter .api .BeforeAll ;
11
13
import org .junit .jupiter .api .extension .ExtendWith ;
12
14
import org .openqa .selenium .By ;
13
15
import org .openqa .selenium .WebElement ;
@@ -40,7 +42,8 @@ abstract class AbstractMosparoIT {
40
42
41
43
static RemoteWebDriver driver ;
42
44
43
- static {
45
+ @ BeforeAll
46
+ static void startEnvironment () throws IOException {
44
47
environment = new ComposeContainer (new File ("src/test/resources/docker-compose.yaml" ))
45
48
.withExposedService (MOSPARO_WEB_COMPOSE_SERVICE , MOSPARO_WEB_EXPOSED_PORT )
46
49
.withExposedService ("firefox" , 4444 );
@@ -51,87 +54,82 @@ abstract class AbstractMosparoIT {
51
54
Integer mosparoPort = environment .getServicePort (MOSPARO_WEB_COMPOSE_SERVICE , MOSPARO_WEB_EXPOSED_PORT );
52
55
mosparoUrl = "http://" + mosparoHost + ":" + mosparoPort ;
53
56
54
- try {
55
- // Connecting to selenium container
56
- String firefoxHost = environment .getServiceHost ("firefox" , 4444 );
57
- Integer firefoxPort = environment .getServicePort ("firefox" , 4444 );
58
- URL spec = new URL ("http://" + firefoxHost + ":" + firefoxPort + "/wd/hub" );
59
- FirefoxOptions options = new FirefoxOptions ();
60
- driver = new RemoteWebDriver (spec , options );
61
- driver .setLogLevel (Level .ALL );
62
-
63
- // Page: Database
64
- driver .get ("http://mosparo_web/setup/database" );
65
- new WebDriverWait (driver , Duration .ofSeconds (60 )).until (ExpectedConditions .titleIs ("Database - mosparo" ));
66
- new Select (driver .findElement (By .id ("form_system" ))).selectByVisibleText ("MySQL/MariaDB" );
67
- driver .findElement (By .id ("form_host" )).sendKeys ("db" );
68
- driver .findElement (By .id ("form_port" )).clear ();
69
- driver .findElement (By .id ("form_port" )).sendKeys ("3306" );
70
- driver .findElement (By .id ("form_database" )).sendKeys ("mosparo" );
71
- driver .findElement (By .id ("form_user" )).sendKeys ("mosparo" );
72
- driver .findElement (By .id ("form_password" )).sendKeys ("password" );
73
- scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
74
-
75
- // Page: Other information
76
- new WebDriverWait (driver , Duration .ofSeconds (5 )).until (ExpectedConditions .titleIs ("Other information - mosparo" ));
77
- driver .findElement (By .id ("form_name" )).sendKeys ("mosparo" );
78
- driver .findElement (By .id ("form_emailAddress" )).sendKeys ("test@mosparo.io" );
79
- driver .findElement (By .id ("form_password_plainPassword_first" )).sendKeys ("password" );
80
- driver .findElement (By .id ("form_password_plainPassword_second" )).sendKeys ("password" );
81
- scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
82
-
83
- // The installation was successfully completed
84
- new WebDriverWait (driver , Duration .ofSeconds (5 ))
85
- .until (ExpectedConditions .elementToBeClickable (By .linkText ("Go to login" )));
86
- scrollIntoViewAndClick (By .linkText ("Go to login" ));
87
-
88
- // Page: Login
89
- new WebDriverWait (driver , Duration .ofSeconds (5 )).until (ExpectedConditions .titleIs ("Login - mosparo" ));
90
- driver .findElement (By .id ("field-email" )).sendKeys ("test@mosparo.io" );
91
- driver .findElement (By .id ("field-password" )).sendKeys ("password" );
92
- scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
93
-
94
- // Page: Projects
95
- new WebDriverWait (driver , Duration .ofSeconds (5 )).until (ExpectedConditions .titleIs ("Projects - mosparo" ));
96
- scrollIntoViewAndClick (By .xpath ("//button[normalize-space()='Create']" ));
97
- scrollIntoViewAndClick (By .linkText ("Create project" ));
98
-
99
- // Page: Create project
100
- new WebDriverWait (driver , Duration .ofSeconds (5 )).until (ExpectedConditions .titleIs ("Create project - mosparo" ));
101
- driver .findElement (By .id ("project_form_name" )).sendKeys ("tests" );
102
- driver .findElement (By .id ("project_form_description" ))
103
- .sendKeys ("Test project for mosparo Java API MosparoDefaultClient" );
104
- driver .findElement (By .cssSelector ("input[id^='project_form_hosts_']" )).sendKeys ("*" );
105
- scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
106
-
107
- // Page: Create project › Select design
108
- new WebDriverWait (driver , Duration .ofSeconds (5 ))
109
- .until (ExpectedConditions .titleIs ("Create project › Select design - mosparo" ));
110
- scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
111
-
112
- // Page: Create project › Enable security features
113
- new WebDriverWait (driver , Duration .ofSeconds (5 ))
114
- .until (ExpectedConditions .titleIs ("Create project › Enable security features - mosparo" ));
115
- scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
116
-
117
- // Page: Create project › Connection details
118
- new WebDriverWait (driver , Duration .ofSeconds (5 ))
119
- .until (ExpectedConditions .titleIs ("Create project › Connection details - mosparo" ));
120
- projectUuid = driver .findElement (By .id ("projectUuid" )).getDomAttribute ("value" );
121
- projectPublicKey = driver .findElement (By .id ("projectPublicKey" )).getDomAttribute ("value" );
122
- projectPrivateKey = driver .findElement (By .id ("projectPrivateKey" )).getDomAttribute ("value" );
123
-
124
- // Generate the demo form
125
- String html = IOUtils .resourceToString ("index.html" , StandardCharsets .UTF_8 ,
126
- AbstractMosparoIT .class .getClassLoader ());
127
- html = html .replace ("#projectUuid#" , projectUuid );
128
- html = html .replace ("#projectPublicKey#" , projectPublicKey );
129
- ContainerState website = environment .getContainerByServiceName ("website" ).orElseThrow ();
130
- website .copyFileToContainer (Transferable .of (html ), "/usr/share/nginx/html/index.html" );
131
-
132
- } catch (Exception e ) {
133
- throw new RuntimeException (e );
134
- }
57
+ // Connecting to selenium container
58
+ String firefoxHost = environment .getServiceHost ("firefox" , 4444 );
59
+ Integer firefoxPort = environment .getServicePort ("firefox" , 4444 );
60
+ URL spec = new URL ("http://" + firefoxHost + ":" + firefoxPort + "/wd/hub" );
61
+ FirefoxOptions options = new FirefoxOptions ();
62
+ driver = new RemoteWebDriver (spec , options );
63
+ driver .setLogLevel (Level .ALL );
64
+
65
+ // Page: Database
66
+ driver .get ("http://mosparo_web/setup/database" );
67
+ new WebDriverWait (driver , Duration .ofSeconds (60 )).until (ExpectedConditions .titleIs ("Database - mosparo" ));
68
+ new Select (driver .findElement (By .id ("form_system" ))).selectByVisibleText ("MySQL/MariaDB" );
69
+ driver .findElement (By .id ("form_host" )).sendKeys ("db" );
70
+ driver .findElement (By .id ("form_port" )).clear ();
71
+ driver .findElement (By .id ("form_port" )).sendKeys ("3306" );
72
+ driver .findElement (By .id ("form_database" )).sendKeys ("mosparo" );
73
+ driver .findElement (By .id ("form_user" )).sendKeys ("mosparo" );
74
+ driver .findElement (By .id ("form_password" )).sendKeys ("password" );
75
+ scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
76
+
77
+ // Page: Other information
78
+ new WebDriverWait (driver , Duration .ofSeconds (5 )).until (ExpectedConditions .titleIs ("Other information - mosparo" ));
79
+ driver .findElement (By .id ("form_name" )).sendKeys ("mosparo" );
80
+ driver .findElement (By .id ("form_emailAddress" )).sendKeys ("test@mosparo.io" );
81
+ driver .findElement (By .id ("form_password_plainPassword_first" )).sendKeys ("password" );
82
+ driver .findElement (By .id ("form_password_plainPassword_second" )).sendKeys ("password" );
83
+ scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
84
+
85
+ // The installation was successfully completed
86
+ new WebDriverWait (driver , Duration .ofSeconds (5 ))
87
+ .until (ExpectedConditions .elementToBeClickable (By .linkText ("Go to login" )));
88
+ scrollIntoViewAndClick (By .linkText ("Go to login" ));
89
+
90
+ // Page: Login
91
+ new WebDriverWait (driver , Duration .ofSeconds (5 )).until (ExpectedConditions .titleIs ("Login - mosparo" ));
92
+ driver .findElement (By .id ("field-email" )).sendKeys ("test@mosparo.io" );
93
+ driver .findElement (By .id ("field-password" )).sendKeys ("password" );
94
+ scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
95
+
96
+ // Page: Projects
97
+ new WebDriverWait (driver , Duration .ofSeconds (5 )).until (ExpectedConditions .titleIs ("Projects - mosparo" ));
98
+ scrollIntoViewAndClick (By .xpath ("//button[normalize-space()='Create']" ));
99
+ scrollIntoViewAndClick (By .linkText ("Create project" ));
100
+
101
+ // Page: Create project
102
+ new WebDriverWait (driver , Duration .ofSeconds (5 )).until (ExpectedConditions .titleIs ("Create project - mosparo" ));
103
+ driver .findElement (By .id ("project_form_name" )).sendKeys ("tests" );
104
+ driver .findElement (By .id ("project_form_description" ))
105
+ .sendKeys ("Test project for mosparo Java API MosparoDefaultClient" );
106
+ driver .findElement (By .cssSelector ("input[id^='project_form_hosts_']" )).sendKeys ("*" );
107
+ scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
108
+
109
+ // Page: Create project › Select design
110
+ new WebDriverWait (driver , Duration .ofSeconds (5 ))
111
+ .until (ExpectedConditions .titleIs ("Create project › Select design - mosparo" ));
112
+ scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
113
+
114
+ // Page: Create project › Enable security features
115
+ new WebDriverWait (driver , Duration .ofSeconds (5 ))
116
+ .until (ExpectedConditions .titleIs ("Create project › Enable security features - mosparo" ));
117
+ scrollIntoViewAndClick (By .cssSelector ("button[type='submit']" ));
118
+
119
+ // Page: Create project › Connection details
120
+ new WebDriverWait (driver , Duration .ofSeconds (5 ))
121
+ .until (ExpectedConditions .titleIs ("Create project › Connection details - mosparo" ));
122
+ projectUuid = driver .findElement (By .id ("projectUuid" )).getDomAttribute ("value" );
123
+ projectPublicKey = driver .findElement (By .id ("projectPublicKey" )).getDomAttribute ("value" );
124
+ projectPrivateKey = driver .findElement (By .id ("projectPrivateKey" )).getDomAttribute ("value" );
125
+
126
+ // Generate the demo form
127
+ String html = IOUtils .resourceToString ("index.html" , StandardCharsets .UTF_8 ,
128
+ AbstractMosparoIT .class .getClassLoader ());
129
+ html = html .replace ("#projectUuid#" , projectUuid );
130
+ html = html .replace ("#projectPublicKey#" , projectPublicKey );
131
+ ContainerState website = environment .getContainerByServiceName ("website" ).orElseThrow ();
132
+ website .copyFileToContainer (Transferable .of (html ), "/usr/share/nginx/html/index.html" );
135
133
}
136
134
137
135
static void scrollIntoViewAndClick (By locator ) {
@@ -157,9 +155,18 @@ Map<String, Object> visitForm(Map<String, Object> input) {
157
155
new Actions (driver ).moveToElement (element ).click ().perform ();
158
156
159
157
// Retrieve the tokens
160
- String mosparoSubmitToken = driver .findElement (By .name ("_mosparo_submitToken" )).getDomAttribute ("value" );
158
+ WebElement submitTokenElement = driver .findElement (By .name ("_mosparo_submitToken" ));
159
+ new WebDriverWait (driver , Duration .ofSeconds (2 )).until (
160
+ ExpectedConditions .not (ExpectedConditions .domAttributeToBe (submitTokenElement , "value" , "" )));
161
+
162
+ String mosparoSubmitToken = submitTokenElement .getDomAttribute ("value" );
161
163
formData .put ("_mosparo_submitToken" , mosparoSubmitToken );
162
- String mosparoValidationToken = driver .findElement (By .name ("_mosparo_validationToken" )).getDomAttribute ("value" );
164
+
165
+ WebElement validationTokenElement = driver .findElement (By .name ("_mosparo_validationToken" ));
166
+ new WebDriverWait (driver , Duration .ofSeconds (2 )).until (
167
+ ExpectedConditions .not (ExpectedConditions .domAttributeToBe (validationTokenElement , "value" , "" )));
168
+
169
+ String mosparoValidationToken = validationTokenElement .getDomAttribute ("value" );
163
170
formData .put ("_mosparo_validationToken" , mosparoValidationToken );
164
171
165
172
// Submit form
0 commit comments