简体   繁体   English

Selenium 2 Webdriver与JUnit或TestNG用于PageObject设计模式

[英]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. 我们正在尝试使用Selenium 2 Webdriver在我们的测试框架中选择Junit和TestNG。 We have decided to use Page object design pattern for this. 我们已决定为此使用Page对象设计模式。

I am convinced that TestNG would suit for this approach by using @BeforeSuite annotation. 我坚信TestNG通过使用@BeforeSuite批注将适合这种方法。 Have described a simple example for this. 为此描述了一个简单的例子。

  1. Access Google Page. 访问Google Page。
  2. Search for gmail.com. 搜索gmail.com。 Go to Gmail.com 转到Gmail.com
  3. Test/Assert for "Create Account" text. 测试/声明“创建帐户”文本。
  4. Login with usn and pswd. 用usn和pswd登录。
  5. Test/Assert for "inbox" on the homepage 在首页上测试/声明“收件箱”

PageObjects: GooglePage. PageObjects:GooglePage。 LoginPage HomePage LoginPage主页

Ex: Actual TestClass. 例如:实际的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 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. 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 主页-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 > 我如何与JUnit类似,如>
  • 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. 我知道最新的JUnit版本4.11支持测试执行顺序。

Also the steps for "a series of workflow/interaction (before the actual test)" method can be annotated with the @BeforeClass. 同样,“一系列工作流程/交互(在实际测试之前)”方法的步骤也可以使用@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. 但是此注释强制我的方法是静态的,然后@BeforeClass方法中的所有页面对象都需要声明为静态。

(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 如何在一系列用户交互中使用与JUnit类似的东西
  • Test/assert something. 测试/声明某些内容。
  • navigate again to the next page 再次导航到下一页

1) clarify your question - user interactions are done by selenium and not testNG 1)澄清您的问题-用户互动是通过硒完成的,而不是testNG
2) 2)
- http://junit.sourceforge.net/javadoc/org/junit/Assert.html -http://junit.sourceforge.net/javadoc/org/junit/Assert.html
- assert vs. JUnit Assertions - 断言与JUnit断言
3) navigation is also done by selenium and neither testNG or Junit 3)导航也由硒完成,而testNG或Junit都不

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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