Page source is different while running test suite and a single test

I am automating an Ionic framework app, I have a feature where I click on a button and a modal shows up with button of text “Save”, but clicking on it clicks a button which is behind the modal which is also a button with text “Save”.

I am using XPath "//*[contains(@text,‘Save’)] since we don’t have accessibility ids and single codebase for both iOS and Android.

I am forced to use NATIVE context although entire app is inspectable with chrome in the debug version of same app(like a WEBVIEW) because of client requirement to run the tests in the release builds.

When running the test separately, the page_source(as I checked with driver.page_source) has only one “Save” button and clicks the modal one perfectly fine

When running the test file with multiple tests, the page_source has two Save buttons and appium clicks on unintended Save button

How to deal with the issue? I am clueless, if I use instance [2], it will fail when running single test case, if I don’t use instance, it will not work when running as suite.

@Aleksei @Telmo_Cardoso
Can you please help here for any possible workaround?

Pls share page source with two buttons

I managed to get it working by finding the required button as
//[contains(@text,‘Save’) and contains(@class,‘Button’) and ancestor::[contains(@class, ‘Dialog’)]]

Thanks for the reply, but I still couldn’t figure out why page source has the elements hidden behind the android.app.Dialog when entire test suite is run, but when run as single test, the page source has only the elements in the android.app.Dialog.

<android.widget.Button index=“0” package=“dummy_package” class=“android.widget.Button” text=“Save” checkable=“false” checked=“false” clickable=“true” enabled=“true” focusable=“true” focused=“false” long-clickable=“false” password=“false” scrollable=“false” selected=“false” bounds=“[553,2289][1057,2339]” displayed=“true” hint=“” />

Here is the button’s XML, unfortunately, I can’t share complete page source due to NDA.