2015-12-22 91 views
0

我有一個多個Test類,我想執行4個測試,但是,我想執行的4個測試是不同類文件中的不同類。所以我想在我的多個Test類中每次調用每個類1。所以代碼正在各個類中執行,多個Test類實際上只是根據優先級來處理每個測試的執行。我只是不確定如何調用另一個類來執行。TestNG多測試類 - 運行其他類

我多類:

@Test(priority = 0) //Set Priority of Test - Priority of test always starts from Zero 
    public void one() { 

    System.out.println("This is Test Case 1"); 
    } 
     //Test 2 
    @Test(priority = 1) // Test priority 1 
    public void Two(){ 

    System.out.println("This is Test Case 2"); 
     } 

我需要在@Test塊來執行我的外部類。新自動化可能我的問題是我對java的理解?

如何我通過我的參數:在執行這些主類文件單獨的類文件&

public class TestNGDataProvider { 

    private WebDriver driver; 
private static Logger logger1 = LogManager.getLogger("Logger1"); 

@Test(dataProvider = "Authentication") 
public void testMethod1(String sUsername, String sPassword, String sMemorableWord) { 

    DOMConfigurator.configure("log4j.xml"); 

driver = new FirefoxDriver(); 

    logger1.info("New Instance of firefox created"); 
    //Add wait to allow web elements to load 

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

    logger1.info("Timeout Applied for 10 seconds to Allow Load of Elements"); 
    //Launch FireFoxDriver - Open WebPage 

    driver.get("http://localhost/2010A15/"); 

    logger1.info("Website Launched"); 
    Reporter.log("Website Lauched Successfully | "); //Main Event Logger/Report 

    //Find Login Element 
    driver.findElement(By.id("WEB_LoginButton")).click(); 

    logger1.info("Login Button Clicked"); 
    //Find User name Element 

    driver.findElement(By.id("dijit_form_ValidationTextBox_1")).sendKeys(sUsername); 

    logger1.info("Username Entered"); 

    //Find Password Element 

    driver.findElement(By.id("dijit_form_ValidationTextBox_2")).sendKeys(sPassword); 




    @DataProvider(name = "Authentication") 
    public static Object[][] credentials() { 
return new Object[][] { { "[email protected]", "password1", "smith" }, { "[email protected]", "password1", "smith" }, { "[email protected]", "password1", "smith" }}; 
}} 
+0

所以,你有4個差異類與4個測試方法,你想所有的測試類應該運行所有4個測試用例基於優先權嗎? – Paras

回答

1

按我的理解,你已經處理測試邏輯。 所以,如果你有以下2類測試,

public class TestLogic1 
{ 
    public void testMethod1() 
    { 
     System.out.println("Test 1 executing"); 
    } 
} 
public class TestLogic2 
{ 
    public void testMethod2() 
    { 
     System.out.println("Test 2 executing"); 
    } 
} 

然後就可以調用,這些測試方法在執行類的,

public class TestExecute 
{ 
    TestLogic1 method1=new TestLogic1(); 
    TestLogic2 method2=new TestLogic2(); 

    @Test(priority=1) 
    public void test1() 
    { 
     method1.testMethod1(); 
    } 
@Test(priority=2) 
    public void test2() 
    { 
     method2.testMethod2(); 
    } 
} 

編輯: 假設你已經寫了@Test方法& @DataProvider在一個類中&處理它在其他類中的執行順序。使用下面的代碼來處理來自DataProvider的參數。

public class TestExecute{ 

TestNGDataProvider tg=new TestNGDataProvider(); 

@Test(dataProvider = "Authentication",dataProviderClass = TestNGDataProvider.class) 
public void test1(String login,String user,String pass) throws InterruptedException { 

    tg.testMethod1(login,user,pass); 
} 
} 

希望這是你在找什麼。 也look here有關TestNG數據提供程序的更多詳細信息。

+0

在MultipleTest類中調用的外部類 公用類TestNGDataProvider私有WebDriver驅動程序; \t private static Logger logger1 = LogManager.getLogger(「Logger1」); \t @Test(數據提供程序= 「認證」) 公共無效testMethod1(字符串sUsername,字符串spassword開頭,字符串sMemorableWord) method1.testmethod1();被強調爲紅色,就像存在語法問題一樣。我是否錯了?我相信這個問題與參數有關? – Speedychuck

+0

當然,你需要實例化一個外部類的對象來訪問它的方法。就像我在回答'TestLogic1 method1 = new TestLogic1();'中所做的那樣;'你正在採取這一步嗎? – MKay

+0

是採取那一步:TestNGDataProvider method1 = new TestNGDataProvider(); – Speedychuck