简体   繁体   中英

Selenium 2 webdriver with JUnit or TestNG for PageObject Design Pattern

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.

I am convinced that TestNG would suit for this approach by using @BeforeSuite annotation. Have described a simple example for this.

  1. Access Google Page.
  2. Search for gmail.com. Go to Gmail.com
  3. Test/Assert for "Create Account" text.
  4. Login with usn and pswd.
  5. Test/Assert for "inbox" on the homepage

PageObjects: GooglePage. LoginPage HomePage

Ex: Actual TestClass.

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);
    }  

}  

GooglePage - PageObject

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);
     }
  }

LoginPage - PageObject.

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();  
    }  
  }    

Homepage - PageObject

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();  
      }  
  }  

My question is:

  • How can I have something similar with JUnit as in >
  • series of user interactions
  • Test/assert something.
  • navigate again to the next page.
  • Test/assert something.

I am aware that the latest JUnit release 4.11 supports Test execution order.

Also the steps for "a series of workflow/interaction (before the actual test)" method can be annotated with the @BeforeClass.

But this annotations forces my method to be static and then all my pageobjects in the @BeforeClass method need to be declared as static.

(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. )

Hope I have made my case clear. Any inputs or feedback on this would be appreciated.

Thanks

  • How can I have something similar with JUnit as in > series of user interactions
  • Test/assert something.
  • navigate again to the next page

1) clarify your question - user interactions are done by selenium and not testNG
2)
- http://junit.sourceforge.net/javadoc/org/junit/Assert.html
- assert vs. JUnit Assertions
3) navigation is also done by selenium and neither testNG or Junit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM