mardi 1 octobre 2019

"Object reference not set to an instance of an object." on Page Object Model on every Android element

these are my first steps in mobile automation, I need some help with element initialization on SamplePage.cs, below there are my three classes:

TestBase.cs

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
using System;
using AppiumDotNetSamples.Helper;
using AppiumDotNetSamples.Page;

namespace AppiumDotNetSamples
{
    public class TestBase 
    {
        protected AndroidDriver<AndroidElement> _driver;

        [SetUp()]
        public void BeforeAll()
        {
            DesiredCapabilities capabilities = new DesiredCapabilities();
            //capabilities.SetCapability(MobileCapabilityType.BrowserName, "");
            capabilities.SetCapability(MobileCapabilityType.PlatformName, "Android");
            capabilities.SetCapability(MobileCapabilityType.PlatformVersion, "7.1.2");
            capabilities.SetCapability(MobileCapabilityType.AutomationName, "UIAutomator2");
            capabilities.SetCapability(MobileCapabilityType.DeviceName, "3e52f2ee7d34");
            capabilities.SetCapability("appPackage", "com.google.android.calculator");
            capabilities.SetCapability("appActivity", "com.android.calculator2.Calculator");
            capabilities.SetCapability(MobileCapabilityType.App, App.AndroidApp());

            _driver = new AndroidDriver<AndroidElement>(Env.ServerUri(), capabilities, Env.INIT_TIMEOUT_SEC);
            _driver.Manage().Timeouts().ImplicitWait = Env.IMPLICIT_TIMEOUT_SEC;

        }

        [TearDown()]
        public void AfterAll()
        {
            _driver.Quit();
        }
    }
}

AndroidBasicInteractionsTest.cs

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
using System;
using AppiumDotNetSamples.Helper;
using AppiumDotNetSamples.Page;

namespace AppiumDotNetSamples
{
    [TestFixture()]
    public class AndroidBasicInteractionsTest:TestBase
    {
        private SamplePage _samplePage;
        [SetUp()]
        public void SetUpForLogInTest()
        {
            _samplePage = new SamplePage(_driver);
           /* if (!_samplePage.isDisplayed())
            {
                return;
            }*/


        }
        [Test()]
        public void ClickOnButtonOne()
        {
            // AndroidElement buttonOne = _driver.FindElementById("com.google.android.calculator:id/digit_1");
            // buttonOne.Click();

            _samplePage.ClickOnButtonOne(); 
        }
    }
}

SamplePage.cs

using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Support.PageObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Appium.Interfaces;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.PageObjects.Attributes;
using OpenQA.Selenium.Support.UI;
using System.Threading;

namespace AppiumDotNetSamples.Page
{
    public class SamplePage
    {
        private AndroidDriver<AndroidElement> _driver;

        [FindsByAndroidUIAutomator(ID = "com.google.android.calculator:id/digit_1")]
        public AndroidElement _buttonOne;

        public SamplePage(AndroidDriver<AndroidElement> driver)
        {
            _driver = driver;
            PageFactory.InitElements(_driver, this);
        }

        public void ClickOnButtonOne()
        {
            //AndroidElement _buttonOne = _driver.FindElementById("com.google.android.calculator:id/digit_1");
            _buttonOne.Click();
        }

       /* public Boolean isDisplayed()
        {
            return _buttonOne.Displayed;
        }*/
    }
}

I've got Message: System.NullReferenceException : Object reference not set to an instance of an object.

error and during debbuging I see https://i.postimg.cc/SQDdXVpW/2019-10-02-08-08-41-Window.png

when I comment

[FindsByAndroidUIAutomator(ID = "com.google.android.calculator:id/digit_1")] public AndroidElement _buttonOne;

and then remove comment from AndroidElement _buttonOne = _driver.FindElementById("com.google.android.calculator:id/digit_1"); everything works, and during debbuging I see https://i.postimg.cc/Dy0g1s2R/2019-10-02-08-09-43-Window.png

I tried with uncommented method

{
return _buttonOne.Displayed; 
}

from SamplePage.cs

and condition

if (!_samplePage.isDisplayed()) 
{
return;
}

from AndroidBasicInteractionsTest:TestBase.cs but it doesn't help. What am I doing wrong? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire