2017-11-08 9 views
0

私はGWTアプリケーションをテストしており、TestNGを使用してテストを並列に実行するタスクが与えられています。今、私はブラウザを起動し、いくつかのブラウザ関連の操作を実行する次のコードを書いた。TestNGを使用してSelenium Grid Testを実行中にThreadlocal.get()がnullを返す

public class Browser { 
    //ThreadLocal will provide thread-safe tests 
    public static ThreadLocal<RemoteWebDriver> threadLocal = null; 
    String hubMachineIPAddress = "http://xx.xx.xx.xx:4444/wd/hub"; 

    @BeforeTest 
    @Parameters("browser") 
    public void setBrowser(String browser) throws MalformedURLException{ 

     if(browser.equalsIgnoreCase("chrome")) 
     { 
      System.setProperty("webdriver.chrome.driver", ".src/Drivers/chromedriver.exe"); 
      DesiredCapabilities capability = DesiredCapabilities.chrome(); 
      capability.setPlatform(Platform.WINDOWS); 
      capability.setBrowserName("chrome"); 
      createRemoteWebDriver(capability); 
     } 
     else if(browser.equalsIgnoreCase("firefox")) 
     { 
      System.setProperty("webdriver.gecko.driver", ".src/Drivers/geckodriver.exe"); 
      DesiredCapabilities capability = DesiredCapabilities.firefox(); 
      capability.setPlatform(Platform.WINDOWS); 
      capability.setBrowserName("firefox"); 
      createRemoteWebDriver(capability); 
     } 

     else if(browser.equalsIgnoreCase("internet explorer")) 
     { 
      System.setProperty("webdriver.ie.driver", ".src/Drivers/IEDriverServer.exe"); 
      DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); 
      capability.setPlatform(Platform.WINDOWS); 
      capability.setBrowserName("internet explorer"); 
      capability.setCapability("unexpectedAlertBehaviour", "accept"); 
      capability.setCapability("ignoreProtectedModeSettings", true); 
      capability.setCapability("disable-popup-blocking", true); 
      capability.setCapability("enablePersistentHover", true); 
      capability.setCapability("requireWindowFocus", true); 
      createRemoteWebDriver(capability); 
     } 
     else if(browser.equalsIgnoreCase("MicrosoftEdge")) 
     { 
      System.setProperty("webdriver.edge.driver", ".src/Drivers/MicrosoftWebDriver.exe"); 
      DesiredCapabilities capability = null; 
      capability = DesiredCapabilities.edge(); 
      capability.setPlatform(Platform.WINDOWS); 
      capability.setBrowserName("MicrosoftEdge"); 
      createRemoteWebDriver(capability); 
     } 
    } 

    public static void launchApplication(String TestSuiteName) throws InterruptedException 
    { 
     getDriver().manage().window().setSize(new Dimension(1600, 850)); 
     getDriver().manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); 
     Capabilities capabilities = ((RemoteWebDriver) getDriver()).getCapabilities(); 
     String browserName = capabilities.getBrowserName().toLowerCase(); 
     if(TestSuiteName.equalsIgnoreCase("A1")) 
     { 
      if(browserName.equalsIgnoreCase("chrome")) 
       getDriver().get("http://example.com"); 
      else if(browserName.equalsIgnoreCase("firefox")) 
       getDriver().get("http://example.com"); 
      else if(browserName.equalsIgnoreCase("internet explorer")) 
       getDriver().get("http://example.com"); 
     } 
     else if(TestSuiteName.equalsIgnoreCase("A2")) 
     { 
      if(browserName.equalsIgnoreCase("chrome")) 
       getDriver().get("http://example.com"); 
      else if(browserName.equalsIgnoreCase("firefox")) 
       getDriver().get("http://example.com"); 
      else if(browserName.equalsIgnoreCase("internet explorer")) 
       getDriver().get("http://example.com"); 
     } 
     //pause code execution until canvas element is click-able on the page 
     WebDriverWait wait = new WebDriverWait(getDriver(), 20); 
     //wait.until(ExpectedConditions.titleContains("abc")); 
     wait.until(ExpectedConditions.elementToBeClickable(By.id("frontCanvas"))); 
     Thread.sleep(5000); 
    } 

    public void createRemoteWebDriver(DesiredCapabilities capability) throws MalformedURLException 
    { 
     threadLocal = new ThreadLocal<RemoteWebDriver>(); 
     threadLocal.set(new RemoteWebDriver(new URL(hubMachineIPAddress), capability)); 
    } 

    public static WebDriver getDriver() 
    { 
     return threadLocal.get(); 
    } 

    @AfterTest 
    public void closeBrowser() 
    { 
     getDriver().close(); 
    } 

} 

今、私は私のテストケースのコードを書いているにクラスが以下の通りです:

public class MyTest 
{ 
    private final String fileName = "Example.xlsx"; 
    final static String ButtonPath = "//img[contains(@src,'InsertButton')]"; 
    public final List<LinkedTestCaseData> LinkTestCaseAndDataList; 
    public final HashMap<String, HashMap<String,String>> TestDataMap; 

    public InsertOrifice() throws IOException 
    { 
     WorkBook workbook = new WorkBook(fileName); 
     this.LinkTestCaseAndDataList = workbook.LinkTestCaseAndDataList; 
     this.TestDataMap = workbook.TestDataMap; 
    } 

    @BeforeMethod 
    public void start() throws IOException, InterruptedException 
    { 
     String TestSuiteName = "A1"; 
     Browser.launchWebCAD(TestSuiteName); 
    } 

    @Test 
    public void testCase() throws InterruptedException, IOException 
    { 
     String TestSuiteName = "A1"; 
     Browser.launchWebCAD(TestSuiteName); 
     //further code 
    } 

} 

、問題は、私は文Browser.launchWebCAD(TestSuiteName);return threadLocal.get();中にNullPointerExceptionが直面していますということですgetDriver()関数Browserクラスの関数を静的にする代わりに、私のテストケースでBrowserクラスを拡張すると、エラーは発生しません。しかし、いくつかの特定の条件のために、私はブラウザクラスを拡張しないように言われました。このエラーを修正するには?

答えて

0

問題はテストコードにあります。

@BeforeTestは、<test>タグごとに1回だけ実行されることが保証されています。また、TestNGは、@BeforeTest@BeforeMethod/@Testメソッドが同じスレッドで実行されることを保証するものではありません。あなたの@BeforeMethod

  • してから@AfterMethodあなたはクリーンアップを呼び出し内:

    は、問題を修正するには、ブラウザのインス​​タンスを呼び出してからThreadLocalにブラウザを設定するようにコードを変更する必要がありますブラウザを閉じるためのロジック。
  • beforeInvocation()の中での中にThreadLocalにプッシュし、そのafterInvocation()にクリーンアップロジックを実行する、org.testng.IInvokedMethodListenerを実装するリスナーを構築します。このリスナーは、@Listenersアノテーション(または)を使用してスイートXML(または)の<listeners>タグをサービスローダーアプローチを使用して注入する必要があります。あなたはこの動作を確認する方法についていくつかの情報を探している場合は、リスナーの詳細については、my blog post

を参照し、並列実行をサポートするためにIInvokedListener使用する方法を示しmy blog postThreadLocalを参照してください

関連する問題