title: How to represent Page Object navigation in a Selenium WebDriver project using java
body: <p>I have read in a few places that Navigating between Pages when using WebDriver API is typically done by the Page Object returning the next page that should be returned when the action that causes the navigation to occur.</p>
  
  <p>The solution works great for actions that always return the same page (eg, SearchPage.search() returning new ResultPage) but what happens when an action may put the user on a different page depending on some input or state. Java does not allow overloading of methods based on return type. So how can this be done?</p>
  
  <p>I had a rough attempt at implementing a <code>Navigator</code> object that could be used to return a generic <code>Page</code> object based on what the expected result was.</p>
  
  <pre><code>public class Navigator {
  
      private WebDriver driver;
      private String baseUrl;
      private Map&lt;Class, String&gt; pagesToRelativePaths;
  
  
      public Navigator(WebDriver driver) {
          this.driver = driver;
      }
  
      public Navigator(WebDriver driver, String baseUrl, Map&lt;Class, String&gt; pagesToRelativePaths) {
          this.driver = driver;
  
          setBaseUrl(baseUrl);
          setPagesToRelativePaths(pagesToRelativePaths);
      }
  
      public void setBaseUrl(String baseUrl) {
          this.baseUrl = baseUrl;
      }
  
      public void setPagesToRelativePaths(Map&lt;Class, String&gt; pagesToRelativePaths) {
          this.pagesToRelativePaths = pagesToRelativePaths;
      }
  
      public &lt;T extends PageBase&gt; T navigateTo(Class&lt;T&gt; type) {
          driver.get(baseUrl + pagesToRelativePaths.get(type));
  
          return type.cast(new PageBase(driver));
      }
  
      public &lt;T extends PageBase&gt; T expectedLandingPage(Class&lt;T&gt; type) {
          return type.cast(new PageBase(driver));
      }
  }
  </code></pre>
  
  <p>Action that would result in the page moving from one Page Object to another would implement a method like so:</p>
  
  <pre><code>public Navigator signIn(String username, String password) {
      usernameField.type(username);
      passwordField.type(password);
      signInButton.click();
  
      return new Navigator(driver);
  }
  </code></pre>
  
  <p>and a Test would look like this:</p>
  
  <pre><code>@Test
  public void SignInWithInvalidCredentialsTest() {
      SignInPage signInPage = navigator.navigateTo(SignInPage.class);
  
      signInPage = signInPage.signIn("invalid", "invalid").expectedLandingPage(SignInPage.class);
  
      assertThat(signInPage.getTitle(), is(equalTo(driver.getTitle())));
  }
  </code></pre>
  
  <p>But unfortunately I couldn't get the Navigator to go to any page based on user input. Is there a way to do this that works or is there an alternative way to do this?</p>
  
  <p>Source:</p>
  
  <ul>
  <li><a href="http://code.google.com/p/selenium/wiki/PageObjects" rel="nofollow noreferrer">Selenium Wiki: PageObjects</a></li>
  <li><a href="https://stackoverflow.com/questions/10315894/selenium-webdriver-page-object">Stack Overflow Question on Page Object navigation</a></li>
  </ul>