2013-02-21 50 views
1

我使用Selenium網絡驅動程序+ TestNG並行運行我的硒測試。我用三個類無法使用硒網格從虛擬創建的testng.xml並行運行硒測試

一級(主類):

從這節課我要去開始測試。我執行的起點。

public class MainClass { 

public WebDriver driver = null; 

public void gid() { 

    try { 
     CreateTestngXml.gridHubLaunching(); 
     Thread.sleep(10000); 
    } catch (IOException | InterruptedException e) { 
     e.printStackTrace(); 
    } 

    CreateTestngXml.grid("firefox", "chrome"); 
    driver.close(); 
} 

public static void main(String a[]) { 
    MainClass ts = new MainClass(); 
    try{ 
    ts.gid(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 
} 

類雙(CreateTestngXml):

公共類CreateTestngXml {

/** 
* 
* @param Brow 
* @param Brows 
* @code = To create a TestNg XML suite file and run it. 
*/ 
public static void grid(String Brow, String Brows) { 

    XmlSuite suite = new XmlSuite(); 
    suite.setName("Compatability"); 
    suite.setVerbose(1); 
    suite.setPreserveOrder("true"); 
    suite.setThreadCount(4); 
    suite.setParallel("tests"); 
    suite.setTimeOut("5000"); 

    // Test 
    XmlTest test = new XmlTest(suite); 
    test.setName("Browser One"); 
    test.addParameter("Browser", Brow); 

    XmlTest testOne = new XmlTest(suite); 
    testOne.setName("Browser Two"); 
    testOne.addParameter("Browser", Brows); 

    List<XmlTest> tests = new ArrayList<XmlTest>(); 
    tests.add(test); 
    tests.add(testOne); 

    // Class 
    List<XmlClass> classes = new ArrayList<XmlClass>(); 
    classes.add(new XmlClass("Grid.CheckGridOne")); 
    test.setXmlClasses(classes); 
    testOne.setXmlClasses(classes); 

    suite.setTests(tests); 

    // Suite 
    List<XmlSuite> suites = new ArrayList<XmlSuite>(); 
    suites.add(suite); 
    TestNG tng = new TestNG(); 
    tng.setXmlSuites(suites); 
    try { 
     // Running the Suite file. 
     tng.run(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

/** 
* 
* @throws IOException 
* @throws InterruptedException 
* @code = To launch the Grid Hub and Nodes 
*/ 
public static void gridHubLaunching() throws IOException, 
     InterruptedException { 

    // Launching nodes for each browser. 
    String[] hub = new String[] { "C:\\Windows\\System32\\cmd.exe", "/c", 
      "Start", "Grid-BatchFiles\\hub.bat" }; 
    String[] firefox = new String[] { "C:\\Windows\\System32\\cmd.exe", 
      "/c", "Start", "Grid-BatchFiles\\firefox.bat" }; 
    String[] chrome = new String[] { "C:\\Windows\\System32\\cmd.exe", 
      "/c", "Start", "Grid-BatchFiles\\chrome.bat" }; 
    String[] ie = new String[] { "C:\\Windows\\System32\\cmd.exe", "/c", 
      "Start", "Grid-BatchFiles\\IE.bat" }; 
    Runtime.getRuntime().exec(hub); 
    Thread.sleep(3000); 
    Runtime.getRuntime().exec(firefox); 
    Runtime.getRuntime().exec(chrome); 
    Runtime.getRuntime().exec(ie); 
} 

    } 

gridHubLaunching()方法用於啓動所述輪轂和相應的瀏覽器的節點。它在創建節點時沒有問題。我通過使用http://localhost:4444/grid/console

類三(Grid.CheckGridOne)確保的創建:

public class CheckGridOne { 

MainClass ts = new MainClass(); 
WebDriver driver = ts.driver; 

DesiredCapabilities capability = null; 

/** 
* 
* @param Browser 
* @throws Exception 
* @code = To launch a Browser for Compatibility Testing purpose. 
*/ 

@Test 
@Parameters({ "Browser" }) 
public void browserLaunch(String Browser) throws Exception { 

    // Checking condition for a Firefox. 
    if (Browser.equalsIgnoreCase("firefox")) { 
     System.out.println("Firefox"); 

     capability = DesiredCapabilities.firefox(); 
     capability.setBrowserName("firefox"); 
     capability.setPlatform(org.openqa.selenium.Platform.WINDOWS); 

     URL url = new URL(`http://lo***st:4444/wd/hub`); 
     System.out.println("testing"); 
     driver = new RemoteWebDriver(url, capability); 

     System.out.println("test"); 
    } 

    // Checking condition for a iexplorer. 
    if (Browser.equalsIgnoreCase("iexplorer")) { 
     System.out.println("IE"); 

     System.setProperty(
       "webdriver.ie.driver", 
       "E:\\FW\\Test-2.28.0(Feb-18)\\IEDriverServer\\64-Bit\\IEDriverServer.exe"); 

     capability = DesiredCapabilities.internetExplorer(); 
     capability.setBrowserName("internet explorer"); 
     capability.setPlatform(org.openqa.selenium.Platform.ANY); 
     System.out.println("testing"); 
     driver = new RemoteWebDriver(new URL(
        `http://lo***st:4444/wd/hub`), capability); 
     System.out.println("test"); 
    } 

    // Checking condition for a chrome. 
    if (Browser.equalsIgnoreCase("chrome")) { 
     System.out.println("chrome"); 

    } 
} 
    } 

通過使用上述3類我試圖並行運行硒測試。我的問題是它在第三節課到達driver = new RemoteWebDriver(url, capability);之前工作正常。一旦到達線路,它將終止TestNG suite file。我不知道我的代碼有什麼問題?

輸出:

[TestNG] Running: 
    Command line suite 

Firefox 
IE 
testing 
testing 

=============================================== 
Compatability 
Total tests run: 0, Failures: 0, Skips: 0 
=============================================== 

test 
test 

輸出顯示總測試運行作爲0。我不知道它爲什麼會這樣。

Selenium服務器 - 2.28.0
TestNG的 - 6.1.1 & 6.8
IEDriverServer - 2.28.0

我知道這個問題有點大。我認爲這是解釋清楚的正確方法。任何幫助,將不勝感激。

+0

你確定這個零件正在工作嗎? 'classes.add(new XmlClass(「Grid.CheckGridOne」));'即將測試類添加到testng xml中,是否可以發佈您的輸出testng xml – StaleElementException 2013-02-25 15:04:18

+0

是的。上面的工作正常。它可以調用類中的'@ Test'方法。 XML文件在運行時創建。所以,不可能從外部看到XML文件。如果你需要我可以發佈它如何創建XML文件。 – Manigandan 2013-02-26 03:27:41

+0

我想你可以在/ test-output文件夾下看到創建的xml文件 – StaleElementException 2013-02-26 09:11:14

回答

0

DIT:你爲什麼要從第三堂課回到第一堂課?這看起來像一個循環。

只需在第三堂課中定義您的WebDriver即可。

public class CheckGridOne { 

@Test 
@Parameters({ "Browser" }) 
public void browserLaunch(String Browser) throws Exception { 

WebDriver driver = null; 

    // Checking condition for a Firefox. 
    if (Browser.equalsIgnoreCase("firefox")) { 
     System.out.println("Firefox"); 

     capability = DesiredCapabilities.firefox(); 
     capability.setBrowserName("firefox"); 
     capability.setPlatform(org.openqa.selenium.Platform.WINDOWS); 

     URL url = new URL(`http://lo***st:4444/wd/hub`); 
     System.out.println("testing"); 
     driver = new RemoteWebDriver(url, capability); 

     System.out.println("test"); 
    } 

    // Checking condition for a iexplorer. 
    if (Browser.equalsIgnoreCase("iexplorer")) { 
     System.out.println("IE"); 

     System.setProperty(
       "webdriver.ie.driver", 
       "E:\\EproNewFW\\SWIFTest-2.28.0(Feb-18)\\IEDriverServer\\64-Bit\\IEDriverServer.exe"); 

     capability = DesiredCapabilities.internetExplorer(); 
     capability.setBrowserName("internet explorer"); 
     capability.setPlatform(org.openqa.selenium.Platform.ANY); 
     System.out.println("testing"); 
     driver = new RemoteWebDriver(new URL(
        `http://lo***st:4444/wd/hub`), capability); 
     System.out.println("test"); 
    } 

    // Checking condition for a chrome. 
    if (Browser.equalsIgnoreCase("chrome")) { 
     System.out.println("chrome"); 

    } 
} 
    }