2017-08-02 5 views
0

私の問題からテストケースを実行しているときのSessionID = nullの問題を解決するためにどのように私はそれが正常に動作個別にテストケースを実行すると SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? (Selenium)testng.xml

をthis-に似ていますが、 .xml 2番目のテストケース以降、すべてがセッションIDがnullであるために失敗します。私は解決策を探していて、問題はドライバのスコープにあると考えました。誰もがこれを解決する最良の方法は何か教えていただけますか?

これは私のフレームワークは

の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"); 

     } 

    } 

} 

Reporterのクラス

like-に見えるものです
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という同じ静的データメンバーを共有することになります。これは競合状態を引き起こしています。

したがって、2つ以上の@Testメソッドを同時に実行すると、同じ静的WebDriver参照を共有してしまいます。私はあなたの@Testメソッドの1つが非常に高速で実行され、ドライバオブジェクトのquit()メソッドを呼び出すため、問題の原因となっていると思います。次に、第2のテスト方法がquit()コールに達すると、quit()を2回目に呼び出すことになります。

コード全体で静的参照を削除してください。

コード内にある継承のレイヤーをトリムすることもお勧めします。コードに複雑さが増し、デバッグが困難になります。あなたは相続よりも組成を好むかもしれません。

私が作成したブログ記事thisを見てください。そこでは、あなたのWebdriverテストと同じ種類の並列実行を実現できますが、継承ではなくコンポジションを使用します。

+0

ありがとうございますKrishnan。私はあなたのブログを昨晩偶然見つけ、それを参照してきました。私は変更することがたくさんあることを実感します。助けてくれてありがとう –

+0

助けてくれたら、私の答えを受け入れることができますか? –

+0

完了。 私の呼び出しメソッドが呼び出されると、webdriverlsiternerクラスの呼び出し前メソッドは実行されません。 Invokeメソッドが実行された後にのみ実行され、その後NullPointerExceptionのczに失敗します。 –

関連する問題