【问题标题】:Wait for window to open before attempting to switch to it在尝试切换到它之前等待窗口打开
【发布时间】:2020-12-18 17:31:01
【问题描述】:

我的测试出现问题,我选择了一个按钮,然后切换到在我选择按钮后打开的windwo,问题是我一直没有收到帧异常和超出范围异常。

我的代码是:

        clickWithActions(driver, launchNote);
        sleepForTime(6000);
        Set<String> handles = driver.getWindowHandles();

        List list = Arrays.asList(handles.toArray());
        driver.switchTo().window(list.get(1).toString());
        sleepForTime(6000);
        driver.manage().window().maximize();
        driver.manage().window().setSize(new Dimension(1920, 1080));

        break;

有什么方法可以让我等待这个窗口首先出现而无需长时间等待?

【问题讨论】:

    标签: java selenium testing ui-testing


    【解决方案1】:

    这个numberOfWindowsToBe() 预期的等待条件听起来很合适:

    clickWithActions(driver, launchNote);
    
    // wait for the number of windows to increase
    new WebDriverWait(driver, 15).until(ExpectedConditions.numberOfWindowsToBe(2));
    

    【讨论】:

      【解决方案2】:

      在执行clickWithActions() 之前,您需要收集初始的WindowHandle,一旦您执行点击,您需要为numberOfWindowsToBe(2) 诱导WebDriverWait。所以你的有效代码块将是:

      String first_handle = driver.getWindowHandle();
      clickWithActions(driver, launchNote);
      new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
      Set<String> allHandles = driver.getWindowHandles();
      for(String winHandle:allHandles)
      {
          if (!first_handle.equalsIgnoreCase(winHandle)
          {
          driver.switchTo().window(winHandle);
          }
      }
      

      参考文献

      您可以在以下位置找到一些相关的详细讨论:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        相关资源
        最近更新 更多