Appium test runs twice on same exact test

Hi all,

New to appium. I successfully got it running on my Windows environment for our Android device.

I initial run a small test using Google Play store and making it open without any issues.

My problem is, my first test on this Android hardware doesnt seem to very reliable.

The problem, it opens our custom app fine without any issues but, closes the app on the home screen and then open the same app again. Bearing in mind, this is running on a custom hardware and not on a phone.

Any ideas on whats causing it? I can try to give as much information as a I can to help diagnose the problem.

You should share your code so people can help you.

Thanks wil do, please find enclosed my code:

package appiumtests;  

import java.net.MalformedURLException; 
import java.net.URL; 
import java.time.Duration; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait;  
import io.appium.java_client.AppiumDriver; 
import io.appium.java_client.android.AndroidDriver; 

public class FirstTest { 

	static AppiumDriver driver; 

	public static void main(String[] args) { 
		 

		try {
                  openTest(); 
                } catch (Exception e) { 
                   e.printStackTrace(); 
               } 
} 

	public static void openTest() throws Exception { 

		 

		DesiredCapabilities cap = new DesiredCapabilities(); 

		cap.setCapability("deviceName", "Device name"); 
		cap.setCapability("udid", "#####"); 
		cap.setCapability("platformName", "Android"); 
		cap.setCapability("platformVersion", "9"); 
		cap.setCapability("appPackage", "com.custom.package"); 
		cap.setCapability("appActivity", "com.MainActivity"); 

		URL url = new URL("http://127.0.0.1:4723/wd/hub"); 

		driver = new AppiumDriver(url,cap); 

		System.out.println("Application Start..."); 

WebElement three =driver.findElement(By.xpath("//android.widget.Button[@text='Some text']")); 

			} 
}

So I would caution you against opening the driver inside each test. You may want to organize your test into a setup, testing, and teardown phase, more like this example code:

There is a tutorial that you may find useful on the above code here:

1 Like

Thanks for the helpful resonse @wreed.

I managed to get most of the code and edit to my needs. My issue for some reason is, I cant get one of the lines on that code to work which is this one:

driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), caps);

the error red lines that show up near is:
Multiple markers at this line
- The type AndroidDriver is not generic; it cannot be parameterized with arguments
- MobileElement cannot be resolved to a type
- The constructor URL(String) is deprecated
- Line breakpoint:Revamp_First_Test [line: 38] - setup()

the import section for it also shows an error:

The import io.appium.java_client.MobileElement cannot be resolved

My pom.xml seems ok but here it is for reference:

4.0.0 appiumtests appiumtests 0.0.1-SNAPSHOT Sample Appium description
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
org.seleniumhq.selenium selenium-java 4.13.0
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.8.0</version>
</dependency>
io.appium java-client 8.5.1

Any help or directions would be greatly apperciated.

Use now WebElement instead

Sorry if the example is a little old. In the example, it looks a little different:

public AndroidDriver<MobileElement> driver;
driver = new AndroidDriver<MobileElement>(new URL(“http://127.0.0.1:4723/wd/hub”), caps);

Note: The < symbol gets hidden on this messageboard, so I’m pretty sure your code has <MobileElement>. You have to escape it (‘\<’) to see properly here.

I believe @Aleksei is correct that now WebElement is used instead. I also went to the Java Client github page to see what they are doing there:

AndroidDriver driver = new AndroidDriver(
    // The default URL in Appium 1 is http://127.0.0.1:4723/wd/hub
    new URL("http://127.0.0.1:4723"), options
); 

Hopefully between those resources you can sort out the issues. If not, come back & we’ll have a look again.

Thanks all for the response. I think I need some more help.

When I run my code, it doesnt show any error message but it just says its terminated in the console and shows no errors. This what my code looks like so far:

package appiumTest;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.Duration;

public class FirstTest {

  public AndroidDriver driver;
  public WebDriverWait wait;

  //Elements By
  By firstID = By.id("//android.widget.Button[@text='SomeText']");

  @BeforeMethod
  public void setup() throws MalformedURLException {
    DesiredCapabilities caps = new DesiredCapabilities();
    
    caps.setCapability("deviceName", "DeviceName");
	caps.setCapability("udid", "IDgoeshere");
	caps.setCapability("platformName", "Android");
	caps.setCapability("platformVersion", "9");
	caps.setCapability("appPackage", "com.test");
	caps.setCapability("appActivity", "com.test.MainActivity");

	UiAutomator2Options options = new UiAutomator2Options()
		    .setUdid("UDIDGoes heres")
		    .setApp("/home/myapp.apk");
	
	AndroidDriver driver = new AndroidDriver(
		    
		    new URL("http://127.0.0.1:4723"), options
		);
	
	WebElement three =driver.findElement(By.xpath("//android.widget.Button[@text='Some text']"));	    
	WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

}

  @Test
  public void basicTest() throws InterruptedException {
     

        //Click on the element
        wait.until(ExpectedConditions.visibilityOfElementLocated(firstID)).click();

       

    }
@AfterMethod
  public void teardown() {
    driver.quit();
  }
}

Any help or direction would be great.

What is the code not doing that you expect it to? No error messages is good, but you seem to think there should be an error message. Why?

After this test I would expect the console to say terminated, because the @AfterMethod does a driver.quit(). Do you not want it to terminate? If you want to run multiple tests you could do @AfterSuite. See here for testng annotations:

Hi @wreed thanks again for your response.

I have my Android device that Iam trying to run my test through intellij. I was hoping it would automate on my device, which i didnt. Thats why I was thinking it failed.

Reading through your test it looks to me like this means that the automation did not click the button. Please correct me if I’m wrong. Can you post the log? Would prefer it as a Git Gist, but just post it here if you aren’t sure how to do that.

Hi wreed, really sorry for the delayed response.
My work enviroment has since changed but, Iam still having the same issue.
Having another look at what happens, it seems like it refreshes the home screen 3-4 times before attempting to try and click on something which it doesnt.
However, in regards to the code, Iam using the recorder function built in Appium inspector that generates the code for me. And I can then run the test using my command line tool and just navigate to the file where its located.

I experimented with the different language options I had to choose, including Python, Java etc which all face the same problem.

The log I got out of robot framework was:
IndexError: list index out of range

And the screenshot it captured was a blank page which I suspect it needs to refresh the page to load the object. Is there a way to do that?

I guess my question is:

  • Is there a way to stop Appium refreshing the home page multiple time on the device?
  • Do we have a command to refresh the device so it can update the blank page?

This is what the code looks like now:

driver = webdriver.Remote(“http://127.0.0.1:4723/wd/hub”, options=options)

actions = ActionChains(driver)
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, “touch”))
actions.w3c_actions.pointer_action.move_to_location(965, 503)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(0.1)
actions.w3c_actions.pointer_action.release()
actions.perform()

actions = ActionChains(driver)
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, “touch”))
actions.w3c_actions.pointer_action.move_to_location(221, 462)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(0.1)
actions.w3c_actions.pointer_action.release()
actions.perform()

driver.quit()

So this is what a log would tell us. It’s impossible for me to tell what’s going on without it.