1111package com .redhat .devtools .intellij .commonUiTestLibrary ;
1212
1313import com .intellij .remoterobot .RemoteRobot ;
14- import com .redhat .devtools .intellij .commonUiTestLibrary .utils .GlobalUtils ;
15-
16- import java .io .*;
14+ import com .redhat .devtools .intellij .commonUiTestLibrary .fixtures .dialogs .WelcomeFrameDialog ;
15+
16+ import java .io .File ;
17+ import java .io .IOException ;
18+ import java .io .InputStream ;
19+ import java .io .OutputStream ;
20+ import java .io .FileOutputStream ;
21+ import java .net .InetSocketAddress ;
22+ import java .net .Socket ;
23+ import java .net .SocketAddress ;
1724import java .nio .file .Files ;
1825import java .nio .file .Path ;
1926import java .nio .file .Paths ;
27+ import java .time .Duration ;
28+
29+ import static com .intellij .remoterobot .utils .RepeatUtilsKt .waitFor ;
2030
2131/**
2232 * Basic methods for starting and quiting the IntelliJ Idea IDE for UI tests
@@ -34,20 +44,21 @@ public static RemoteRobot runIde(String ideaVersion, int port) {
3444 String osName = System .getProperty ("os.name" ).toLowerCase ();
3545 ProcessBuilder pb ;
3646 if (osName .contains ("windows" )) {
37- pb = new ProcessBuilder ("powershell.exe" , "Start-Process" , ". \\ gradlew.bat" , "-ArgumentList " , "\" runIdeForUiTests -PideaVersion=" + ideaVersion + " \" " , "-WindowStyle" , "hidden" );
47+ pb = new ProcessBuilder (". \\ gradlew.bat" , "runIdeForUiTests " , "-PideaVersion=" + ideaVersion , "-Drobot-server.port=" + port );
3848 } else {
3949 pb = new ProcessBuilder ("./gradlew" , "runIdeForUiTests" , "-PideaVersion=" + ideaVersion , "-Drobot-server.port=" + port );
4050 }
4151
4252 try {
4353 ideProcess = pb .start ();
44- GlobalUtils . waitUntilIntelliJStarts (port );
45- robot = GlobalUtils . getRemoteRobotConnection (port );
54+ waitUntilIntelliJStarts (port );
55+ robot = getRemoteRobotConnection (port );
4656 } catch (IOException | InterruptedException e ) {
4757 e .printStackTrace ();
4858 }
4959
50- GlobalUtils .clearTheWorkspace ();
60+ WelcomeFrameDialog wfd = robot .find (WelcomeFrameDialog .class , Duration .ofSeconds (10 ));
61+ wfd .clearTheWorkspace ();
5162 return robot ;
5263 }
5364
@@ -56,7 +67,11 @@ public static RemoteRobot runIde(String ideaVersion) {
5667 }
5768
5869 public static void closeIde () {
59- ideProcess .destroy ();
70+ if (robot .isWin ()) {
71+ robot .find (WelcomeFrameDialog .class , Duration .ofSeconds (10 )).windowsCloseButton ().click ();
72+ } else {
73+ ideProcess .destroy ();
74+ }
6075 }
6176
6277 public static RemoteRobot getRemoteRobotInstance () {
@@ -76,8 +91,7 @@ private static void makeSureAllTermsAndConditionsAreAccepted() {
7691 String acceptedDir = System .getProperty ("user.home" ) + "/.local/share/JetBrains/consentOptions" ;
7792 createDirectoryHierarchy (acceptedDir );
7893 copyFileFromJarResourceDir (acceptedSourceLocation , acceptedDir + "/accepted" );
79- }
80- else if (osName .contains ("os x" )) {
94+ } else if (osName .contains ("os x" )) {
8195 String plistSourceLocation = "com.apple.java.util.prefs.plist" ;
8296 String plistDir = System .getProperty ("user.home" ) + "/Library/Preferences" ;
8397 copyFileFromJarResourceDir (plistSourceLocation , plistDir + "/com.apple.java.util.prefs.plist" );
@@ -95,16 +109,15 @@ else if (osName.contains("os x")) {
95109 } catch (IOException | InterruptedException e ) {
96110 e .printStackTrace ();
97111 }
98- }
99- else if (osName .contains ("windows" )) {
112+ } else if (osName .contains ("windows" )) {
100113 String acceptedSourceLocation = "accepted" ;
101114 String acceptedDir = System .getProperty ("user.home" ) + "\\ AppData\\ Roaming\\ JetBrains\\ consentOptions" ;
102115 createDirectoryHierarchy (acceptedDir );
103116 copyFileFromJarResourceDir (acceptedSourceLocation , acceptedDir + "\\ accepted" );
104117
105118 String registryPath = "HKCU:\\ Software\\ JavaSoft\\ Prefs\\ jetbrains\\ privacy_policy" ;
106119 ProcessBuilder pb1 = new ProcessBuilder ("powershell.exe" , "New-Item" , "-Path" , registryPath , "-Force" );
107- ProcessBuilder pb2 = new ProcessBuilder ("powershell.exe" , "New-ItemProperty" , "-Path" , registryPath , "-Name" , "accepted_version" , "-Value" , "\" 2.1\" " );
120+ ProcessBuilder pb2 = new ProcessBuilder ("powershell.exe" , "New-ItemProperty" , "-Path" , registryPath , "-Name" , "accepted_version" , "-Value" , "' 2.1' " );
108121
109122 try {
110123 Process p1 = pb1 .start ();
@@ -117,6 +130,39 @@ else if (osName.contains("windows")) {
117130 }
118131 }
119132
133+ private static void waitUntilIntelliJStarts (int port ) {
134+ waitFor (Duration .ofSeconds (600 ), Duration .ofSeconds (3 ), "The IntelliJ Idea did not start in 10 minutes." , () -> isIntelliJUIVisible (port ));
135+ }
136+
137+ private static boolean isIntelliJUIVisible (int port ) {
138+ return isHostOnIpAndPortAccessible ("127.0.0.1" , port );
139+ }
140+
141+ private static boolean isHostOnIpAndPortAccessible (String ip , int port ) {
142+ SocketAddress sockaddr = new InetSocketAddress (ip , port );
143+ Socket socket = new Socket ();
144+
145+ try {
146+ socket .connect (sockaddr , 10000 );
147+ } catch (IOException IOException ) {
148+ return false ;
149+ }
150+ return true ;
151+ }
152+
153+ public static RemoteRobot getRemoteRobotConnection (int port ) throws InterruptedException {
154+ RemoteRobot remoteRobot = new RemoteRobot ("http://127.0.0.1:" + port );
155+ for (int i = 0 ; i < 60 ; i ++) {
156+ try {
157+ remoteRobot .find (WelcomeFrameDialog .class );
158+ } catch (Exception ex ) {
159+ Thread .sleep (1000 );
160+ }
161+ }
162+
163+ return remoteRobot ;
164+ }
165+
120166 private static void createDirectoryHierarchy (String location ) {
121167 Path path = Paths .get (location );
122168 try {
0 commit comments