2017-08-02 54 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。 –

相关问题