title: Selenium 2 webdriver with JUnit or TestNG for PageObject Design Pattern
body: <p>We are trying to choose between Junit and TestNG for our Test framework with Selenium 2 Webdriver. 
  We have decided to use Page object design pattern for this. </p>
  
  <p>I am convinced that TestNG would suit for this approach by using @BeforeSuite annotation.
  Have described a simple example for this. </p>
  
  <ol>
  <li>Access Google Page.</li>
  <li>Search for gmail.com. Go to Gmail.com</li>
  <li>Test/Assert for "Create Account" text.</li>
  <li>Login with usn and pswd.</li>
  <li>Test/Assert for "inbox" on the homepage</li>
  </ol>
  
  <p>PageObjects:
  GooglePage.
  LoginPage
  HomePage</p>
  
  <p>Ex: Actual TestClass.</p>
  
  <pre><code>public class firstTest {  
  
      private final WebDriver driver = new FirefoxDriver();  
      private final  String url = "www.google.com"
      String keyword = "gmail.com";
      GooglePage gPage ;
      LoginPage lPage ;
  
  
    @BeforeSuite(alwaysRun = true)
      public void testWorkFlow{
      driver.get(url);
      gPage = new GooglePage(driver);
      lPage= gPage.searchKeyword(keyword);
   }
  
      @Test  
      public void testLoginPageMsg() throws Exception {  
          assertTrue(lPage.contqains("A Google approach to email."));
       }  
  
      @Test  
      public void testHomePage() throws Exception {  
         HomePage hPage = loginPage.loginWith("abcdef", "ghijklmno");  
         String h1Msg = hPage.gteMsg();
         assertEquals("inbox", h1Msgx);
      }  
  
  }  
  </code></pre>
  
  <p>GooglePage - PageObject</p>
  
  <pre><code>public class GooglePage {  
  
      private final WebDriver driver;  
        public GooglePage(WebDriver driver) {  
          super();  
          this.driver = driver;  
       }  
  
       public LoginPage searchKeyword(String keyword){
       driver.findElement(By.id("gbqfq")).sendKeys(keyword); 
       driver.findElement(By.id("submit")).submit(); 
       driver.findElement(By.linkText(keyword));
  
       return new LoginPage(driver);
       }
    }
  </code></pre>
  
  <p>LoginPage - PageObject.</p>
  
  <pre><code>public class LoginPage {  
        private final WebDriver driver;
  
        public LoginPage(WebDriver driver) {  
          super();  
          this.driver = driver;  
       }  
  
      public HomePage loginWith(String username, String password) {  
            executeLogin(username, password);  
          return new HomePage(driver);  
      }  
  
      private void executeLogin(String username, String password) {  
            driver.findElement(By.id("username")).sendKeys(username);  
          driver.findElement(By.id("password")).sendKeys(password);  
          driver.findElement(By.id("submit")).submit();  
      }  
    }    
  </code></pre>
  
  <p>Homepage - PageObject</p>
  
  <pre><code>public class HomePage {  
  
      private final WebDriver driver;  
  
       public HomePage(WebDriver driver) {  
          super();  
          this.driver = driver;  
         }  
  
      public String getMsg() throws Exception{  
            return driver.findElement(By.id("h1")).getText();  
        }  
    }  
  </code></pre>
  
  <p>My question is:</p>
  
  <ul>
  <li>How can I have something similar with JUnit as in ></li>
  <li>series of user interactions</li>
  <li>Test/assert something.</li>
  <li>navigate again to the next page.</li>
  <li>Test/assert something.</li>
  </ul>
  
  <p>I am aware that the latest JUnit release 4.11 supports Test execution order. </p>
  
  <p>Also the steps for "a series of workflow/interaction (before the actual test)" method can be annotated with the @BeforeClass. </p>
  
  <p>But this annotations forces my method to be static and then all my pageobjects in the @BeforeClass method need to be declared as static.</p>
  
  <p>(The above sample is just an example I wanted to describe my case. In actual I have quite a number of pageobjects and their methods being called in this method before asserting them. )</p>
  
  <p>Hope I have made my case clear. 
  Any inputs or feedback on this would be appreciated.</p>
  
  <p>Thanks</p>