1
+ /*
2
+ * Copyright © Magento, Inc. All rights reserved.
3
+ * See COPYING.txt for license details.
4
+ */
5
+
6
+ package com.magento.idea.magento2plugin.steps
7
+
8
+ import com.intellij.remoterobot.RemoteRobot
9
+ import com.intellij.remoterobot.fixtures.ContainerFixture
10
+ import com.intellij.remoterobot.fixtures.JTextFieldFixture
11
+ import com.intellij.remoterobot.search.locators.byXpath
12
+ import com.intellij.remoterobot.steps.CommonSteps
13
+ import com.intellij.remoterobot.stepsProcessing.step
14
+ import com.intellij.remoterobot.utils.keyboard
15
+ import com.intellij.remoterobot.utils.waitFor
16
+ import com.magento.idea.magento2plugin.pages.*
17
+ import java.awt.Point
18
+ import java.awt.event.KeyEvent.*
19
+ import java.io.File
20
+ import java.io.IOException
21
+ import java.nio.file.Paths
22
+ import java.time.Duration.ofMinutes
23
+ import java.util.*
24
+
25
+ class SharedSteps (private val remoteRobot : RemoteRobot ) {
26
+ private lateinit var tempProjectDir: File
27
+
28
+ fun createOrOpenTestProject (): File {
29
+ setupTemporaryMagentoProject()
30
+
31
+ step(" Create Or Open Test Project" , Runnable {
32
+ try {
33
+ remoteRobot.welcomeFrame {
34
+ val newProjectButton = remoteRobot.find(
35
+ ContainerFixture ::class .java,
36
+ byXpath(" //div[@visible_text='New Project']" )
37
+ )
38
+ newProjectButton.click()
39
+ Thread .sleep(2_000 )
40
+
41
+ val jTextFieldFixture = find<JTextFieldFixture >(byXpath(" //div[@class='TextFieldWithBrowseButton']" ))
42
+ jTextFieldFixture.click()
43
+ jTextFieldFixture.keyboard {
44
+ hotKey(VK_CONTROL , VK_A )
45
+ key(VK_DELETE )
46
+ enterText(tempProjectDir.absolutePath.toString().replace(" \\ " , " \\\\ " ))
47
+ }
48
+ keyboard { key(VK_ENTER ) }
49
+
50
+ dialog(" Directory Is Not Empty" ) {
51
+ button(" Create from Existing Sources" ).click()
52
+ }
53
+
54
+ enableMagentoSupport()
55
+ }
56
+ } catch (exception: Exception ) {
57
+ // temporary workaround until we get license for CI
58
+ activateIde()
59
+ // end temporary workaround
60
+ try {
61
+ val launchedFromScript = remoteRobot.find(
62
+ ContainerFixture ::class .java,
63
+ byXpath(" //div[@class='LinkLabel']" )
64
+ )
65
+ launchedFromScript.click()
66
+ } catch (e: Exception ) {
67
+ // Element does not exist, continue without failing the test
68
+ }
69
+
70
+ createProjectFromExistingFiles()
71
+ enableMagentoSupport()
72
+ }
73
+ })
74
+
75
+ return tempProjectDir;
76
+ }
77
+
78
+ fun closeProject () {
79
+ CommonSteps (remoteRobot).closeProject()
80
+ }
81
+
82
+ private fun setupTemporaryMagentoProject () {
83
+ // Create a parent directory and a random child directory inside it
84
+ val parentDir = Paths .get(" intellij-test-project" ).toFile()
85
+ if (parentDir.exists()) {
86
+ parentDir.deleteRecursively()
87
+ }
88
+ parentDir.mkdirs()
89
+
90
+ // Create a randomly named child directory inside the parent directory
91
+ tempProjectDir = File (parentDir, UUID .randomUUID().toString()).apply {
92
+ mkdirs()
93
+ }
94
+
95
+ // Define the source directory for the test data
96
+ val sourceDir = File (" testData/project/magento2" )
97
+
98
+ // Copy the test data to the temporary directory
99
+ sourceDir.copyRecursively(
100
+ target = tempProjectDir,
101
+ overwrite = true
102
+ )
103
+ }
104
+
105
+ private fun activateIde () {
106
+ if (" true" == System .getenv(" GITHUB_ACTIONS" )) {
107
+ val startTrial =
108
+ remoteRobot.find(ContainerFixture ::class .java, byXpath(" //div[@visible_text='Start trial']" ))
109
+ startTrial.click()
110
+
111
+ val startTrialFree = remoteRobot.find(ContainerFixture ::class .java, byXpath(" //div[@class='s']" ))
112
+ startTrialFree.click()
113
+
114
+ val dialog = remoteRobot.find(
115
+ DialogFixture ::class .java, byXpath(" //div[@class='MyDialog']" )
116
+ )
117
+ dialog.button(" Close" ).click()
118
+
119
+ try {
120
+ Thread .sleep(10000 )
121
+ } catch (e: InterruptedException ) {
122
+ Thread .currentThread().interrupt()
123
+ throw RuntimeException (e)
124
+ }
125
+
126
+ closeBrowser()
127
+ } else {
128
+ val dialog = remoteRobot.find(
129
+ DialogFixture ::class .java, byXpath(" //div[@class='MyDialog']" )
130
+ )
131
+ dialog.button(" Activate" ).click()
132
+ dialog.button(" Close" ).click()
133
+ }
134
+ }
135
+
136
+ private fun enableMagentoSupport () {
137
+ remoteRobot.idea {
138
+ step(" Enable Magento Integration" ) {
139
+ waitFor(ofMinutes(1 )) { isDumbMode().not () }
140
+ Thread .sleep(5_000 )
141
+ enableSupportLink.click(Point (1 , 1 ))
142
+ waitFor(ofMinutes(1 )) { isDumbMode().not () }
143
+
144
+ if (! isProjectViewVisible()) {
145
+ keyboard {
146
+ hotKey(VK_ALT , VK_1 )
147
+ }
148
+ }
149
+ }
150
+ }
151
+ }
152
+
153
+ private fun createProjectFromExistingFiles () {
154
+ remoteRobot.welcomeFrame {
155
+ try {
156
+ val launchedFromScript = find<ContainerFixture >(byXpath(" //div[@class='LinkLabel']" ))
157
+ launchedFromScript.click()
158
+ } catch (e: Exception ) {
159
+ // Element does not exist, continue without failing the test
160
+ }
161
+
162
+ createNewProjectFromExistingFilesLink.click()
163
+ selectProjectPath()
164
+ }
165
+ }
166
+
167
+ private fun WelcomeFrame.selectProjectPath () {
168
+ dialog(" Open File or Project" ) {
169
+ // Set the path for the copied test data
170
+ val comboBox = find<ContainerFixture >(byXpath(" //div[@class='BorderlessTextField']" ))
171
+ comboBox.click() // Focus on the comboBox
172
+ comboBox.keyboard {
173
+ hotKey(VK_CONTROL , VK_A ) // Select all text
174
+ key(VK_DELETE ) // Delete selected text
175
+ enterText(tempProjectDir.absolutePath.toString().replace(" \\ " , " \\\\ " ))
176
+ }
177
+
178
+ button(" OK" ).click()
179
+ trustProjectLink.click()
180
+ }
181
+ }
182
+
183
+ /* *
184
+ * Closes the browser by terminating its process based on the operating system.
185
+ */
186
+ fun closeBrowser () {
187
+ val os = System .getProperty(" os.name" ).lowercase(Locale .getDefault())
188
+
189
+ try {
190
+ if (os.contains(" win" )) {
191
+ // For Windows: Close common browsers like Chrome, Firefox, etc.
192
+ Runtime .getRuntime().exec(" taskkill /F /IM edge.exe" )
193
+ } else if (os.contains(" mac" )) {
194
+ // For macOS: Kill browsers using `pkill`
195
+ Runtime .getRuntime().exec(" killall -9 safari" )
196
+ } else if (os.contains(" nix" ) || os.contains(" nux" )) {
197
+ // For Linux-based systems: Kill typical browser processes
198
+ Runtime .getRuntime().exec(" killall -9 firefox" )
199
+ }
200
+ } catch (e: IOException ) {
201
+ e.printStackTrace()
202
+ }
203
+ }
204
+ }
0 commit comments