Skip to content

Commit 0522a5d

Browse files
authored
Merge pull request #5 from jenkinsci/master
Update
2 parents 6e4ba85 + 9d23850 commit 0522a5d

10 files changed

Lines changed: 151 additions & 58 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>org.jenkins-ci.plugins</groupId>
1313
<artifactId>katalon</artifactId>
14-
<version>1.0.3-SNAPSHOT</version>
14+
<version>1.0.7-SNAPSHOT</version>
1515
<packaging>hpi</packaging>
1616
<name>Katalon Studio Plugin</name>
1717
<description>Execute Katalon Studio in Jenkins</description>

src/main/java/com/katalon/jenkins/plugin/ExecuteKatalonStudioTask.java

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
import hudson.tasks.BuildStepDescriptor;
1111
import hudson.tasks.Builder;
1212
import net.sf.json.JSONObject;
13-
import org.apache.commons.lang3.StringUtils;
1413
import org.kohsuke.stapler.DataBoundConstructor;
1514
import org.kohsuke.stapler.StaplerRequest;
1615

17-
import java.io.File;
1816
import java.io.IOException;
19-
import java.nio.file.Paths;
2017

2118
public class ExecuteKatalonStudioTask extends Builder {
2219

@@ -26,11 +23,22 @@ public class ExecuteKatalonStudioTask extends Builder {
2623

2724
private String executeArgs;
2825

26+
private String x11Display;
27+
28+
private String xvfbConfiguration;
29+
2930
@DataBoundConstructor
30-
public ExecuteKatalonStudioTask(String version, String location, String executeArgs) {
31+
public ExecuteKatalonStudioTask(
32+
String version,
33+
String location,
34+
String executeArgs,
35+
String x11Display,
36+
String xvfbConfiguration) {
3137
this.version = version;
3238
this.location = location;
3339
this.executeArgs = executeArgs;
40+
this.x11Display = x11Display;
41+
this.xvfbConfiguration = xvfbConfiguration;
3442
}
3543

3644
public String getVersion() {
@@ -57,67 +65,53 @@ public void setExecuteArgs(String executeArgs) {
5765
this.executeArgs = executeArgs;
5866
}
5967

60-
private void executeKatalon(String katalonExecutableFile, String workSpace, BuildListener buildListener) throws IOException {
61-
File file = new File(katalonExecutableFile);
62-
if (!file.exists()) {
63-
file = new File(katalonExecutableFile + ".exe");
64-
}
65-
if (file.exists()) {
66-
file.setExecutable(true);
67-
}
68-
if (katalonExecutableFile.contains(" ")) {
69-
katalonExecutableFile = "\"" + katalonExecutableFile + "\"";
70-
}
71-
String command = katalonExecutableFile +
72-
" -noSplash " +
73-
" -runMode=console " +
74-
" -projectPath=\"" + workSpace + "\" " +
75-
this.executeArgs;
68+
public String getX11Display() {
69+
return x11Display;
70+
}
7671

77-
OsUtils.runCommand(buildListener, command);
72+
public void setX11Display(String x11Display) {
73+
this.x11Display = x11Display;
74+
}
75+
76+
public String getXvfbConfiguration() {
77+
return xvfbConfiguration;
78+
}
79+
80+
public void setXvfbConfiguration(String xvfbConfiguration) {
81+
this.xvfbConfiguration = xvfbConfiguration;
7882
}
7983

8084
@Override
81-
public boolean perform(AbstractBuild<?, ?> abstractBuild, Launcher launcher, BuildListener buildListener) throws InterruptedException, IOException {
85+
public boolean perform(AbstractBuild<?, ?> abstractBuild, Launcher launcher, BuildListener buildListener)
86+
throws InterruptedException, IOException {
8287
try {
88+
8389
FilePath workspace = abstractBuild.getWorkspace();
8490

8591
if (workspace != null) {
8692
String workspaceLocation = workspace.getRemote();
8793

8894
if (workspaceLocation != null) {
8995

90-
String katalonDirPath;
91-
92-
if (StringUtils.isBlank(this.location)) {
93-
File katalonDir = KatalonUtils.getKatalonPackage(buildListener, this.version);
94-
katalonDirPath = katalonDir.getAbsolutePath();
95-
} else {
96-
katalonDirPath = this.location;
97-
}
98-
99-
LogUtils.log(buildListener, "Using Katalon Studio at " + katalonDirPath);
100-
String katalonExecutableFile;
101-
String os = OsUtils.getOSVersion(buildListener);
102-
if (os.contains("macos")) {
103-
katalonExecutableFile = Paths.get(katalonDirPath, "Contents", "MacOS", "katalon")
104-
.toAbsolutePath()
105-
.toString();
106-
} else {
107-
katalonExecutableFile = Paths.get(katalonDirPath, "katalon")
108-
.toAbsolutePath()
109-
.toString();
110-
}
111-
executeKatalon(katalonExecutableFile, workspaceLocation, buildListener);
96+
return KatalonUtils.executeKatalon(
97+
buildListener,
98+
this.version,
99+
this.location,
100+
workspaceLocation,
101+
this.executeArgs,
102+
this.x11Display,
103+
this.xvfbConfiguration);
112104

113105
}
114106
}
115107

108+
return true;
109+
116110
} catch (Exception e) {
117111
String stackTrace = Throwables.getStackTraceAsString(e);
118112
LogUtils.log(buildListener, stackTrace);
113+
return false;
119114
}
120-
return true;
121115
}
122116

123117
@Extension

src/main/java/com/katalon/jenkins/plugin/KatalonUtils.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hudson.FilePath;
66
import hudson.model.BuildListener;
77
import org.apache.commons.io.FileUtils;
8+
import org.apache.commons.lang3.StringUtils;
89

910
import java.io.File;
1011
import java.io.IOException;
@@ -134,4 +135,73 @@ static File getKatalonPackage(
134135

135136
return katalonContainingDir;
136137
}
138+
139+
private static boolean executeKatalon(
140+
BuildListener buildListener,
141+
String katalonExecutableFile,
142+
String projectPath,
143+
String executeArgs,
144+
String x11Display,
145+
String xvfbConfiguration)
146+
throws IOException, InterruptedException {
147+
File file = new File(katalonExecutableFile);
148+
if (!file.exists()) {
149+
file = new File(katalonExecutableFile + ".exe");
150+
}
151+
if (file.exists()) {
152+
file.setExecutable(true);
153+
}
154+
if (katalonExecutableFile.contains(" ")) {
155+
katalonExecutableFile = "\"" + katalonExecutableFile + "\"";
156+
}
157+
String command = katalonExecutableFile +
158+
" -noSplash " +
159+
" -runMode=console ";
160+
if (!executeArgs.contains("-projectPath")) {
161+
command += " -projectPath=\"" + projectPath + "\" ";
162+
}
163+
command += " " + executeArgs + " ";
164+
165+
return OsUtils.runCommand(buildListener, command, x11Display, xvfbConfiguration);
166+
}
167+
168+
public static boolean executeKatalon(
169+
BuildListener buildListener,
170+
String version,
171+
String location,
172+
String projectPath,
173+
String executeArgs,
174+
String x11Display,
175+
String xvfbConfiguration)
176+
throws IOException, InterruptedException {
177+
178+
String katalonDirPath;
179+
180+
if (StringUtils.isBlank(location)) {
181+
File katalonDir = KatalonUtils.getKatalonPackage(buildListener, version);
182+
katalonDirPath = katalonDir.getAbsolutePath();
183+
} else {
184+
katalonDirPath = location;
185+
}
186+
187+
LogUtils.log(buildListener, "Using Katalon Studio at " + katalonDirPath);
188+
String katalonExecutableFile;
189+
String os = OsUtils.getOSVersion(buildListener);
190+
if (os.contains("macos")) {
191+
katalonExecutableFile = Paths.get(katalonDirPath, "Contents", "MacOS", "katalon")
192+
.toAbsolutePath()
193+
.toString();
194+
} else {
195+
katalonExecutableFile = Paths.get(katalonDirPath, "katalon")
196+
.toAbsolutePath()
197+
.toString();
198+
}
199+
return executeKatalon(
200+
buildListener,
201+
katalonExecutableFile,
202+
projectPath,
203+
executeArgs,
204+
x11Display,
205+
xvfbConfiguration);
206+
}
137207
}

src/main/java/com/katalon/jenkins/plugin/OsUtils.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
import hudson.model.BuildListener;
44
import org.apache.commons.io.IOUtils;
5+
import org.apache.commons.lang3.StringUtils;
56
import org.apache.commons.lang3.SystemUtils;
67

7-
import java.io.BufferedReader;
8-
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.io.InputStreamReader;
8+
import java.io.*;
119
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.util.ArrayList;
1213
import java.util.Arrays;
14+
import java.util.List;
1315

1416
class OsUtils {
1517

@@ -43,16 +45,29 @@ static String getOSVersion(BuildListener buildListener) {
4345
return "";
4446
}
4547

46-
static void runCommand(BuildListener buildListener, String command) throws IOException {
48+
static boolean runCommand(
49+
BuildListener buildListener,
50+
String command,
51+
String x11Display,
52+
String xvfbConfiguration)
53+
throws IOException, InterruptedException {
4754

4855
String[] cmdarray;
4956
if (SystemUtils.IS_OS_WINDOWS) {
5057
cmdarray = Arrays.asList("cmd", "/c", command).toArray(new String[]{});
5158
} else {
52-
cmdarray = Arrays.asList("sh", "-c", command).toArray(new String[]{});
59+
if (!StringUtils.isBlank(x11Display)) {
60+
command = "DISPLAY=" + x11Display + " " + command;
61+
}
62+
if (!StringUtils.isBlank(xvfbConfiguration)) {
63+
command = "xvfb-run " + xvfbConfiguration + " " + command;
64+
}
65+
List<String> cmdlist = Arrays.asList("sh", "-c", command);
66+
cmdarray = cmdlist.toArray(new String[]{});
5367
}
54-
LogUtils.log(buildListener, "Execute " + command);
55-
Process cmdProc = Runtime.getRuntime().exec(cmdarray);
68+
Path workingDirectory = Files.createTempDirectory("katalon-");
69+
LogUtils.log(buildListener, "Execute " + Arrays.toString(cmdarray) + " in " + workingDirectory);
70+
Process cmdProc = Runtime.getRuntime().exec(cmdarray, null, workingDirectory.toFile());
5671
try (
5772
BufferedReader stdoutReader = new BufferedReader(
5873
new InputStreamReader(
@@ -67,5 +82,7 @@ static void runCommand(BuildListener buildListener, String command) throws IOExc
6782
LogUtils.log(buildListener, line);
6883
}
6984
}
85+
cmdProc.waitFor();
86+
return cmdProc.exitValue() == 0;
7087
}
7188
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
<?jelly escape-by-default='true'?>
22
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
33

4-
<f:entry title="Download Katalon Studio version (e.g. 5.8.3)" field="version">
4+
<f:entry title="Download Katalon Studio version" field="version">
55
<f:textbox />
66
</f:entry>
77

88
<f:entry title="Use pre-installed Katalon Studio" field="location">
99
<f:textbox />
1010
</f:entry>
1111

12-
<f:entry title="Command arguments (except -runMode and -projectPath)" field="executeArgs">
12+
<f:entry title="Command arguments" field="executeArgs">
1313
<f:textarea />
1414
</f:entry>
1515

16+
<f:entry title="X11 DISPLAY (for Linux)" field="x11Display">
17+
<f:textbox />
18+
</f:entry>
19+
20+
<f:entry title="Xvfb-run configuration (for Linux)" field="xvfbConfiguration">
21+
<f:textbox />
22+
</f:entry>
23+
1624
</j:jelly>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div>E.g. -browserType="Chrome" -retry=0 -statusDelay=15 -testSuitePath="Test Suites/Regression Tests/All tests".</div>
1+
<div>E.g. <code>-browserType="Chrome" -retry=0 -statusDelay=15 -testSuitePath="Test Suites/Regression Tests/All tests"</code>. Please leave out <code>-runMode</code>. If not specified, <code>-projectPath</code> will be set to the current workspace directory.</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>E.g. <code>/var/lib/jenkins/Katalon_Studio_Linux_64-5.10.1</code>. Use this field when Katalon Studio cannot be downloaded automatically (often due to network conditions).</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div>The list of all releases can be retrieved from <a href="https://github.com/katalon-studio/katalon-studio/releases" target="_blank">here</a>.</div>
1+
<div>E.g. <code>5.10.1</code>. The list of all releases can be retrieved from <a href="https://github.com/katalon-studio/katalon-studio/releases" target="_blank">here</a>.</div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div>E.g. <code>:1</code>. This value will be used as the <code>DISPLAY</code> environment variable.
2+
Jenkins must be allowed to connect to the display, see <code>xhost</code> if you encounter access control issues.</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>E.g. <code>-a -n 0 -s "-screen 0 1024x768x24"</code>. If specified, <code>xvfb-run</code> will be used. Please make sure Xvfb has been installed.</div>

0 commit comments

Comments
 (0)