Selenium get value of current implicit wait

前端 未结 5 1789
悲&欢浪女
悲&欢浪女 2021-01-17 15:50

I realize that Selenium has a default value for implicit waits, but how do I get this value if I change it? For example:

<         


        
相关标签:
5条回答
  • 2021-01-17 15:53

    Unfortunately there's no getter for that.

    http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html

    There isn't for explicit waits either.

    http://selenium.googlecode.com/svn/trunk/docs/api/java/com/thoughtworks/selenium/Wait.html

    0 讨论(0)
  • 2021-01-17 16:02

    I know I'm a couple years late, and @JaneGoodall is not wrong -- there is no built-in function for that. But it's not impossible!

    It's not very difficult to create your own versions of the WebDriver interface and browser-specific driver class. And then, you can put whatever code you want into the driver!

    Example:

    MyDriver.java (specialized version of WebDriver, not quite mandatory but a very good idea):

    public interface MyDriver extends WebDriver {
        void setWait(int timeout);
        int getWait();
    }
    

    MyChrome.java (specialized version of ChromeDriver -- works the same for any browser)

    public class MyChrome extends ChromeDriver implements MyDriver {
        int timeout = 0;
        public void setWait(int timeout) {
            this.timeout = timeout;
            this.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
        }
        public int getWait() {
            return timeout;
        }
    }
    

    And now, to use it, MyProgram.java:

    public class MyProgram {
        MyDriver driver = new MyChrome();
        driver.setWait(10);
        assert(driver.getWait() == 10);
    }
    

    I hope this is helpful!

    0 讨论(0)
  • 2021-01-17 16:09

    For those who came here from google. In 2018 it seems like there is a method to get those timeouts at least in javascript (I know question was about java):

    const {implicit, pageLoad, script} = await driver.manage().getTimeouts();
    

    Hope this will help.

    0 讨论(0)
  • 2021-01-17 16:10

    This can print real timeout value (plus calculating time, usually within 100ms):

    public void getCurrentWaitTimeout() {
        long milliseconds = java.time.ZonedDateTime.now().toInstant().toEpochMilli();
        driver.findElements(By.cssSelector(".nonExistingElement"));
        milliseconds = java.time.ZonedDateTime.now().toInstant().toEpochMilli() - milliseconds;
        log.info("Current waiting timeout is {} milliseconds", milliseconds);
    }
    

    So you can always call such a method to be sure you know actual timeout, not the value you tried to set.

    0 讨论(0)
  • 2021-01-17 16:11

    TL;DR - This is not a solution to get implicit waits. You cannot get the implicit wait in Java even today, without using a workaround like this.

    In 2020, selenium 3.141.59 still does not have a getter for any timeouts. The WebDriver interface has a nested interface Timeouts which does not define any getters. RemoteWebDriver, which is the parent of Chrome and Firefox drivers, implements the WebDriver interface and it does not add a getter for timeouts.

    RemoteWebDriver implements WebDriver.Timeouts, but it does not store the value of implicit timeout anywhere, as you can see from the code below.

    protected class RemoteTimeouts implements Timeouts {
    
      public Timeouts implicitlyWait(long time, TimeUnit unit) {
        execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
            "implicit", TimeUnit.MILLISECONDS.convert(time, unit)));
        return this;
      }
    
      public Timeouts setScriptTimeout(long time, TimeUnit unit) {
        execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
            "script", TimeUnit.MILLISECONDS.convert(time, unit)));
        return this;
      }
    
      public Timeouts pageLoadTimeout(long time, TimeUnit unit) {
        execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
            "pageLoad", TimeUnit.MILLISECONDS.convert(time, unit)));
        return this;
      }
    } // timeouts class.
    

    The execute() method in the RemoteWebDriver takes the wait inside a Map of parameters, but it does not make that map or the wait settings accessible to us via a getter.

      protected Response execute(String driverCommand, Map<String, ?> parameters)
      //Open the source code to see why you can't make your own getter for implicitWait.
    
    0 讨论(0)
提交回复
热议问题