Hi, here is the complete code
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import agent1997.automation.framework.utilities.Utilities;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* For Starting and closing Appium Server.
* Make sure to always stop the server when not needed.
*/
public class AppiumServer {
private final AppiumServiceBuilder serviceBuilder = new AppiumServiceBuilder();
private AppiumDriverLocalService server;
private int port;
private final String appiumLogsLoc = "appium-logs";
private final String logFileName = "logs";
/**
* The server will start once it is instantiated
*/
public AppiumServer() {
this.port = Utilities.getAvailablePort();
this.serviceBuilder.usingPort(port);
this.server = AppiumDriverLocalService.buildService(serviceBuilder);
this.server.start();
}
public void stop() {
this.server.stop();
}
public AppiumDriverLocalService get(){
return this.server;
}
/**
* This will redirect log to another file.
* Will generate log file under 'appium-logs' directory
* Sample filename generated for the log file: logs_07_02_2021-11:48:13
*/
public void redirectLog() {
this.server.clearOutPutStreams();
File directory = new File(Utilities.getProjectDirectory() + this.appiumLogsLoc);
Utilities.makeDirIfItDoNotExist(directory);
try {
this.server.addOutPutStream(new FileOutputStream(Utilities.getProjectDirectory() + this.appiumLogsLoc + Utilities.generateFileName(logFileName)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}