Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.appium.java_client.service.local;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -217,13 +219,23 @@ public AppiumServiceBuilder withArgument(ServerArgument argument, String value)
case "-g":
withLogFile(new File(value));
break;
case "--base-path":
serverArguments.put(argName, sanitizeBasePath(value));
break;
default:
serverArguments.put(argName, value);
break;
}
return this;
}

private static String sanitizeBasePath(String basePath) {
basePath = checkNotNull(basePath).trim();
checkArgument(!basePath.isEmpty(),
"Given base path is not valid - blank or empty values are not allowed for base path");
return basePath.endsWith("/") ? basePath : basePath + "/";
}

/**
* Adds capabilities.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import static io.appium.java_client.TestUtils.getLocalIp4Address;
import static io.appium.java_client.service.local.AppiumDriverLocalService.buildDefaultService;
import static io.appium.java_client.service.local.AppiumServiceBuilder.APPIUM_PATH;
import static io.appium.java_client.service.local.AppiumServiceBuilder.BROADCAST_IP_ADDRESS;
import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
import static io.appium.java_client.service.local.flags.GeneralServerFlag.BASEPATH;
import static io.appium.java_client.service.local.flags.GeneralServerFlag.CALLBACK_ADDRESS;
import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE;
import static io.github.bonigarcia.wdm.WebDriverManager.chromedriver;
Expand All @@ -18,6 +21,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -312,4 +316,45 @@ public void checkAbilityToStartServiceWithLogFileUsingShortFlag() {
service.start();
assertTrue(testLogFile.exists());
}

@Test
public void checkAbilityToStartServiceUsingValidBasePathWithMultiplePathParams() {
String baseUrl = String.format("http://%s:%d/", BROADCAST_IP_ADDRESS, DEFAULT_APPIUM_PORT);
String basePath = "wd/hub";
service = new AppiumServiceBuilder().withArgument(BASEPATH, basePath).build();
service.start();
assertTrue(service.isRunning());
assertEquals(baseUrl + basePath + "/", service.getUrl().toString());
}

@Test
public void checkAbilityToStartServiceUsingValidBasePathWithSinglePathParams() {
String baseUrl = String.format("http://%s:%d/", BROADCAST_IP_ADDRESS, DEFAULT_APPIUM_PORT);
String basePath = "/wd/";
service = new AppiumServiceBuilder().withArgument(BASEPATH, basePath).build();
service.start();
assertTrue(service.isRunning());
assertEquals(baseUrl + basePath.substring(1), service.getUrl().toString());
}

@Test
public void checkAbilityToValidateBasePathForEmptyBasePath() {
assertThrows(IllegalArgumentException.class, () -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: you could add expected exception signature to Test annotations: https://www.guru99.com/junit-exception-test.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted with thanks.

new AppiumServiceBuilder().withArgument(BASEPATH, "").build();
});
}

@Test
public void checkAbilityToValidateBasePathForBlankBasePath() {
assertThrows(IllegalArgumentException.class, () -> {
new AppiumServiceBuilder().withArgument(BASEPATH, " ").build();
});
}

@Test
public void checkAbilityToValidateBasePathForNullBasePath() {
assertThrows(NullPointerException.class, () -> {
new AppiumServiceBuilder().withArgument(BASEPATH, null).build();
});
}
}