How to run one script against both platforms android/IOS

im able to run test against each platform separately, but wanted to utilize the script so i can write one script in page object model that can be executed from both platform android/ios
whatever what device is connected appium will recognize it and execute the relative driver instance

how i will set desired cap to work for both platforms

how i will set page element factory

how will write the element for both platforms
or if anyone has a ready code sample

here is sample of my code

public class BaseSetup {

public static AppiumDriver driver = null;
private Logger logger;

@BeforeSuite(alwaysRun = true)
public void setUp () throws MalformedURLException {
    System.out.println("Driver Is Initiated");
    DesiredCapabilities dc = new DesiredCapabilities();

    //Mobile Config Setup
    dc.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
    dc.setCapability("platformName","android");
    dc.setCapability("platformVersion","10");
    dc.setCapability("avd","Pixel");
    dc.setCapability(MobileCapabilityType.APPLICATION_NAME,"uiautomator2");

// dc.setCapability(MobileCapabilityType.AUTO_WEBVIEW,true);

    //Application Config Setup
    dc.setCapability(MobileCapabilityType.APP,
            new File("src/main/resources/Safe.apk").getAbsolutePath());
    dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.test..mobile");
    dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.test.mobile.MainActivity");
    dc.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS,"true");
    dc.setCapability(MobileCapabilityType.CLEAR_SYSTEM_FILES,true);

    driver = new AppiumDriver<MobileElement> (new URL("http://localhost:4723/wd/hub"),dc);

} 

public class AndroidDecorator extends BaseSetup {

protected WebDriverWait wait;
public AndroidDecorator(AppiumDriver<MobileElement> driver) {
    this.driver = driver;
    wait = new WebDriverWait(driver, 15);
    driver.context("NATIVE_APP");
    PageFactory.initElements(new AppiumFieldDecorator(driver),this);
}

}

public class LoginPage extends AndroidDecorator {

public LoginPage() {
    super(driver);
}

@BeforeMethod
private void context(String native_app) {
    driver.context("NATIVE_APP");
}


@AndroidFindBy(xpath = "//android.view.View[@text='Welcome to SAFE!']")
private MobileElement welcomeHeader;

   public void welcome_header_is_visible() {
    wait.until(ExpectedConditions.visibilityOf(welcomeHeader));
}

public class LoginTest extends BaseSetup {


@Test(priority = 1)
public void Verify_login_page_is_loaded_properly () {
    LoginPage page = new LoginPage();
    page.welcome_header_is_visible();
}

@Aleksei
do you have any code sample maybe

it is not clear what problem you have. e.g. Page element working for both:

    @AndroidFindBys(value = {
            @AndroidBy(id = "tv_first_name"),
            @AndroidBy(className = "android.widget.EditText")
    })
    @iOSXCUITFindBys(value = {
            @iOSXCUITBy(id = "firstName"),
            @iOSXCUITBy(className = "XCUIElementTypeTextField")
    })
    private MobileElement firstNameInput;

what about the desired capability how i will make appium select the relative driver based on the connected device (android or IOS)

Well e.g. i use testNG and there i use parameters for setting phones to test. In code i am getting them and open either iOSdriver or AndroidDriver.

so you are passing the parameters to your capabilities, using two testNg xml files

Yes. Also this parameter can be sent via command line like -DparameterName=value

awesome thanks i will try this.