Skip to content

Commit 096b085

Browse files
The addition to #522 (#524)
* Make AppiumService aware of port number when using withArgument(...) method. * Use equalsIgnoreCase instead * changed to toLowerCase instead. * switched order of equals check. * code formating fixes. * Code formating fixes * Changed unit test names * The addition to #522 - the ability use ip and log file as server args was provided - test update/fix
1 parent 1243257 commit 096b085

File tree

3 files changed

+139
-7
lines changed

3 files changed

+139
-7
lines changed

src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,16 @@ public AppiumServiceBuilder withArgument(ServerArgument argument) {
237237
* @return the self-reference.
238238
*/
239239
public AppiumServiceBuilder withArgument(ServerArgument argument, String value) {
240-
serverArguments.put(argument.getArgument(), value);
240+
String argName = argument.getArgument().trim().toLowerCase();
241+
if ("--port".equalsIgnoreCase(argName) || "-p".equalsIgnoreCase(argName)) {
242+
usingPort(Integer.valueOf(value));
243+
} else if ("--address".equalsIgnoreCase(argName) || "-a".equalsIgnoreCase(argName)) {
244+
withIPAddress(value);
245+
} else if ("--log".equalsIgnoreCase(argName) || "-g".equalsIgnoreCase(argName)) {
246+
withLogFile(new File(value));
247+
} else {
248+
serverArguments.put(argName, value);
249+
}
241250
return this;
242251
}
243252

@@ -434,6 +443,7 @@ private String parseCapabilities() {
434443
* @param nodeJSExecutable The executable Node.js to use.
435444
* @return A self reference.
436445
*/
446+
@Override
437447
public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) {
438448
return super.usingDriverExecutable(nodeJSExecutable);
439449
}
@@ -445,6 +455,7 @@ public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) {
445455
* @param port The port to use; must be non-negative.
446456
* @return A self reference.
447457
*/
458+
@Override
448459
public AppiumServiceBuilder usingPort(int port) {
449460
return super.usingPort(port);
450461
}
@@ -454,6 +465,7 @@ public AppiumServiceBuilder usingPort(int port) {
454465
*
455466
* @return A self reference.
456467
*/
468+
@Override
457469
public AppiumServiceBuilder usingAnyFreePort() {
458470
return super.usingAnyFreePort();
459471
}
@@ -475,6 +487,7 @@ public AppiumServiceBuilder usingAnyFreePort() {
475487
* @param logFile A file to write log to.
476488
* @return A self reference.
477489
*/
490+
@Override
478491
public AppiumServiceBuilder withLogFile(File logFile) {
479492
return super.withLogFile(logFile);
480493
}

src/test/java/io/appium/java_client/android/UIAutomator2Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.appium.java_client.android;
22

3+
import static org.junit.Assert.assertEquals;
4+
35
import io.appium.java_client.remote.AutomationName;
46
import io.appium.java_client.remote.MobileCapabilityType;
57
import io.appium.java_client.service.local.AppiumDriverLocalService;
@@ -13,8 +15,6 @@
1315

1416
import java.io.File;
1517

16-
import static org.junit.Assert.assertEquals;
17-
1818
public class UIAutomator2Test {
1919
private static AppiumDriverLocalService service;
2020
protected static AndroidDriver<AndroidElement> driver;

src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private static File findCustomNode() {
222222
}
223223

224224
@Test public void checkAbilityToChangeOutputStream() throws Exception {
225-
File file = new File("target/test");
225+
File file = new File("test");
226226
file.createNewFile();
227227
OutputStream stream = new FileOutputStream(file);
228228
AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
@@ -243,7 +243,7 @@ private static File findCustomNode() {
243243
}
244244

245245
@Test public void checkAbilityToChangeOutputStreamAfterTheServiceIsStarted() throws Exception {
246-
File file = new File("target/test");
246+
File file = new File("test");
247247
file.createNewFile();
248248
OutputStream stream = new FileOutputStream(file);
249249
AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
@@ -295,8 +295,7 @@ private static File findCustomNode() {
295295

296296
@Test public void checkAbilityToStartServiceWithLogFile() throws Exception {
297297
AppiumDriverLocalService service = null;
298-
File rootLogDir = new File("target/");
299-
File log = new File(rootLogDir, "Log.txt");
298+
File log = new File("Log.txt");
300299
log.createNewFile();
301300
try {
302301
service = new AppiumServiceBuilder().withLogFile(log).build();
@@ -312,4 +311,124 @@ private static File findCustomNode() {
312311
}
313312
}
314313
}
314+
315+
@Test public void checkAbilityToBuildServiceWithPortUsingFlag() throws Exception {
316+
String port = "8996";
317+
String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port);
318+
319+
AppiumDriverLocalService service = null;
320+
321+
try {
322+
service = new AppiumServiceBuilder()
323+
.withArgument(() -> "--port", port)
324+
.build();
325+
service.start();
326+
String actualUrl = service.getUrl().toString();
327+
assertEquals(expectedUrl, actualUrl);
328+
} finally {
329+
if (service != null) {
330+
service.stop();
331+
}
332+
}
333+
}
334+
335+
@Test public void checkAbilityToBuildServiceWithPortUsingShortFlag() throws Exception {
336+
String port = "8996";
337+
String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port);
338+
339+
AppiumDriverLocalService service = null;
340+
341+
try {
342+
service = new AppiumServiceBuilder()
343+
.withArgument(() -> "-p", port)
344+
.build();
345+
service.start();
346+
String actualUrl = service.getUrl().toString();
347+
assertEquals(expectedUrl, actualUrl);
348+
} finally {
349+
if (service != null) {
350+
service.stop();
351+
}
352+
}
353+
}
354+
355+
@Test public void checkAbilityToBuildServiceWithIpUsingFlag() throws Exception {
356+
String expectedUrl = String.format("http://%s:%s/wd/hub", testIP, 4723);
357+
358+
AppiumDriverLocalService service = null;
359+
360+
try {
361+
service = new AppiumServiceBuilder()
362+
.withArgument(() -> "--address", testIP)
363+
.build();
364+
service.start();
365+
String actualUrl = service.getUrl().toString();
366+
assertEquals(expectedUrl, actualUrl);
367+
} finally {
368+
if (service != null) {
369+
service.stop();
370+
}
371+
}
372+
}
373+
374+
@Test public void checkAbilityToBuildServiceWithIpUsingShortFlag() throws Exception {
375+
String expectedUrl = String.format("http://%s:%s/wd/hub", testIP, 4723);
376+
377+
AppiumDriverLocalService service = null;
378+
379+
try {
380+
service = new AppiumServiceBuilder()
381+
.withArgument(() -> "-a", testIP)
382+
.build();
383+
service.start();
384+
String actualUrl = service.getUrl().toString();
385+
assertEquals(expectedUrl, actualUrl);
386+
} finally {
387+
if (service != null) {
388+
service.stop();
389+
}
390+
}
391+
}
392+
393+
@Test public void checkAbilityToBuildServiceWithLogFileUsingFlag() throws Exception {
394+
AppiumDriverLocalService service = null;
395+
396+
File log = new File("Log2.txt");
397+
398+
try {
399+
service = new AppiumServiceBuilder()
400+
.withArgument(() -> "--log", log.getAbsolutePath())
401+
.build();
402+
service.start();
403+
assertTrue(log.exists());
404+
} finally {
405+
if (service != null) {
406+
service.stop();
407+
}
408+
if (log.exists()) {
409+
log.delete();
410+
}
411+
}
412+
}
413+
414+
@Test public void checkAbilityToBuildServiceWithLogFileUsingShortFlag() throws Exception {
415+
AppiumDriverLocalService service = null;
416+
417+
File log = new File("Log3.txt");
418+
419+
try {
420+
service = new AppiumServiceBuilder()
421+
.withArgument(() -> "-g", log.getAbsolutePath())
422+
.build();
423+
service.start();
424+
assertTrue(log.exists());
425+
} finally {
426+
if (service != null) {
427+
service.stop();
428+
}
429+
if (log.exists()) {
430+
log.delete();
431+
}
432+
}
433+
}
315434
}

0 commit comments

Comments
 (0)