2017-10-11 161 views
0

我想用testng並行運行我的自動化測試(Selenium webdriver)。這是我正在運行的節點:如何使用testng並行運行我的硒測試方法

java -Dwebdriver.gecko.driver=chromedriver.exe -jar selenium-server-standalone-3.4.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=chrome,maxInstances=2 -maxSession 2 

這是我的測試類:

public class TestParallel { 

Login login; 

//@BeforeMethod(alwaysRun = true) 
public SeleniumDriverCore testSetup() throws FileNotFoundException, IOException{ 
    SeleniumDriverCore driver = new SeleniumDriverCore("config/chromeDriverConfig"); 
    Properties config = new Properties(); 
    config.load(new FileInputStream("config/testConfig")); 
    this.login = new Login(driver); 
    driver.browser.open("https://test.test.xyz"); 

    driver.browser.maximize(); 
    driver.waits.waitForPageToLoad(); 
    return driver; 
} 

@Test(groups={"parallel"}) 
public void test_one() throws FileNotFoundException, IOException{ 
    SeleniumDriverCore driver=testSetup(); 
    login.navigateToPage(Pages.LOGIN); 
    login.assertion.verifyLoginPopupAndTitleDisplayed(); 
    testCleanup(driver); 
} 

@Test(groups={"parallel"}) 
public void test_two() throws FileNotFoundException, IOException{ 
    SeleniumDriverCore driver=testSetup(); 
    login.navigateToPage(Pages.LOGIN); 
    login.assertion.verifyLoginPopupAndTitleDisplayed(); 
    testCleanup(driver); 
} 

@Test(groups={"parallel"}) 
public void test_three() throws FileNotFoundException, IOException{ 
    SeleniumDriverCore driver=testSetup(); 
    login.navigateToPage(Pages.LOGIN); 
    login.assertion.verifyLoginPopupAndTitleDisplayed(); 
    testCleanup(driver); 
} 

@Test(groups={"parallel"}) 
public void test_four() throws FileNotFoundException, IOException{ 
    SeleniumDriverCore driver=testSetup(); 
    login.navigateToPage(Pages.LOGIN); 
    login.assertion.verifyLoginPopupAndTitleDisplayed(); 
    testCleanup(driver); 
} 


public void testCleanup(SeleniumDriverCore driver){ 
    driver.close(); 
    driver.quit(); 
} 

}

這裏是我的xml:

<suite name="Ontega - All Tests Mobile" parallel="methods" thread-count="2"> 
    <test name="Ontega - All Tests Mobile"> 
     <groups> 
      <run> 
       <include name="parallel"/> 
       <exclude name="open-defects"/> 
      </run> 
     </groups> 
     <packages> 
      <package name="tests.*"/> 
     </packages> 
    </test> 
</suite> 

當我運行XML,我希望我的測試能夠在兩個線程中同時在兩個瀏覽器上運行,但是當我運行XML時,我得到兩個瀏覽器ances在第一時間運行,然後他們增加和50%的測試失敗,你可以看到我試圖在我的每個方法中實例化驅動程序,雖然這不是我的框架如何工作,但我試圖去解決這個問題的瓶頸。 任何幫助將不勝感激 在此先感謝

+1

要創建每個測試方法調用兩個司機,你也只需要調用退出時,你拆掉 –

+0

我已經修復了我的代碼,現在的問題是,鉻頁面打開一個空白頁,就是這樣。 –

回答

2

以下是在TestNG中執行此操作的一些方法。您基本上通過@BeforeMethod@AfterMethod配置方法管理您的webdriver實例化和清理。那麼您需要決定如何與您的@Test方法共享創建的webdriver實例。對於你有三種選擇:

  1. 您使用一個ThreadLocal變種,因爲TestNG的保證給你,它會在同一個線程執行@BeforeMethod@Test@AfterMethod所有。

這裏的一個樣品,顯示您這個動作

import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.remote.RemoteWebDriver; 
import org.testng.ITestResult; 
import org.testng.Reporter; 
import org.testng.annotations.AfterMethod; 
import org.testng.annotations.BeforeMethod; 
import org.testng.annotations.DataProvider; 
import org.testng.annotations.Test; 

public class TestClassSampleUsingThreadLocal { 
    private static final ThreadLocal<RemoteWebDriver> drivers = new ThreadLocal<>(); 

