WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Azure pipeline giving error An element could not be located at line var sessionb = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptions);

Open omkar4289 opened this issue 11 months ago • 8 comments

I am getting error: ClassInitialize threw exception. OpenQA.Selenium.WebDriverException: OpenQA.Selenium.WebDriverException: An element could not be located on the page using the given search parameters.. 2025-02-13T07:48:27.0449820Z Stack Trace: 2025-02-13T07:48:27.0450277Z at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) 2025-02-13T07:48:27.0450578Z at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) 2025-02-13T07:48:27.0451173Z at OpenQA.Selenium.Appium.AppiumDriver1.Execute(String driverCommandToExecute, Dictionary2 parameters) 2025-02-13T07:48:27.0452283Z at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) 2025-02-13T07:48:27.0452973Z at OpenQA.Selenium.Appium.AppiumDriver1.FindElementByName(String name)

on line: var sessionb = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptions);

in my local machine it's working fine but I am getting this error when I run it through azure pipeline.

It is a desktop application and I have used winappdriver to automate it. Below tasks I have used for release pipeline but not sure whether it is opening the application or not

Image

omkar4289 avatar Feb 13 '25 08:02 omkar4289

@omkar4289 From your stack, looks like the Azure environment is slower than locally. Put a delay until the element with that name exists.
Check exists by a loop using FindElements instead of FindElement.

liljohnak avatar Feb 13 '25 11:02 liljohnak

Hi @liljohnak , Thank you for your response.

Now I am getting below error: System.NullReferenceException: Object reference not set to an instance of an object.

but same works fine when I run the code locally in my machine

omkar4289 avatar Feb 14 '25 08:02 omkar4289

omkar4289 Please provide code example, try to use waiters

Shakevg avatar Feb 14 '25 10:02 Shakevg

@liljohnak @Shakevg PFB: public class SessionInit { protected static WindowsDriver<WindowsElement> sessionb; protected static WindowsDriver<WindowsElement> session;

    public static void StartSession(TestContext context)
    {
            string username = Environment.UserName;
            var appiumOptions = new OpenQA.Selenium.Appium.AppiumOptions();
            appiumOptions.AddAdditionalCapability("platformName", "Windows");
            appiumOptions.AddAdditionalCapability("app", "Root");
            appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");

            
             Thread.Sleep(10000);
            var sessionb = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appiumOptions);
           

            // Attaching to existing Application Window
            
            var applicationwindow = sessionb.FindElementsByName("Hermes 5 (DEV) - HERMES_5_1_66_8.ECF 07/02/2025 19:07 (LIS_W1_66,PT_SHARE_66,ADMIN,ADMIN)");
            
             
             foreach (WindowsElement element in applicationwindow)
       {
             if (element.Text.Contains("Hermes 5 (DEV)"))
           {
               var topLevelWindowHandle = element.GetAttribute("NativeWindowHandle");
               topLevelWindowHandle = int.Parse(topLevelWindowHandle).ToString("X");

                var appopt = new OpenQA.Selenium.Appium.AppiumOptions();
                appopt.AddAdditionalCapability("appTopLevelWindow", topLevelWindowHandle);
               session = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appopt);
               
            }
        }
        
            try { session.Manage().Window.Maximize(); } catch { }

}

    public static void EndSession()
    {
        // Close the application and delete the session
        if (session != null)
        {
            session.Close();
            session.Quit();
            session = null;
        }
    }
}

and I have test methods in below class [TestClass] public class OP_Exp_NOT_Out4 : SessionInit { [TestMethod] public void Test() { string Scriptid; Scriptid = "Exp_NTM_Out4"; //////create 1st Export flight Thread.Sleep(1000); session.FindElementByAccessibilityId("MDIClientForm").Click(); // to click in background session.FindElementByAccessibilityId("2").FindElementByAccessibilityId("6").Click(); Fun_exp_createnewflight.execute(Scriptid); //Create new Exp flight Thread.Sleep(2000); }

 [ClassInitialize]
 public static void ClassInitialize(TestContext context)
 {
     StartSession(context);
 }

}

I am getting error at line: session.FindElementByAccessibilityId("MDIClientForm").Click(); // to click in background System.NullReferenceException: Object reference not set to an instance of an object. may be because of session

omkar4289 avatar Feb 14 '25 12:02 omkar4289

@omkar4289 for this one, all of the .FindElement() have an additional condition of being focusable and clickable. To relax these conditions use one of the .FindElements() methods and get the BoundingBox attribute. Then you can click the center of the bounding box.

Another possibility is you need a delay for the button to become visible.

liljohnak avatar Feb 14 '25 14:02 liljohnak

@liljohnak still its giving the same error. System.NullReferenceException: Object reference not set to an instance of an object.

omkar4289 avatar Feb 17 '25 05:02 omkar4289

Maybe a 500 error on WinAppDriver.exe?

liljohnak avatar Feb 17 '25 05:02 liljohnak

Please check if winappdriver task has started successfully. Is it running with self hosted agent or Microsoft hosted agent? Also share the pipeline logs? I see that you are using powershell to run winappdriver.exe. Please introduce a sleep for 5 second.

anunay1 avatar Mar 04 '25 07:03 anunay1