dotnet-client icon indicating copy to clipboard operation
dotnet-client copied to clipboard

Explicit Wait not working in Appium 1.8.0 with c# client

Open Gocoollnath opened this issue 6 years ago • 12 comments

Description

Explicit wait not working and the sever doesn't wait for the time specified for the object to be present

Environment

Appium version 1.8.0 Desktop OS/version used to run Appium: Windows 10 Pro Node.js version (unless using Appium.app|exe): 8.10.0 Mobile platform/version under test: Android API 25 ,v7.1.1 Real device or emulator/simulator: Real Device Samsung Tab Appium CLI or Appium.app|exe: both IDE: VisualStudio 2017 Enterprise and C# as coding language

Code To Reproduce Issue [ Good To Have ]

new WebDriverWait(driver, TimeSpan.FromMilliseconds(50000)).Until(ExpectedConditions.ElementToBeClickable(By.ClassName("android.widget.Spinner")));

and also tried this code

new WebDriverWait(driver,FromMilliseconds(50000)).Until(X=>X.FindElement(By.Id("elementid");

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(50)); Func<IWebDriver, IWebElement> waitForElement = new Func<IWebDriver, IWebElement>((IWebDriver Web) => {

             IWebElement element = Web.FindElement(By.XPath("//android.widget.Button[@text='OK']"));
             if (element.GetAttribute("style").Contains("red"))
             {
                 return element;
             }
             return null;
         });

IWebElement ok =wait.Until(waitForElement);

Ecxeption stacktraces

Test Name: TestMethod1 Test FullName: UnitTestProject1.UnitTest1.TestMethod1 Test Source: C:\Users\gokulnath.kumar\source\repos\UnitTestProject1\UnitTestProject1\UnitTest1.cs : line 25 Test Outcome: Failed Test Duration: 0:01:49.3963396

Result StackTrace:

at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Appium.AppiumDriver1.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath) at OpenQA.Selenium.By.<>c__DisplayClass19_0.<XPath>b__0(ISearchContext context) at OpenQA.Selenium.By.FindElement(ISearchContext context) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by) at OpenQA.Selenium.Appium.AppiumDriver1.FindElement(By by) at AMCS_Mobile.Page_Objects.Vehicle_Selector_Objects.OK_btn() in C:\Users\gokulnath.kumar\source\repos\UnitTestProject1\UnitTestProject1\Page_Objects\Vehicle_Selector_Objects.cs:line 36 at AMCS_Mobile.Vehicle_Selector.Vehicle_selector.Vehicle_Selector(String Vehiclename) in C:\Users\gokulnath.kumar\source\repos\UnitTestProject1\UnitTestProject1\App_Modules\Vehicle_selector.cs:line 30 at UnitTestProject1.UnitTest1.TestMethod1() in C:\Users\gokulnath.kumar\source\repos\UnitTestProject1\UnitTestProject1\UnitTest1.cs:line 29 Result Message: Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: An element could not be located on the page using the given search parameters.

Link to Appium logs

https://gist.github.com/Gocoollnath/547096033a7ff6a9bc4c89b9f30f7d1b

Gocoollnath avatar May 10 '18 12:05 Gocoollnath

I am running into the same issue. When I use WebDriverWait it does not appear to actually wait for the time specified.

wjvii avatar Aug 18 '18 14:08 wjvii

I also seem to be running into this issue :-/

jankratochvilcz avatar Aug 20 '18 19:08 jankratochvilcz

I'm having the same issue. Appium CLI: 1.8.1 Appium app: 1.6.3

vsushyk avatar Aug 21 '18 22:08 vsushyk

Any workaround?

tomerpeled avatar Sep 19 '18 12:09 tomerpeled

Same issue using Appium.WebDriver 3.0.0.2. WebDriverWait does not wait for the time specified.

ArekGr avatar Oct 29 '18 14:10 ArekGr

Having the same issue with Selenium.WebDriver 3.141.0. Any plan to fix this?

spogulis avatar May 10 '19 11:05 spogulis

Using new WebDriverWait(Driver, timeout).Until(func); is working fine for us. Are you aware that the passed function needs to return a boolean?

Maybe you can try one of these

new WebDriverWait(driver, FromMilliseconds(50000)).Until(drv => drv.FindElement(By.Id("elementid")).Displayed); or new WebDriverWait(driver, FromMilliseconds(50000)).Until(drv => drv.FindElements(By.Id("elementid")).Any());

I have to say that I also did not have much success when using ExpectedConditions (so I can give no guidance here).

lanzelot1989 avatar Jun 19 '19 05:06 lanzelot1989

This is not working for me either. Tried various WinAppDriver versions. Works fine with the webdrivers.

alexp1980 avatar Nov 19 '19 12:11 alexp1980

I am having this issue with Selenium IDE (we still haven't moved forward with another automation tool) All my "Wait for element present" commands just spin. But if I pause my test and un-pause, it then goes forward.

smith-test avatar Jun 10 '20 14:06 smith-test

Facing the same issue using Appium.WebDriver 4.2.1. I am using DefaultWait and it does not wait for the time specified. I set a wait time of 5 seconds. But it is taking more than that(sometimes even 20+ seconds) Any update on this ?

nramprakash88 avatar Feb 24 '21 04:02 nramprakash88

I did a bit research of this issue, but not totally sure about the second point

  • Quite obvious that the ImplicitWait time has to be smaller than the one you passed to WebDriverWait. So you need to set it to a small amount and set back to the original value after the WebDriverWait.
  • The WebDriverWait is expecting a NotFoundException when element not found. But seems appnium (maybe not from appnium) is throwing WebDriverException instead.

I guess below would workaround the issue

WebDriverWait GetWebDriverWait(IWebDriver driver, TimeSpan waitTime)
{
   var wait = new WebDriverWait(driver, waitTime);
   wait.IgnoreExceptionTypes(typeof(NotFoundException),
        typeof(InvalidOperationException), // i guess some version may throw this exception instead.
        typeof(WebDriverException));
   return wait;
}

UseTheWebDriverWait() {
  using(Disposable(onInit: SetImplicitWaitTimeToSmallerAmount, onDispose: SetImplicitWaitTimeBack)){
     var wait = GetWebDriverWait(theDriver, theTimeToWait);
     wait.Until(......);
  }
}

EbenZhang avatar Apr 20 '21 04:04 EbenZhang

@EbenZhang Adding IgnoreExceptionTypes worked for me but when this is going to be fixed?

giorgitedi avatar Oct 07 '22 13:10 giorgitedi

@Gocoollnath Can you please try with appium 2.0? Appium 1.0 is out of support

Dor-bl avatar Nov 04 '22 17:11 Dor-bl

Closing this issue since it's the expected behavior. As @EbenZhang mentioned, you should use: wait.IgnoreExceptionTypes(typeof(NotFoundException) . In addition, you should wrap your code with try-catch, in case element is not found in the given time frame.

Dor-bl avatar Nov 13 '22 19:11 Dor-bl