    @BeforeMethod 
    public void instantiateBrowser(ITestResult testResult) { 
     RemoteWebDriver driver = new ChromeDriver(); 
     drivers.set(driver); 
    } 

    @Test(dataProvider = "dp") 
    public void testMethod(String url) { 
     Reporter.log("Launching the URL [" + url + "] on Thread [" + Thread.currentThread().getId() + "]", true); 
     driver().get(url); 
     Reporter.log("Page Title :" + driver().getTitle(), true); 
    } 

    @DataProvider(name = "dp", parallel = true) 
    public Object[][] getData() { 
     return new Object[][]{ 
       {"http://www.google.com"}, {"http://www.stackoverflow.com"}, {"http://facebook.com"} 
     }; 
    } 

    @AfterMethod 
    public void cleanupBrowser() { 
     RemoteWebDriver driver = driver(); 
     driver.quit(); 
    } 

    private RemoteWebDriver driver() { 
     RemoteWebDriver driver = drivers.get(); 
     if (driver == null) { 
      throw new IllegalStateException("Driver should have not been null."); 
     } 
     return driver; 
    } 

} 
  • 您可以通過ITestResult對象共享的webdriver實例。以下是一個示例,展示了這一行動。
  • import org.openqa.selenium.chrome.ChromeDriver; 
    import org.openqa.selenium.remote.RemoteWebDriver; 
    import org.testng.ITestResult; 
    import org.testng.Reporter; 
    import org.testng.annotations.AfterMethod; 
    import org.testng.annotations.BeforeMethod; 
    import org.testng.annotations.DataProvider; 
    import org.testng.annotations.Test; 
    
    public class TestClassSample { 
        private static final String WEBDRIVER = "driver"; 
    
        @BeforeMethod 
        public void instantiateBrowser(ITestResult testResult) { 
         RemoteWebDriver driver = new ChromeDriver(); 
         testResult.setAttribute(WEBDRIVER, driver); 
        } 
    
        @Test(dataProvider = "dp") 
        public void testMethod(String url) { 
         Reporter.log("Launching the URL [" + url + "] on Thread [" + Thread.currentThread().getId() + "]", true); 
         driver().get(url); 
         Reporter.log("Page Title :" + driver().getTitle(), true); 
        } 
    
        @DataProvider(name = "dp", parallel = true) 
        public Object[][] getData() { 
         return new Object[][]{ 
           {"http://www.google.com"}, 
           {"http://www.stackoverflow.com"}, 
           {"http://facebook.com"} 
         }; 
        } 
    
        @AfterMethod 
        public void cleanupBrowser(ITestResult testResult) { 
         RemoteWebDriver driver = driver(testResult); 
         driver.quit(); 
        } 
    
        private RemoteWebDriver driver() { 
         return driver(Reporter.getCurrentTestResult()); 
        } 
    
        private RemoteWebDriver driver(ITestResult testResult) { 
         if (testResult == null) { 
          throw new IllegalStateException("testResult should have not been null."); 
         } 
         Object driverObject = testResult.getAttribute(WEBDRIVER); 
         if (driverObject == null) { 
          throw new IllegalStateException("Driver should have not been null."); 
         } 
         if (!(driverObject instanceof RemoteWebDriver)) { 
          throw new IllegalStateException("Driver is not a valid webdriver object"); 
         } 
         return (RemoteWebDriver) driverObject; 
        } 
    } 
    
  • 您提取出的webdriver實例化和淨化成TestNG的監聽器(一個實現一個org.testng.IInvokedMethodListener其設定所創建的webdriver到ITestResult(如圖選項2 )或成ThreadLocal(如選項1所示),你可以在我的blog post找到關於使用代碼片段沿此選項的詳細信息。
  • +0

    非常感謝您的回答! –

    0

    看來你創建每個測試多個驅動程序。我猜你需要保持testSetup以外的代碼的一些部分()方法

    SeleniumDriverCore driver = new SeleniumDriverCore("config/chromeDriverConfig"); 
    Properties config = new Properties(); 
    config.load(new FileInputStream("config/testConfig")) 
    

    我分享我的代碼,我使用了並行測試的一部分,可能是將你的情況幫enter image description here

    我的xml enter image description here

    +0

    感謝您的評論@Shoaib,在並行方法中運行測試以及在測試方法之外聲明驅動程序的問題是驅動程序實例狀態在測試執行期間可能會被覆蓋 –