title: Selenium Framework Page Object Model and Page Navigation
body: <p>I am learning how to create framework for web applications using Selenium. I am now learning how to create page object models with page navigation. The video I am currently on is showing how to do this, but the code at the end of the video does not work. He is trying to use a return type of the Class to initialize all the objects in that class, and he fails in doing so, because he uses a non-parameterized constructor. So is there a way to initialize my page elements when using a non-parameterized constructor? </p>
  
  <p>Video located here: <a href="http://youtu.be/IUBwtLG9hbs" rel="nofollow">http://youtu.be/IUBwtLG9hbs</a></p>
  
  <p>In the above video, he is just creating a simple test to enter the text "Selenium" into the google text box. Click on search. Click on the Selenium link. Then click on the Download tab on the Selenium webpage. </p>
  
  <p>At the end of the video, he is trying to show how to use Page Navigation by initializing the elements after a button or link is clicked that sends you to a new page. The goal of this test is to map all of the elements of the Selenium web page after the click of the Selenium link from the Google page. </p>
  
  <p>So he adds "return new SeleniumPageObjects();" to the following method in the GoogleHomePageObjects class:</p>
  
  <pre><code>public SeleniumPageObjects ClickSelenium()
  {
      lnkSelenium.Click();
      return new SeleniumPageObjects();
  }
  </code></pre>
  
  <p>Then in the main/test method in the Test class, he adds the following to click on the Selenium link and then return all of the mapped objects of the Selenium page:</p>
  
  <pre><code>SeleniumPageObjects selPage = page.ClickSelenium();
  </code></pre>
  
  <p>He then adds a constructor in the SeleniumPageObjects class:</p>
  
  <pre><code>public SeleniumPageObjects()
  {
  }
  </code></pre>
  
  <p>It throws a NullReferenceException because it never initialized the selenium page objects (See below code of how it is done without using a return type). How can this be fixed? He says to watch part 7, but I watched part 7 and the erroneous code is never addressed.</p>
  
  <p>NOTE: I already know how to explicitly initialize page objects in my main/test method. For instance, add this to the main/test method:</p>
  
  <pre><code>    SeleniumPageObjects selPage = new SeleniumPageObjects(driver);
  </code></pre>
  
  <p>And initialize the elements in the SeleniumPageObjects class:</p>
  
  <pre><code>public SeleniumPageObjects(IWebDriver driver)
  {
      PageFactory.InitElements(driver, this);
  }
  </code></pre>
  
  <p>I just want to know how to do this when using a return type, as this accomplishes the Page Relation he is talking about.</p>