2017-08-02 56 views
0

我的問題運行測試情況下化解的SessionID = NULL問題類似於這 - SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? (Selenium)如何從的testng.xml

當我單獨運行測試用例運行良好,但是當我從TestNG的運行它們.xml由於會話標識爲空,所有第二個測試用例都會失敗。我一直在尋找解決方案,並認爲問題在於駕駛員的範圍。有誰可以告訴我解決這個問題的最佳方法是什麼?

這是我的框架看起來喜歡 -

的TestCase

package testCase; 

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.NoSuchElementException; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 
import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 
import utility.Constant; 
import utility.OpentapWrappers; 
import utility.Reporter; 

public class TC001 extends OpentapWrappers{ 

    @Test (description="Test") 
    public void main() { 
     WebDriverWait wait=new WebDriverWait(driver, 60); 

     try { 
      wait.until(ExpectedConditions.urlContains(Constant.Plumbing_URL)); 

      /* Validate navigation to Plumbing */ 
      ValidateUrl(Constant.Plumbing_URL); 

     } catch (NoSuchElementException e) { 
      e.printStackTrace(); 
      Reporter.reportStep("NoSuchElementException" , "FAIL"); 
     } 
    } 

    @BeforeClass 
    public void beforeClass(){ 
     browserName="firefox"; 
     testCaseName = "TC001"; 
     testDescription = "Validate Header"; 
    } 

} 

ReusableActions

import java.util.List; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.support.ui.Select; 
import com.relevantcodes.extentreports.ExtentReports; 
import com.relevantcodes.extentreports.ExtentTest; 
import utility.Reporter; 

public class ReusableActions { 

    public static WebDriver driver; 
    public ExtentReports extent; 
    public static ExtentTest test; 

    /* Invoke Browser and enter the URL */ 

    public static void InvokeApp(String browser, String url) { 
     try { 
      if (browser.equalsIgnoreCase("chrome")) { 
       System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe"); 
       driver = new ChromeDriver(); 
      } else { 
       System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); 
       DesiredCapabilities capabilities = new DesiredCapabilities(); 
       capabilities.setCapability("acceptInsecureCerts",true); 
       driver = new FirefoxDriver(capabilities); 
      } 

      //driver.manage().window().maximize(); 
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
      driver.get(url); 
      Reporter.reportStep("The browser :" +browser+ " is launched with URL :" +url+ "successfully","PASS"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      Reporter.reportStep("The browser :" +browser+ " could not be launched with URL :" +url+ "successfully","FAIL"); 
     } 
    } 

    /* Validate URL*/ 

    public static void ValidateUrl(String URL){ 
     try { 
      if (driver.getCurrentUrl().contains(URL)) { 
       Reporter.reportStep("Page is successfully loaded :"+URL, "PASS"); 
      } else { 
       Reporter.reportStep("Page Title :"+driver.getCurrentUrl()+" did not match with :"+URL, "FAIL"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Reporter.reportStep("The URL did not match", "FAIL"); 
     } 
    } 


    /* Quit Browser*/ 

    public void quitBrowser() { 

    try { 

     driver.quit(); 

     } catch (Exception e) { 

     Reporter.reportStep("The browser could not be closed.", "FAIL"); 

     } 

    } 

} 

記者班

public class Reporter extends OpentapWrappers{ 

    private static ExtentTest test; 
    private static ExtentReports extent; 

    public static void reportStep(String desc, String status) { 
     long number = (long) Math.floor(Math.random() * 900000000L) + 10000000L; 
     File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 

     try { 
      FileUtils.copyFile(src, new File("D:\\Reports\\Screenshots\\Scr_"+number+".png")); 
     } catch (WebDriverException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // Write if it is successful or failure or information 
     if (status.toUpperCase().equals("PASS")) { 
      test.log(LogStatus.PASS, desc + test.addScreenCapture(".\\Screenshots\\Scr_"+number+".png")); 

     } else if (status.toUpperCase().equals("FAIL")) { 
      test.log(LogStatus.FAIL, desc + test.addScreenCapture(".\\Screenshots\\Scr_"+number+".png")); 
      throw new RuntimeException("FAILED"); 

     } else if (status.toUpperCase().equals("INFO")) { 
      test.log(LogStatus.INFO, desc); 
     } 
    } 

    public static void startResult() { 
     extent = new ExtentReports("D:\\Reports\\SiteCoreUS.html", false); 
     extent.loadConfig(new File("D:\\extentreports-java-2.41.2\\extent-config.xml")); 
    } 

    public static void startTestCase() { 
     test = extent.startTest(testCaseName, testDescription); 
    } 

    public static void endResult() { 
     extent.endTest(test); 
     extent.flush(); 
    } 

} 

OpenTapWrapper類

package utility; 

public class OpentapWrappers extends ReusableActions { 

    protected static String browserName; 
    protected static String testCaseName; 
    protected static String testDescription; 

    @BeforeSuite 
    public void beforeSuite() throws FileNotFoundException, IOException { 
     Reporter.startResult(); 
    } 

    @BeforeMethod 
    public void beforeMethod() { 
     Reporter.startTestCase(); 
     InvokeApp(browserName,Constant.SiteCoreUSURL); 
    } 

    @AfterSuite 
    public void afterSuite() { 
     Reporter.endResult(); 
    } 

    @AfterMethod 
    public void afterMethod() { 
     quitBrowser(); 
    } 

} 

回答

0

你有很多的併發症在你的代碼,其中最大的一個是你有一個靜態參考ReusableActions#driver。因此,這裏發生的是ReusableActions的所有子類,即TC001最終共享driver的相同靜態數據成員。這對你造成了競爭條件。

因此,當您並行運行兩個或更多個@Test方法時,它們最終會共享相同的靜態WebDriver引用。我想這就是導致這個問題的原因,因爲你的@Test方法之一非常快速地運行,並且它調用驅動程序對象上的quit()方法。現在,當第二種測試方法到達quit()呼叫時,它將第二次結束呼叫quit()

請刪除整個代碼中的靜態引用。

我也建議你修剪你的代碼中的繼承層。它增加了代碼的複雜性並使調試變得困難。您可能希望贊成合成而不是繼承。

看看我創建的this博客文章,其中您可以爲您的webdriver測試實現相同類型的並行執行,但使用組合而不是繼承。

+0

謝謝你克里希南。我昨天晚上偶然發現了你的博客,並一直在提及它。我意識到我有很多要修改的東西。感謝您的幫助 –

+0

如果能幫到您,請您接受我的回答嗎? –

+0

完成。 現在,當我調用invoke方法時,不會執行webdriverlsiterner類的before調用方法。它僅在Invoke方法執行後執行,然後失敗NullPointerException的cz。 –

相關問題