Creating own Customised MobileElement by extending from MobileElement

430 views Asked by At

I am trying to have own customized MobileElement class that i can add more methods. For example I have a class named SamplePage and it contains below mobile element:

  @iOSFindBy(accessibility = "Settings")
  @AndroidFindBy(id = "Settings")
  public MobileElement SettingsButton;

I use it in test case lets say as below:

  samplePage.SettingsButton.click();

What I want to have is as below

   @iOSFindBy(accessibility = "Settings")
   @AndroidFindBy(id = "Settings")
   public customisedMobileElement SettingsButton;

A test case with IsVisible() method or CopyText() method I have in customisedMobileElement class:

   Assert.isTrue(samplePage.SettingsButton.IsVisible(), "not visible");
   samplePage.LoginTextInput.CopyText();

Could you please share your ideas about how to do it?

1

There are 1 answers

1
Suban Dhyako On

First define the page as follow:

public class SettingPage{       

    @AndroidFindBy(accessibility = "Settings")
    @iOSFindBy(accessibility = "Settings")
    private MobileElement setting;

    public SettingPage(AppiumDriver<MobileElement> driver) {
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    public boolean isScreenDisplayed(){
        try {
            return setting.isDisplayed();
        }catch (Exception e){
            return false;
        }
    }

    public void click(){
         setting.click();
    }
}

Then you can use this as following:

public class Test(){
  AppiumDriver<MobileElement> driver;

  //define your desiredCapabilities and appium driver

  private SettingPage settingPage;

  public void displayTest(
   settingPage= new SettingPage(driver);
   settingPage.isScreenDisplayed();
  }

  public void clickTest(
   settingPage= new SettingPage(driver);
   settingPage.click();
  }

}