Skip to content

Conversation

@TikhomirovSergey
Copy link
Contributor

Change list

Test of the appium-specific DriverService implementation were improved/redesigned.

Types of changes

  • No changes in production code.
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Details

We are going to improve tests and make them runnable on many environments.
#325 #494


public class ServerBuilderTest {

private static String pathToAppiumNodeInProperties;
Copy link
Contributor

Choose a reason for hiding this comment

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

probably, we can mark properties, which are not going to be changed, as final

testIP = null;
}
}
if (testIP != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume having this in a separate private method would save us several lines of code (break -> return) and will improve readability.

Copy link
Contributor

Choose a reason for hiding this comment

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

the returned result can be of type Optional

}

ofNullable(file).ifPresent(fileArg -> {
if (file.exists()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

file or fileArg?

}

@Test public void checkAbilityToUseNodeDefinedExplicitly() {
File node = new File("src/test/java/io/appium/java_client/service/local/appium.js");
Copy link
Contributor

Choose a reason for hiding this comment

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

the magic string can be moved to constants

}

@Test public void checkAbilityToStartServiceUsingCapabilities() throws Exception {
File appDir = new File("src/test/java/io/appium/java_client");
Copy link
Contributor

Choose a reason for hiding this comment

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

there are many common root paths. The common part can be represented as a constant, so then one can use new File(parent, basename) constructor, where parent is a constant

service = buildDefaultService();
service.addOutPutStream(stream);
service.start();
assertTrue(file.length() > 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

-> assertThat(file.length(), greaterThan(0L))

Copy link
Contributor

Choose a reason for hiding this comment

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

this is a bit longer, but produces better error message in comparison to the generic True assertion

new AppiumServiceBuilder().usingAnyFreePort().build(),
new AppiumServiceBuilder().usingAnyFreePort().build(),
new AppiumServiceBuilder().usingAnyFreePort().build());
services.parallelStream().forEach(AppiumDriverLocalService::start);
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

service = new AppiumServiceBuilder().withLogFile(file).build();
service.start();
assertTrue(file.exists());
assertTrue(file.length() > 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

@mykola-mokhnach
Copy link
Contributor

Shall we add a description somewhere about src/test/java/io/appium/java_client/service/local/appium.js is just a stub?

Copy link
Contributor

@mykola-mokhnach mykola-mokhnach left a comment

Choose a reason for hiding this comment

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

see above

@TikhomirovSergey
Copy link
Contributor Author

@mykola-mokhnach @SrinivasanTarget
Code was improved

@appium appium deleted a comment Sep 17, 2017
@appium appium deleted a comment Sep 17, 2017
if (validator.isValid(result)) {
return result;
}
return null;
Copy link
Member

Choose a reason for hiding this comment

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

return validator.isValid(result) ? result : null;

if (!inetAddress.isLoopbackAddress()) {
InetAddressValidator validator = InetAddressValidator.getInstance();

testIP = ofNullable(testIP).orElseGet(() -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

this won't work as expected, because the iteration does not stop here even if testIP set. Please extract the loop into a separate method private static Optional<String> getTestIP(), so then beforeClass's body should look like testIP = getTestIP().orElseThrow(() -> new IllegalStateException("Cannot initialize the IP address for tests"))

*/
private static final String PATH_TO_APPIUM_NODE_IN_PROPERTIES = getProperty(APPIUM_PATH);

private static final String ROOT_TEST_PATH = "src/test/java/io/appium/java_client/";
Copy link
Contributor

Choose a reason for hiding this comment

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

trailing slash is not needed if path concatenation is done in File constructor

Copy link
Contributor

Choose a reason for hiding this comment

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

also, will these tests work properly in Windows if one hardcodes path separators?

/**
* This is the path to the stub appium.js file
*/
private static final String PATH_T0_TEST_APPIUM_JS = ROOT_TEST_PATH + "service/local/appium.js";
Copy link
Contributor

Choose a reason for hiding this comment

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

It will be more handy to keep it as File object:
TEST_APPIUM_JS = Path.get(ROOT_TEST_PATH).resolve("service").resolve("local").resolve("appium.js").toFile()

Copy link
Member

Choose a reason for hiding this comment

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

@TikhomirovSergey Why don't we name stub as main.js in sync with appium server's main.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SrinivasanTarget
Because it searches for appium.js when it tries to launch the node locally

Copy link
Member

@saikrishna321 saikrishna321 Sep 19, 2017

Choose a reason for hiding this comment

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

@TikhomirovSergey with the latest appium its main.js as @SrinivasanTarget mentioned

Copy link
Contributor Author

Choose a reason for hiding this comment

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

});

System.clearProperty(APPIUM_PATH);
ofNullable(PATH_TO_APPIUM_NODE_IN_PROPERTIES).ifPresent(s -> setProperty(APPIUM_PATH, s));
Copy link
Contributor

Choose a reason for hiding this comment

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

the constant cannot be null

Copy link
Contributor Author

@TikhomirovSergey TikhomirovSergey Sep 19, 2017

Choose a reason for hiding this comment

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

@mykola-mokhnach It may be null when the property was not defined


@Test public void checkAbilityToStartServiceUsingCapabilitiesAndFlags() throws Exception {
File app = new File(ROOT_TEST_PATH, "ApiDemos-debug.apk");
File chrome = new File(new File(ROOT_TEST_PATH, "pagefactory_tests"), "chromedriver.exe");
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Path.resolve method to construct longer paths

@TikhomirovSergey
Copy link
Contributor Author

Ok. Will improve it ASAP

@TikhomirovSergey
Copy link
Contributor Author

@mykola-mokhnach @SrinivasanTarget
Could you take a look one more time. It seems it should be finally resolved.

private File testLogFile;
private OutputStream stream;

private static String getLocalIP(NetworkInterface intf) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please add Nullable annotation

InetAddressValidator validator = InetAddressValidator.getInstance();
calculated = inetAddress.getHostAddress().toString();
if (validator.isValid(calculated)) {
result = calculated;
Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Sep 20, 2017

Choose a reason for hiding this comment

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

return calculated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will try it but codecy is sensetive to things like that

}
}
}
return result;
Copy link
Contributor

Choose a reason for hiding this comment

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

return null, result variable is not needed.

String result = null;
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
.hasMoreElements(); ) {
String calculated;
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the point to define calculated before if condition?

public void tearDown() throws Exception {
ofNullable(service).ifPresent(AppiumDriverLocalService::stop);

if (stream != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ofNullable(stream).ifPresent(OutputStream::close). Just to be consistent

Copy link
Contributor Author

@TikhomirovSergey TikhomirovSergey Sep 20, 2017

Choose a reason for hiding this comment

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

It throws the checked IO exception that needs to be handled by catch. So I decided to change nothing there because lambda with the catch clause looks not so cool.

}

@Test public void checkAbilityToFindNodeDefinedInProperties() {
File definedNode = PATH_T0_TEST_MAIN_JS.toFile();
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

Copy link
Contributor

@mykola-mokhnach mykola-mokhnach left a comment

Choose a reason for hiding this comment

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

Minor comments. Otherwise all good

@TikhomirovSergey TikhomirovSergey merged commit 9e3946c into appium:master Sep 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants