Unable to get log in eclipse while running testcases with new Appium studio (11.0.34)

Hi,

When i am running testcases from eclipse … i am unable to get log (i.e actions its performing with capabilities and finding element and actions) with errors …

When i was using 10.0 version i was getting log in eclipse console . But in 11.0.34 i am unable to get console log.

Could you please help on this.

Thanks in advance.

Regards,
Sudhakar MK

Hi @sudhakar

You might want to override the driver log method.
You mentioned that you were using Eclipse so I assume you are using Java.

If so, see the attached code that extends the driver classes and overrides the log method:

iOS

import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.SessionId;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NewIOSDriver extends IOSDriver {
private String deviceID = null;
private String deviceName = null;

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    public NewIOSDriver(URL remoteAddress, Capabilities desiredCapabilities) {
        super(remoteAddress, desiredCapabilities);
        try {
            this.deviceID = (String) desiredCapabilities.getCapability("udid");
            this.deviceName = ((String) desiredCapabilities.getCapability("deviceName")).replace(" ", "_").replace("'", "-").trim();

        } catch (Exception e) {
            System.out.println("No Id or Name");
        }

    }

    @Override
    protected void log(SessionId sessionId, String commandName, Object toLog, When when) {
        if (commandName.equals("newSession")) sdf = new SimpleDateFormat("HH:mm:ss");
        System.out.println(sdf.format(new Date(System.currentTimeMillis())) + ": " + deviceID + " - " + when + ": " + commandName + " toLog:" + toLog);
        super.log(sessionId, commandName, toLog, when);
        if (deviceName != null) {
            System.out.println(deviceName +" - "+ sdf.format(new Date(System.currentTimeMillis())) + when + ": " + commandName + " toLog:" + toLog);
        }
    }
}

Android

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.SessionId;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NewAndroidDriver extends AndroidDriver {
    private final String deviceID;
    private String deviceName = "";
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    public NewAndroidDriver(URL remoteAddress, Capabilities desiredCapabilities) {

        super(remoteAddress, desiredCapabilities);

        this.deviceID = (String) desiredCapabilities.getCapability("udid");
        try {
            this.deviceName = ((String) desiredCapabilities.getCapability("deviceName")).replace(" ", "_").replace("'", "-").trim();
        } catch (Exception e) {
            this.deviceName = deviceID;
        }

    }

    @Override
    protected void log(SessionId sessionId, String commandName, Object toLog, When when) {
        if (commandName.equals("newSession")) sdf = new SimpleDateFormat("HH:mm:ss");
        super.log(sessionId, commandName, toLog, when);

        System.out.println(sdf.format(new Date(System.currentTimeMillis())) + ": " + deviceID + " - " + when + ": " + commandName + " toLog:" + toLog);
        if (deviceName != null) {
            System.out.println(deviceName + sdf.format(new Date(System.currentTimeMillis())) + when + ": " + commandName + " toLog:" + toLog);

        }
    }

}

Add them in your project and initialize the driver accordingly:
driver = new NewIOSDriver(new URL(“http://localhost:4723/wd/hub”), dc);
OR
driver = new NewAndroidDriver(new URL(“http://localhost:4723/wd/hub”), dc);

Hi Nivi,

Thanks for reply. This will not help me, because i am my way of calling capabilities is different …

I am calling my capabilities in before method…

Is there any solution will be great.

Thanks in advance.

Hi @sudhakar

When you say that you are calling capabilities in @BeforeMethod, do you mean that it is there where specify them? Capabilities such as UDID and BundleID among others?

If so, then you don’t need to change the way to set capabilities in the @BeforeMethod. The only difference is that you are not using the original driver class but another class that extends it.

If I misunderstood you, can you please paste your code in your reply so we can test it?

Hi

@BeforeMethod
public static void setcaps(Method m) throws MalformedURLException {
String testname = m.getName();

				File classpathRoot = new File(System.getProperty("user.dir"));
				File appDir = new File(classpathRoot, "Application");
				File app = new File(appDir, "MGM_Resorts.ipa");
				DesiredCapabilities dc = new DesiredCapabilities();
				  dc.setCapability("reportDirectory", "reportDirectory  ");
				  dc.setCapability("reportFormat", "html");
				  dc.setCapability("testName", testname);
				  dc.setCapability(MobileCapabilityType.PLATFORM, "ios");
				//dc.setCapability(MobileCapabilityType.UDID,prop.readPropFile("UDID"));

                          dc.setCapability(IOSMobileCapabilityType.BUNDLE_ID,prop.readPropFile("bundleId"));
				 driver = new IOSDriver<IOSElement>(new URL("http://localhost:4723/wd/hub"),dc);

}
}

These above capabilities are in one class and i created my test class separately

In my test class extending my capability class.

Thanks

What happens if you replace
driver = new IOSDriver<IOSElement>(new URL("http://localhost:4723/wd/hub"),dc);

with

driver = new NewIOSDriver<IOSElement>(new URL("http://localhost:4723/wd/hub"),dc);

After having created the new NewIOSDriver class and initializing the driver as NewIOSDriver?

I’m not sure about the capabilities. Do you configure capabilities in one class and then some more in a test class? In the code you provided you define capabilities and then pass them when you create the driver, so I’m not sure about the purpose of adding more capabilities in a test class. You can pass the driver to the test class if that solves your problem.

I am getting attached error…

Hi @sudhakar

In that case, add <IOSElement> after the name of the new class name:

public class NewIOSDriver<IOSElement> extends IOSDriver

This will fix the error.

Can the above cannot be set by some desired capabilities like “log” with value True or False?

Rather than creating a new class and invoking them for driver creation lacks readability. Simple solution could be some capability with Yes/No flag.

Even this way its not working…I am unable to get log print in eclipse console.

@sudhakar

I will try to create a git repo with sample code for you to clone and test.

@sudhakar

Go to this git hub repo - https://github.com/ExperitestOfficial/appium-log

Download the project as zip and extract it to a desired location.
Open Eclipse
Go to File → Import → Gradle → Existing Gradle Project.
Right click on build.gradle → Gradle → Refresh Gradle Project

After that, if you run tests, you will be able to see the log in the console:

1 Like

Hi Nivi,

Thank you so much …Its working fine.

Regards…