Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep alive Appium session for long time

I need to wait in the middle of my test for 5 minutes but the Appium session has default newCommandTimeout of 60s. and I get exception in the next command that my session is timeout.

AndroidDriver appiumDriver = new AndroidDriver(new URL(getMcmUrl()), capabilities);
Thread.sleep(5*60*1000); // 5 minutes sleep time
appiumDriver.executeScript("...")
like image 340
Elhay Avichzer Avatar asked Jan 22 '19 12:01

Elhay Avichzer


People also ask

How do I keep Appium session alive?

in case that the timeout is 60s, you need to execute any command at least once in a minute, to keep the session alive. Show activity on this post. In your DesiredCapabilities add newCommandTimeout capabilities. DesiredCapabilities caps=new DesiredCapabilities(); //other desired caps //add the following line caps.

How do I increase timeout in Appium?

The default suggested timeout for a test run on Bitbar Testing is 10 minutes, which can be adjusted according to your personal needs up to 60 minutes. To adjust this timeout with the client-side Appium, you'll need to familiarize yourself with a new capability and use it in your scrips – testdroid_testTimeout.

How do you sleep in Appium?

sleep() is a static wait, it is not recommended in the appium scripting you can use Implicit wait, It can be applied to all steps where you are locating the elements. It can be used immediately after initializing the driver object. Below is the code syntax.

What is implicit wait in Appium?

Implicit wait is a way to tell the Appium driver to poll the DOM (Document Object Model) for a certain amount of time before throwing an exception to the effect that it can't find the element on the page. The default timeout value is set to 0 seconds.


2 Answers

newCommandTimeout:

How long (in seconds) Appium will wait for a new command from the client before assuming the client quit and ending the session

in case that the timeout is 60s, you need to execute any command at least once in a minute, to keep the session alive.

For example, this is how sleep for 5 minutes should look like

for (int i = 0; i < 5; i++) {
    driver.getOrientation(); // execute some command to keep the session alive
    Thread.sleep(59*1000); // wake up before session expired
}

Read this article for more information
https://l18.me/how-to-keep-alive-appium-driver-da9227b2fa

like image 111
Elhay Avichzer Avatar answered Jan 01 '23 07:01

Elhay Avichzer


In your DesiredCapabilities add newCommandTimeout capabilities.

DesiredCapabilities caps=new DesiredCapabilities();
//other desired caps
//add the following line
caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
//then define your driver here
AppiumDriver<MobileElement> driver= new AndroidDriver(new URL(getMcmUrl()), caps);    

newCommandTimeout means How long (in seconds) Appium will wait for a new command from the client before assuming the client quit and ending the session.

300 sec = 5 minutes

like image 24
Suban Dhyako Avatar answered Jan 01 '23 07:01

Suban Dhyako