"FAILED CONFIGURATION: @BeforeMethod androidSec.AndroidSec.setUp" Could not start a new session

38 views Asked by At

Have attached my Selenium Java TestNG code for Android app ZAP Passive scan Security testing below the error. When I Run this code as TestNG am getting error:

FAILED CONFIGURATION: @BeforeMethod androidSec.AndroidSec.setUp org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: An unknown server-side error occurred while processing the command. Original error: Cannot start the 'com.vajro.robin' application. Visit https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/android/activity-startup.md for troubleshooting. Original error: Error executing adbExec. Original error: 'Command 'C:\Users\ashwi\AppData\Local\Android\Sdk\platform-tools\adb.exe -P **** -s ******** shell am start -W -n *******/***********-S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000' exited with code 255'; Command output: Exception occurred while executing 'start': java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.vajro.robin/.kotlin.ui.preview.activity.PreviewActivityKt } from null (pid=8181, uid=2000) not exported from uid 10326 at com.android.server.wm.ActivityTaskSupervisor.checkStartAnyActivityPermission(ActivityTaskSupervisor.java:1334) at com.android.server.wm.ActivityStarter.executeRequest(ActivityStarter.java:1275) at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:906) at com.android.server.wm.ActivityTaskManagerService.startActivityAndWait(ActivityTaskManagerService.java:2129) at com.android.server.am.ActivityManagerService.startActivityAndWait(ActivityManagerService.java:3552) at com.android.server.am.ActivityManagerShellCommand.runStartActivity(ActivityManagerShellCommand.java:619) at com.android.server.am.ActivityManagerShellCommand.onCommand(ActivityManagerShellCommand.java:204) at com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97) at android.os.ShellCommand.exec(ShellCommand.java:38) at com.android.server.am.ActivityManagerService.onShellCommand(ActivityManagerService.java:10537) at android.os.Binder.shellCommand(Binder.java:986) at android.os.Binder.onTransact(Binder.java:860) at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:6103) at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3034) at android.os.Binder.execTransactInternal(Binder.java:1220) at android.os.Binder.execTransact(Binder.java:1179)

Host info: host: 'LAPTOP-02J6GAU0', ip: '192.168.29.119' Build info: version: '4.12.1', revision: '8e34639b11' System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '20.0.1' Driver info: io.appium.java_client.android.AndroidDriver Command: [null, newSession {capabilities=[{appium:appActivity=, appium:appPackage=*, appium:automationName=UiAutomator2, appium:deviceName=Android, platformName=ANDROID}], desiredCapabilities=Capabilities {appActivity: ********..., appPackage: ******, automationName: UiAutomator2, deviceName: Android, platformName: ANDROID}}] Capabilities {appActivity: *********..., appPackage: *********, automationName: UiAutomator2, deviceName: Android, platformName: ANDROID} at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:578) at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:133) at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:102) at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:182) at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:250) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602) at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:274) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:163) at io.appium.java_client.AppiumDriver.(AppiumDriver.java:89) at io.appium.java_client.AppiumDriver.(AppiumDriver.java:101) at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:113) at androidSec.AndroidSec.setUp(AndroidSec.java:41) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:578) at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139) at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:69) at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:390) at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:325) at org.testng.internal.invokers.TestInvoker.runConfigMethods(TestInvoker.java:810) at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:577) at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:227) at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50) at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:957) at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:200) at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148) at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.testng.TestRunner.privateRun(TestRunner.java:848) at org.testng.TestRunner.run(TestRunner.java:621) at org.testng.SuiteRunner.runTest(SuiteRunner.java:443) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397) at org.testng.SuiteRunner.run(SuiteRunner.java:336) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280) at org.testng.TestNG.runSuitesLocally(TestNG.java:1200) at org.testng.TestNG.runSuites(TestNG.java:1114) at org.testng.TestNG.run(TestNG.java:1082) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

SKIPPED CONFIGURATION: @AfterMethod androidSec.AndroidSec.tearDown SKIPPED: androidSec.AndroidSec.testApp org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: An unknown server-side error occurred while processing the command. Original error: Cannot start the 'com.vajro.robin' application. Visit https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/android/activity-startup.md for troubleshooting. Original error: Error executing adbExec. Original error: 'Command 'C:\Users\ashwi\AppData\Local\Android\Sdk\platform-tools\adb.exe -P 5037 -s ******** shell am start -W -n /****** -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000' exited with code 255'; Command output: Exception occurred while executing 'start': java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=/*} from null (pid=8181, uid=2000) not exported from uid 10326 at com.android.server.wm.ActivityTaskSupervisor.checkStartAnyActivityPermission(ActivityTaskSupervisor.java:1334) at com.android.server.wm.ActivityStarter.executeRequest(ActivityStarter.java:1275) at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:906) at com.android.server.wm.ActivityTaskManagerService.startActivityAndWait(ActivityTaskManagerService.java:2129) at com.android.server.am.ActivityManagerService.startActivityAndWait(ActivityManagerService.java:3552) at com.android.server.am.ActivityManagerShellCommand.runStartActivity(ActivityManagerShellCommand.java:619) at com.android.server.am.ActivityManagerShellCommand.onCommand(ActivityManagerShellCommand.java:204) at com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97) at android.os.ShellCommand.exec(ShellCommand.java:38) at com.android.server.am.ActivityManagerService.onShellCommand(ActivityManagerService.java:10537) at android.os.Binder.shellCommand(Binder.java:986) at android.os.Binder.onTransact(Binder.java:860) at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:6103) at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3034) at android.os.Binder.execTransactInternal(Binder.java:1220) at android.os.Binder.execTransact(Binder.java:1179)

Host info: host: 'LAPTOP-', ip: '0.0.0.0' Build info: version: '4.12.1', revision: '00000000' System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '20.0.1' Driver info: io.appium.java_client.android.AndroidDriver Command: [null, newSession {capabilities=[{appium:appActivity=, appium:appPackage=****, appium:automationName=UiAutomator2, appium:deviceName=Android, platformName=ANDROID}], desiredCapabilities=Capabilities {appActivity: *******., appPackage: ********, automationName: UiAutomator2, deviceName: Android, platformName: ANDROID}}] Capabilities {appActivity: *****..., appPackage: *******, automationName: UiAutomator2, deviceName: Android, platformName: ANDROID} at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:578) at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:133) at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:102) at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:182) at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:250) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602) at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:274) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:163) at io.appium.java_client.AppiumDriver.(AppiumDriver.java:89) at io.appium.java_client.AppiumDriver.(AppiumDriver.java:101) at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:113) at androidSec.AndroidSec.setUp(AndroidSec.java:41) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:578) at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139) at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:69) at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:390) at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:325) at org.testng.internal.invokers.TestInvoker.runConfigMethods(TestInvoker.java:810) at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:577) at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:227) at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50) at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:957) at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:200) at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148) at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.testng.TestRunner.privateRun(TestRunner.java:848) at org.testng.TestRunner.run(TestRunner.java:621) at org.testng.SuiteRunner.runTest(SuiteRunner.java:443) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397) at org.testng.SuiteRunner.run(SuiteRunner.java:336) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280) at org.testng.TestNG.runSuitesLocally(TestNG.java:1200) at org.testng.TestNG.runSuites(TestNG.java:1114) at org.testng.TestNG.run(TestNG.java:1082) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Code:

package androidSec;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zaproxy.clientapi.core.ApiResponse;
import org.zaproxy.clientapi.core.ApiResponseElement;
import org.zaproxy.clientapi.core.ClientApi;
import org.zaproxy.clientapi.core.ClientApiException;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;

public class AndroidSec {
    public static Proxy proxy;
    private AndroidDriver driver;
    private ClientApi zapClient;
    private static final String zapAddress = "#####";
    private static final int zapPort = ####;
    private static final String apiKey = "###########"; // Replace with your ZAP API key

    @BeforeMethod
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
        capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "########");
        capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "######");
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "####");
        try {
            URI uri = new URI("#########");
            URL url = uri.toURL();
            driver = new AndroidDriver(url, capabilities);
            Thread.sleep(5000);
        } catch (URISyntaxException | MalformedURLException | InterruptedException e) {
            e.printStackTrace();
        }
        zapClient = new ClientApi(zapAddress, zapPort, apiKey);
        proxy = new Proxy().setSslProxy(zapAddress + ":" + zapPort).setHttpProxy(zapAddress + ":" + zapPort);
    }

    @Test
    public void testApp() {
        launchApp();
        triggerZapPassiveScan1();
    }

    private void launchApp() {
        // Your code for launching the app goes here
        String appPackage = "###########";
        String appActivity = "###########";

        // Create an intent to launch the app
        String appIntent = "intent://"
                + "#Intent;"
                + "package=" + appPackage + ";"
                + "action=android.intent.action.MAIN;"
                + "component=" + appPackage + "/" + appActivity + ";"
                + "end";

        driver.executeScript("mobile: deepLink", Collections.singletonMap("url", appIntent));
    }

    private void triggerZapPassiveScan1() {
        try {
            ApiResponse response = zapClient.pscan.recordsToScan();
            String tempVal = ((ApiResponseElement) response).getValue();
            while (!tempVal.equals("0")) {
                System.out.println("Passive scan is in progress");
                response = zapClient.pscan.recordsToScan();
                tempVal = ((ApiResponseElement) response).getValue();
            }
            System.out.println("Passive scan is completed");
        } catch (ClientApiException e) {
            e.printStackTrace();
        }
    }

    public static void generateZapReport(String site_to_test) {
        String title = "Demo Title";
        String template = "traditional-html";
        String theme = null;
        String description = "Demo description";
        String contexts = null;
        String sites = site_to_test;
        String sections = null;
        String includedconfidences = null;
        String includedrisks = null;
        String reportfilename = "Demofilename";
        String reportfilenamepattern = null;
        String reportdir = System.getProperty("user.dir");
        String display = null;
        try {
            ClientApi clientApi = new ClientApi(zapAddress, zapPort, apiKey);
            clientApi.reports.generate(title, template, theme, description, contexts, sites, sections,
                    includedconfidences, includedrisks, reportfilename, reportfilenamepattern, reportdir, display);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterMethod
    public void tearDown() {
        // Add any necessary cleanup code here
        driver.quit();
    }
}

Need to resolve this issue. Please help as early as possible.

0

There are 0 answers