'Class' or 'Interface' expected

I’m trying to write a test for android in java.

I’m trying to set my desired capabilities and I’m getting an error that states ‘Class’ or ‘Interface’ expected.

Any help would be appreciated!

@Tom_Cockram you need like:

        File appDir = new File("src/test/java/io/appium/java_client");
        File app = new File(appDir, "ApiDemos-debug.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
        capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AndroidDriver<>(service.getUrl(), capabilities);

Thanks, I’m not getting that error now but a different one where it says it can’t find symbols when I run the test.

@Tom_Cockram

Seems like your issue is not related to appium, but java itself :slight_smile:

Your cap should be caps instead. Look at your code: DesiredCapabilities caps.

Is the object driver and capabilities declared inside your class?

Java won’t guess where you are getting those names from. So for that you’ll need something like

public class Test {
	AppiumDriver driver;
	DesiredCapabitities capabilities;

	public static void main(String [] args){ 
		//on your main just save driver and capabilities to driver and capabilities objects, that do exist globally on this class
		capabilities = new DesiredCapabilities;
		//set your capabilities using somethign like:
		capabilites.setCapability(...);

		//Other stuff

		//Same for the driver object
		try {
			driver = new AndroidDriver(...);
		}
	}

	@Test
	public void Test(){
		driver = new AndroidDriver(...);
	}
}

That way you won’t even need the previous caps object.
That way you will be able to use driver and capabilities wherever you want, on THIS class. Just be sure to check if they are != null so it won’t throw NullPointerException :slight_smile:
This will work, however this may not be the best solution, depending on your use cases :slight_smile:
Hope it helps

EDIT
I fixed the code (was missing your methods).

Hi,

Yes issues definitely my java skills not Appium at the moment!

I have changed cap to caps as you mentioned. However I’m getting a problem that says:

image

My whole AppiumTest class code is below.

package tests;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;

public class AppiumTest {

public static void main(String[] args) {

    //Set the Desired Capabilities
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability(MobileCapabilityType.DEVICE_NAME, "ZY224Gs7NG");
    caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android2.calculator3");
    caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.xlythe.calculator.material.Calculator");
    caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "MobilePlatform.ANDROID");

    //Instantiate Appium Driver
    try {
        AppiumDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), caps);

    } catch (MalformedURLException e) {
        System.out.println(e.getMessage());
    }
}

@Test
public void Test() {
try {
driver = new AndroidDriver(new URL(“http://0.0.0.0:4723/wd/hub”), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
// Click on number 7
WebElement sevenKey = driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,‘button7’)]"));
sevenKey.click();
// Click on ‘+’
WebElement plusKey = driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,‘buttonPlus’)]"));
plusKey.click();
// Click on number 5
WebElement fiveKey = driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,‘button5’)]"));
fiveKey.click();
// Click on ‘=’
WebElement equalsKey = driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,‘buttonEquals’)]"));
equalsKey.click();
// Check the total is correct
WebElement total = driver.findElement(By.xpath("//android.widget.EditText[contains(@resource-id,‘textField’)]"));
String totalValue = total.getText();
Assert.assertEquals(“12”, totalValue);
}}

@Tom_Cockram

I edited the code. See if it is clearer now. (My fault)
Also, I think you should search for easier examples related to Java, on how to store values and re-use it on another methods, because that is your issue. You are trying to use an object driver that does not exist on that method. Although you do have another driver object created on main as far as we know, they can be different objects, so you either do something like:

    @Test
	public void Test(){
		AppiumDriver driver = new AndroidDriver(...);
	}

Which will create another driver object (distinct from the other one on main method) or follow the example I gave you (having a class global object, which will be the same on all methods). The same applies for capabilities object.

Thanks for your help. I have managed to fix the problems with some help.

I’m now running into this problem when I try and run the tests:

I have managed to fix this. I changed the Xpaths to be correct and it now works!