Exemplo n.º 1
0
 @AfterTest
 protected void afterTest(ITestContext context) {
   WebDriver webdriver = getWebDriverInstance(context);
   log.info(Utilities.getCurrentThreadId() + "Closing the instance:" + webdriver.toString());
   webdriver.quit();
   context.removeAttribute(context.getCurrentXmlTest().getName());
 }
  public static String getLocation(WebDriver webDriver) throws Exception {
    List<Exception> exceptions = new ArrayList<>();

    for (int i = 0; i < 3; i++) {
      FutureTask<String> futureTask =
          new FutureTask<>(
              new Callable<String>() {

                @Override
                public String call() throws Exception {
                  return _webDriver.getCurrentUrl();
                }

                private Callable<String> init(WebDriver webDriver) throws Exception {

                  _webDriver = webDriver;

                  return this;
                }

                private WebDriver _webDriver;
              }.init(webDriver));

      Thread thread = new Thread(futureTask);

      thread.start();

      try {
        String location = futureTask.get(PropsValues.TIMEOUT_EXPLICIT_WAIT, TimeUnit.SECONDS);

        return location;
      } catch (CancellationException ce) {
        exceptions.add(ce);
      } catch (ExecutionException ee) {
        exceptions.add(ee);
      } catch (InterruptedException ie) {
        exceptions.add(ie);
      } catch (TimeoutException te) {
        exceptions.add(te);
      } finally {
        thread.interrupt();
      }

      System.out.println("WebDriverHelper#getLocation(WebDriver):");
      System.out.println(webDriver.toString());

      Set<String> windowHandles = webDriver.getWindowHandles();

      for (String windowHandle : windowHandles) {
        System.out.println(windowHandle);
      }
    }

    if (!exceptions.isEmpty()) {
      throw new Exception(exceptions.get(0));
    } else {
      throw new TimeoutException();
    }
  }