Skip to content

Appium Page Elements are Null - Can't get page object factory work with C#? #173

@siggerzz

Description

@siggerzz

The problem

I've managed to get my Selenium tests running successfully on my webapp, however I'm having problems getting Appium to work on my hybrid mobile app. Currently my page elements are null.

Environment

  • Appium Version - 3.0.0.2
    NUnit Version -3.6.1
    NUnit Test Adapter Version - 3.70
    Selenium Version - 3.30
    Selenium Support Version - 3.3.0

  • Last Appium version that did not exhibit the issue (if applicable): N/A

  • Desktop OS/version used to run Appium: Not Sure, Saucelabs VM

  • Mobile platform/version under test: Android 4.4

  • Real device or emulator/simulator: Emulator

  • Appium CLI or Appium.app|exe: N/A

Details

I'm trying to use Page Object Factory when writing my Appium tests, I've managed to get my Selenium tests working, however when trying to implement POM for Appium, the driver instance gets passed to my page, however the elements are still null? I did have a look at the examples, as well as forums, however I can't quite figure out where I'm going wrong, and a lot of the appium/POM stuff I've seen, is mostly Java related. I'm able to set my page elements by doing driver.findelements in my page factory, so it leads me to believe that it's an issue with the way I'm initialising my page object.

Any help would be greatly appreciated!

Code To Reproduce Issue

BaseTests class. GetDriver method is in the setup of all my tests:

[TestFixture("Android Emulator","portrait","Android","4.4")]
class BKGoBaseTests : DynamicObject
{
    public string _deviceName;
    public string _deviceOrientation;
    public string _platformName;
    public string _platformVersion;

    public AppiumDriver<AppiumWebElement> GetDriver()
    {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.SetCapability("username", "####");
        caps.SetCapability("accessKey", "#####");
        //Appium Capabilities
        caps.SetCapability("appiumVersion", "1.5.3");
        caps.SetCapability("deviceName", _deviceName);
        caps.SetCapability("deviceOrientation", _deviceOrientation);
        caps.SetCapability("browserName", "");
        caps.SetCapability("platformName", _platformName);
        caps.SetCapability("platformVersion", _platformVersion);
        caps.SetCapability("app", "sauce-storage:basekit.apk");



        AndroidDriver<AppiumWebElement> _driver = new AndroidDriver<AppiumWebElement>(
        new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
            caps, TimeSpan.FromSeconds(600));

        return _driver;
    }

Test Class:

[Parallelizable]
class BKGoLoginTests : BKGoBaseTests 
{
    public AppiumDriver<AppiumWebElement> _driver;
    private LoginPageFactory _loginPageFactory;

    public BKGoLoginTests(string deviceName, string deviceOrientation, string platformName, string platformVersion)
    {
        _deviceName = deviceName;
        _deviceOrientation = deviceOrientation;
        _platformName = platformName;
        _platformVersion = platformVersion;
    }


    [SetUp]
    public void LoginSetup()
    {
        _driver = GetDriver();
        _loginPageFactory = new LoginPageFactory(_driver);
    }

    [Test]
    public void BKGoLoginTest()
    {
        _loginPageFactory.LoginAs("ben@bkgoautomation.com", "bentest");
        Assert.AreEqual("Editor", _driver.Title);
    }
}

Page Factory:

 class LoginPageFactory : BasePageFactory
    {
        public LoginPage _loginPage;
        public DashboardPage _dashboardPage;
        public AppiumDriver<AppiumWebElement> _driver;
        public LoginPageFactory(AppiumDriver<AppiumWebElement> driver)
        {
            _driver = driver;
            _loginPage = new LoginPage(_driver);
        }

        public void LoginAs(string email, string password)
        {
            //wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(25));
            var LoginEmail = _loginPage.EmailTxt;
            var LoginPassword = _loginPage.PasswordTxt;
            var LoginBtn = _loginPage.SignInBtn;
            TouchAction ta = new TouchAction(_driver);


            LoginEmail.SendKeys(email);
            LoginPassword.SendKeys(password);



            LoginPassword.SendKeys(password);
            ta.Tap(LoginBtn).Perform();


            ta.Tap(editSiteBtn).Perform();
        }
    }
}
 

Page - Fairly certain the issue is with this class


public LoginPage(AppiumDriver<AppiumWebElement> driver)
    {
        AppiumPageObjectMemberDecorator decorator = new AppiumPageObjectMemberDecorator(new TimeOutDuration(System.TimeSpan.FromSeconds(15)));
        PageFactory.InitElements(driver, this, decorator);
    }

    [FindsByAndroidUIAutomator(AndroidUIAutomator = "new UiSelector().resourceId(\"android:id/username\")")]
    public AppiumWebElement EmailTxt { get; set; }

    [FindsByAndroidUIAutomator(AndroidUIAutomator = "//android.widget.EditText[contains(@resource-id,'password')]")]
    public AppiumWebElement PasswordTxt { get; set; }

EDIT: Came across this issue, which is what I think I'm experiencing.

If i set my Elements to AppiumWebElement I get:

However If I set my elements to IMobileElement, I get:
Object reference not set to an instance of an object

Result Message: System.IO.FileNotFoundException : Could not load file or assembly 'file:///C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\WebDriver.Support.dll' or one of its dependencies. The system cannot find the file specified.
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions