java - 从 testng.xml 运行测试用例时如何解决 SessionID=null 问题

标签 java selenium scope testng ui-automation

我的问题与此类似- SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? (Selenium)

当我单独运行测试用例时,它运行良好,但是当我从 testng.xml 第二个测试用例开始运行它们时,由于 session id 为空,一切都失败了。我一直在寻找解决方案,并认为问题出在驱动程序的范围上。谁能告诉我解决这个问题的最佳方法是什么?

这就是我的框架的样子-

测试用例

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";
    }

}

可重用操作

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();
    }

}

最佳答案

您的代码中有很多复杂之处,其中最大的一个是您对 ReusableActions#driver 的静态引用。因此,这里发生的情况是,ReusableActions 的所有子类,即 TC001 最终共享 driver 的相同静态数据成员。这会导致您出现竞争状况。

因此,当您并行运行两个或多个 @Test 方法时,它们最终会共享相同的静态 WebDriver 引用。我猜这就是导致问题的原因,因为您的一个 @Test 方法运行得非常快,并且它调用驱动程序对象上的 quit() 方法。现在,当第二个测试方法到达 quit() 调用时,它最终会第二次调用 quit()

请删除整个代码中的静态引用。

我还建议您减少代码中的继承层。它增加了代码的复杂性并使调试变得困难。您可能希望组合优于继承。

看看this我创建的博客文章,其中您可以通过使用组合而不是继承来为 Webdriver 测试实现相同类型的并行执行。

关于java - 从 testng.xml 运行测试用例时如何解决 SessionID=null 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45454258/

相关文章:

java - 如何基于XSD "Auto-Repair"XML?

python - 通过 python selenium chrome 单击按钮

c++ - VC++如何破坏局部静态变量名?

python - 在 Python 中使 vars 在调用方法范围内可见的方法?

.net - .NET 中是否有创建线程绑定(bind)变量的内置功能?

java - 父类(super class)已经实现了哪些实现和接口(interface)?

Java.随机环绕

java - Content-Disposition 设置的文件名在IE6 中被忽略。来自 web.xml 的 URL 显示为文件名

javascript - 如何在谷歌浏览器中从 HTML 运行 python 脚本?

python - 如何从表格行中抓取特定单词?