0

I need to close the app and relaunch it at the same position where i closed it, for this am using closeApp and launchApp methods. But when i launch the app, it is launching the app from beginning. I tried with noReset and fullReset options. Am using appium 1.2.7 and ios 11.2 with iPhone 7 simulator.In android am able to achieve this using noReset, is this possible in ios.

Can any one suggest?

Thanks in advance.

Kalamarico
  • 5,466
  • 22
  • 53
  • 70
Stephen
  • 93
  • 1
  • 1
  • 5

1 Answers1

0

The only way to do what you want is saving your session_id, and when you launch a new test you start at the end point of the test executed before...

What you have to do is to Override Appium constructor and start_session:

Python class code:

class MyWebDriverAppium(webdriver.Remote):    

def __init__(self, context, command_executor='http://127.0.0.1:4444/wd/hub',
             desired_capabilities=None, session_id=None):
    self.cCore = context
    try:
        self.preserved_session_id = session_id          
        self.error_handler = MobileErrorHandler()
        self._switch_to = MobileSwitchTo(self)

        # add new method to the `find_by_*` pantheon
        By.IOS_UIAUTOMATION = MobileBy.IOS_UIAUTOMATION
        By.IOS_PREDICATE = MobileBy.IOS_PREDICATE
        By.ANDROID_UIAUTOMATOR = MobileBy.ANDROID_UIAUTOMATOR
        By.ACCESSIBILITY_ID = MobileBy.ACCESSIBILITY_ID
        super(MyWebDriverAppium, self).__init__(command_executor, desired_capabilities)
    except Exception:
        print(traceback.format_exc())
        raise MyWebDriverAppiumInit("Error initializing Appium driver")

# If preserved_session is None we will start a new session as normal
# If preserved session is not None we will use that session to execute
def start_session(self, desired_capabilities, browser_profile=None):        
    try:            
        if self.preserved_session_id:            
            self.command_executor._commands['getSession'] = ('GET', '/session/$sessionId')
            self.session_id = self.preserved_session_id
            response = self.execute('getSession', {'sessionId ': self.session_id})            
            self.session_id = response['sessionId']
            self.capabilities = response['value']
            self.w3c = response['status']
        else:
            super(MyWebDriverAppium, self).start_session(desired_capabilities, browser_profile)
    except Exception:
        raise MyWebDriverAppiumStart("Error starting Appium driver after initialization")

Python implementation code:

self.driver = MyWebDriverAppium(self, self.driver_context, self.desired_caps, session_id)

Java class code:

public class MyWebDriverAppium extends RemoteWebDriver {

    private String preserved_session_id = null;

    public MyWebDriverAppium(URL remoteAddress, Capabilities desiredCapabilities, String session_id) {
        super(remoteAddress, desiredCapabilities);
        this.preserved_session_id = session_id;
    }

    @Override
    protected void startSession(Capabilities desiredCapabilities) {
        if(this.preserved_session_id != null){
            setSessionId(this.preserved_session_id);
        }
        super.startSession(desiredCapabilities);
    }
}

Java implementation code:

driver = new MyWebDriverAppium<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities, session_id);

I know python code works perfectly because is the one I use everytime in my tests. I am not 100% sure Java code will work perfectly because I made it without an IDE.

Hope it helps

barbudito
  • 548
  • 1
  • 6
  • 16