使用Testng将多个selenium java测试用例作为套件运行

时间:2014-07-18 07:42:52

标签: java selenium selenium-webdriver testng

大家好我已经在selenium中编写了多个测试用例,使用WebDriver

执行以下任务
  1. 登录我的门户网站
  2. 添加群组
  3. 添加位置
  4. 以下是我在门户网站上执行此任务的硒代码

    Base Selenium Class

    public class BaseSeleniumTest extends SeleneseTestBase {
        protected static WebDriver driver;
        @BeforeSuite
        public static void firefoxSetUp() throws MalformedURLException {
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
        }
    
        @AfterSuite
        public static void closeFirefox(){
           driver.quit();
        }
    }//End BaseSeleniumTest Class.
    

    登录测试用例

    public class LogInTest extends BaseSeleniumTest {
        LogInPage page;
        public static final String BASE_URL = "http://www.mywebsite.com";
    
        @BeforeMethod
        public void beforeMethod() {
            page = PageFactory.initElements(driver, LogInPage.class);
            page.openPage(BASE_URL);
        }
    
        @Test(dataProvider="loginData",groups="loginGroup")
        public void logIn(String email,String password) {
            page.logInAs(email, password);
        }
    
        @DataProvider(name = "loginData")
        public Object[][] getData() {
            return new Object[][] { { "username", "password" } };
        }
    }//End of LogInTest Class.
    

    添加群组测试用例

    public class AddGroupTest extends BaseSeleniumTest {
        AddGroupPage addGroupPage;
          @BeforeMethod
          public void openAddGroupPage() {
              addGroupPage = PageFactory.initElements(driver, AddGroupPage.class);
              addGroupPage.openPage();
          }
    
        @Test(dataProvider = "getCSVData",dataProviderClass=AddGroupsDataProvider.class,dependsOnGroups={"loginGroup"},groups="addGroup")
        public void AddGroup(String groupName,String displayName,String description) {
                addGroupPage.addGroup(groupName, displayName, description);
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
    }//End of AddGroupTest Class.
    

    添加位置测试用例

    public class AddLocationTest extends BaseSeleniumTest {
        AddLocationPage addLocationPage;
        @BeforeMethod
          public void openAddLocationPage() {
              addLocationPage = PageFactory.initElements(driver, AddLocationPage.class);
              addLocationPage.openPage();
         }
    
        @Test(dataProvider = "getCSVData", dataProviderClass = AddLocationDataProvider.class, dependsOnGroups = { "loginGroup" }, groups = "addLocation")
        public void addLocation(String locationName, String displayName,
                String description, String groups) {
            addLocationPage.addLocation(locationName, displayName, description,
                    groups.split(","));
        }
    }  
    

    当我独立运行这个脚本时,它们运行得很好我现在想把这个脚本作为套件来运行,但我不知道从哪里开始。由于依赖于Add Group和Add Group测试用例的Add Location Test案例依赖于Login测试用例。

    以下是我的问题

    1. 我做了什么?以套件形式运行这个测试用例。(后来我必须通过jar文件运行这个套件)
    2. 如何指定它们之间的依赖关系?
    3. 提前致谢...

1 个答案:

答案 0 :(得分:1)

您的问题会导致基于意见的答案,通常为off-topic for SO。考虑浏览Software Quality Assurance & Testing Stack Exchange

我打算试着给你至少一些答案。你应该:

  1. 考虑选择您的测试框架。像TestNG allows you to specify dependencies
  2. 之类的东西
  3. 考虑测试的架构。你的一些测试"作为单独的方法可能会更好,然后您可以从测试中调用。考虑使用PageObject model