'Winappdriver unit test c# automation getting an "Object reference not set to an instance of an object" error in Test2Password method with "possession"
Here when I try to run these unit tests
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System;
using System.Diagnostics;
namespace test
{
[TestClass]
public class AutomationTest
{
public static WindowsDriver<WindowsElement> possession;
[TestMethod]
public void Test1Login()
{
AppiumOptions ao = new AppiumOptions();
ao.AddAdditionalCapability("app", "Root");
WindowsDriver<WindowsElement> windowssession =
new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), ao);
AppiumOptions windowOptions = null;
var listOfAllWindows =
windowssession.FindElementsByXPath(@"//Window");
Debug.WriteLine($"Elements found: {listOfAllWindows.Count}");
foreach (var window in listOfAllWindows)
{
if (window.Displayed && window.Text.Contains("Aronium - POS"))
{
var windowHandle = window.GetAttribute("NativeWindowHandle");
Console.WriteLine($"Window Handle: {windowHandle}");
var handleInt = (int.Parse(windowHandle)).ToString("x");
windowOptions = new AppiumOptions();
windowOptions.AddAdditionalCapability("appTopLevelWindow", handleInt);
break;
}
}
WindowsDriver<WindowsElement> possession = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"),
windowOptions);
possession.FindElementByAccessibilityId("passwordBox").SendKeys("Future@123");
}
[TestMethod]
public void Test2Password()
{
possession.FindElementByClassName("Button").Click();
}
[ClassCleanup]
public static void Test5Cleanup()
{
if (possession != null)
{
possession.Quit();
}
}
}
}
I am getting an error "Object reference not set to an instance of an object" with the 2nd method (Test2Password) Is there a way for me to declare the possession globally Or can I declare the possession as a variable I am new to C# and Winappdriver
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
