View Javadoc

1   package com.atlassian.webdriver.testing.rule;
2   
3   import com.atlassian.webdriver.browsers.WebDriverBrowserAutoInstall;
4   import com.google.common.base.Supplier;
5   import com.google.common.base.Suppliers;
6   import org.junit.rules.ExternalResource;
7   import org.openqa.selenium.WebDriver;
8   
9   import javax.inject.Inject;
10  
11  /**
12   * <p/>
13   * A simple rule to clear the browsers session at the end of a test. This removes the need to have an @After
14   * method that logs the user out of the TestedProduct.
15   *
16   * <p/>
17   * This simply clears the cookies from the browser making it much quicker then actually
18   * going through the products logout process.
19   *
20   * @since 2.1.0
21   */
22  public class SessionCleanupRule extends FailsafeExternalResource
23  {
24  
25      private final Supplier<? extends WebDriver> webDriver;
26  
27      public SessionCleanupRule(Supplier<? extends WebDriver> webDriver)
28      {
29          this.webDriver = webDriver;
30      }
31  
32      @Inject
33      public SessionCleanupRule(WebDriver webDriver)
34      {
35          this(Suppliers.ofInstance(webDriver));
36      }
37  
38      public SessionCleanupRule()
39      {
40          this(WebDriverBrowserAutoInstall.driverSupplier());
41      }
42  
43      @Override
44      protected void before() throws Throwable
45      {
46          cleanUp();
47      }
48  
49      @Override
50      protected void after()
51      {
52          cleanUp();
53      }
54  
55      private void cleanUp()
56      {
57          final WebDriver driver = webDriver.get();
58          if (driver != null)
59          {
60              driver.manage().deleteAllCookies();
61          }
62      }
63  }