Skip to content

Avoid null pointer exceptions when calling getCapabilities #1094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public List findElementsByXPath(String using) {

@Override
public String toString() {
Capabilities capabilities = getCapabilities();
return String.format("%s, Capabilities: %s", getClass().getCanonicalName(),
getCapabilities().asMap().toString());
capabilities != null ? capabilities.asMap().toString() : "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'd rather use checkNotNull macros everywhere

Copy link
Contributor

Choose a reason for hiding this comment

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

or this is normal that getCapabilities can sometimes return null?

Copy link
Contributor Author

@etanol etanol Jan 31, 2019

Choose a reason for hiding this comment

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

getCapabilities will be null until the response of the new session request, where the real capabilities are returned by the server.

As I mentioned in the description, if the new session command fails (which can be pretty often when you are trying to set up the proper version combination of Appium, Automation and device), this NullPointerException masks the real failure (with no exception nesting).

So, overall, this makes the initial set up incredibly frustrating.

The idea of this change is to avoid NPEs altogether.

Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Jan 31, 2019

Choose a reason for hiding this comment

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

yep, I've checked the Selenium code and it's how it currently works. Although I would like to get the response to SeleniumHQ/selenium#6903 first. What if this is just a bug and not the expected behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm ok about learning the official posture. However, if RemoteWebDriver does indeed have a typo (missing a this.), then the exception masking would still remain. But instead of NullPointerException, a ClassCastException would be thrown:

MutableCapabilities capabilities = (MutableCapabilities) super.getCapabilities();

In such a case, the fix would be much uglier.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import javax.annotation.Nullable;

/**
* Android driver implementation.
Expand Down Expand Up @@ -206,9 +207,12 @@ public AndroidBatteryInfo getBatteryInfo() {
*
* @return given {@link Capabilities}
*/
@Nullable
public Capabilities getCapabilities() {
MutableCapabilities capabilities = (MutableCapabilities) super.getCapabilities();
capabilities.setCapability(PLATFORM_NAME, ANDROID_PLATFORM);
if (capabilities != null) {
capabilities.setCapability(PLATFORM_NAME, ANDROID_PLATFORM);
}
return capabilities;
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import javax.annotation.Nullable;

/**
* iOS driver implementation.
Expand Down Expand Up @@ -207,9 +208,12 @@ private class InnerTargetLocator extends RemoteTargetLocator {
*
* @return given {@link Capabilities}
*/
@Nullable
public Capabilities getCapabilities() {
MutableCapabilities capabilities = (MutableCapabilities) super.getCapabilities();
capabilities.setCapability(PLATFORM_NAME, IOS_PLATFORM);
if (capabilities != null) {
capabilities.setCapability(PLATFORM_NAME, IOS_PLATFORM);
}
return capabilities;
}

Expand Down