One test script for two platforms - selectors

Hi,
How can I use two different selector in one test? For eg… If I set platform == android in SetUp() then tests will use android selectors, in the other hand will use ios selectors. It is possible to do that? Any sugestions, ideas?

I do that using POM annotations. can check it here: https://github.com/appium/java-client/blob/master/docs/Page-objects.md

I use also java reflection api and maven profiles to trigger different platform specific test each time.

You can ask further questions if you want.

I’m using C# for writing tests.

So, ok … for eg:
@AndroidFindBy(someStrategy)
@iOSFindBy(someStrategy)
WebElement someElement;

And in test I use ‘someElemement’ ?
How to use @AndroidFindBy when i have testing Android app? What I have to set up and how?

you can see examples here, basically Page Object Model(POM) refers to creating classes which represent mobile screens/pages on your app containing inner elements and use them as regular objects.
to initialize those object(because each field has platform specific annotations) you need to use PageFactory.initElements method and AppiumFieldDecorator (at least in Java).
This looks like a nice article(in Java), you can give it a shot:https://blog.testproject.io/2018/07/31/page-object-model-appium-java-android/

And this is .NET specifications regarding POM: https://github.com/appium/dotnet-client/wiki/Page-objects

As it looks, this how to populate the fields for .NET: https://github.com/appium/dotnet-client/wiki/Page-objects#how-to-populate-fields-and-properties

What I did wrong ?
Before I used single selector and test working:
public AppiumWebElement LoginButton => driver.FindElementByXPath("//android.widget.Button[@text='Zaloguj']");

Now I’m using this and not working:
[FindsByIOSUIAutomation(XPath = “ios_locator”)]
[FindsByAndroidUIAutomator(XPath = “//android.widget.Button[@text=‘Zaloguj’]”)]
public AppiumWebElement LoginButton;

The test was run for android and error is: System.NullReferenceException : Object reference not set to an instance of an object.

Did you create a Login class? Did you create an instance of it? Did you initialize it as mentioned here: https://github.com/appium/dotnet-client/wiki/Page-objects#how-to-populate-fields-and-properties

I you will read what I sent you will understand better for sure.

Its a start page with login button. My test only check that button is present.
I use this:
public void InitElements(ISearchContext context, object screenOrPage, AppiumPageObjectMemberDecorator appiumPageObjectMemberDecorator)
{
throw new NotImplementedException();
}

here are some C# examples:


e.g.

        var timeSpan = new TimeOutDuration(new TimeSpan(0, 0, 0, 5, 0));
        _pageObject = new AndroidPageObjectChecksAttributesForNativeAndroidApp();
        PageFactory.InitElements(_driver, _pageObject, new AppiumPageObjectMemberDecorator(timeSpan));

Thanks for recomendations. Its working :D.

1 Like

Its not working between pages. On single page its ok.

Its WelcomePage and I run test on Android, so I dont need ios selector:

*public class WelcomePage : BasePage*
  • {*

  •    [FindsByAndroidUIAutomator(ID = "btStartLogin")]*
    
  •    [FindsByIOSUIAutomation(XPath = "")]*
    
  •    public IMobileElement<AppiumWebElement> LoginButton;*
    

(…)

  •    public WelcomePage(AppiumDriver<AppiumWebElement> driver) : base(driver) {*
    
  •        PageFactory.InitElements(driver, this);*
    
  •    }*
    
  •    public LoginPage GoToLoginPage()*
    
  •    {*
    
  •        LoginButton.Click();*
    
  •        return new LoginPage(driver);*
    
  •    }*
    

(…)

Test :
[Test]

  •    public void TapLoginButton_LoginPageIsPresent()*
    
  •    {*
    
  •        WelcomePage welcomePage = new WelcomePage(driver);*
    
  •        LoginPage loginPage = welcomePage.GoToLoginPage();*
    
  •        Assert.Multiple(() => {*
    

Error: object reference not set … is on welcomePage.GoToLoginPage(). LoginButton is null.

Any idea why this working:
public AppiumWebElement LoginButton => driver.FindElementById(“btStartLogin”);

but this not :
[FindsByAndroidUIAutomator(ID = “btStartLogin”)]
[FindsByIOSUIAutomation(XPath = “”)]
public AppiumWebElement LoginButton;