org.openqa.selenium.NoSuchSessionException: Session ID is null error

When i am trying to run my selenium tests the first test seem to work correctly but then i am recieving this error for the subsequent tests, Im after switching my tests over to an electron app so not sure if this is after causing a issue :

org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'

My set up page is:

public class SetUp extends TestRail {
private static ChromeDriver browser;
public static String urlHost;
public static String testEnv;
public static PageConfig pageConfig;


@Before
public void initialBrowser() {

    if(browser == null) {

        String projectLocation = System.getProperty("user.dir");
        System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("start-maximized");
        options.addArguments("--disable-gpu");
        options.addArguments("disable-infobars");
        System.setProperty("webdriver.chrome.silentOutput", "true");
        options.setBinary("C:/Users/ElectronApp.exe");

        browser = new ChromeDriver(options);

        //--------------------
        pageConfig = new PageConfig(browser);

    }
}


//method: To get the url configs for the environment used to run the test
//@parameter: dev or test. demo doesn't not have a separate front end url
public void beforeTest(@NotNull final String env) throws Exception {
    testEnv = env;
    System.out.println("TEST ENVIRONMENT:  " + testEnv);
    switch (env){
        case "qa-84" :
            urlHost = "http://ukdc1-docker-mx:84/qa/#/login";
            break;
        case "qa-85":
            urlHost = "http://ukdc1-docker-mx:85/qa/#/login";
            break;
        default:
            throw new Exception("Incorrect environment passed");
    }
}



//method to close the browser after every cucumber scenario

@After
public void closeBrowser(Scenario scenario) throws IOException, APIException {
    if(scenario.isFailed()){
        try{
            TakesScreenshot screenshot = (TakesScreenshot)browser;
            File source = screenshot.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(source, new File("logs/screenshots/" + scenario.getName() + ".png"));
            System.out.println("Screenshot taken");
        } catch (Exception e){
            System.out.println("Exception while taking screenshot " + e.getMessage());
        }
    }
    browser.quit();
    writeResultToTestRail(scenario);
}


public void writeResultToTestRail(Scenario scenario) throws IOException, APIException {
    String tesCaseID = new String();
    for(String s: scenario.getSourceTagNames()){
        if(s.contains("TestRail")){
            int size = s.length();
            int startOfID = s.indexOf('"');
            tesCaseID = s.substring(startOfID + 1,size - 2);
        }
        Map data = new HashMap();
        if(scenario.isFailed()){
            data.put("status_id", 5);
            data.put("comment", "Test Env: " + urlHost + "\n\n" + logError(scenario));
        }else {
            data.put("status_id", 1);
        }
        postTestCaseStatus("add_result_for_case/","3914", tesCaseID, data);
        //3914
    }
}

protected void getTestCaseDetailsInConsole(String testCaseId) throws IOException, APIException {
    System.out.println(getTestCaseDetails(testCaseId));
}

private static String logError(Scenario scenario) {
    try {
        Class klass = ClassUtils.getClass("cucumber.runtime.java.JavaHookDefinition$ScenarioAdaptor" );
        Field fieldScenario = FieldUtils.getField(klass, "scenario", true);

        if (fieldScenario != null) {
            fieldScenario.setAccessible(true);
            Object objectScenario = fieldScenario.get(scenario);

            Field fieldStepResults = objectScenario.getClass().getDeclaredField("stepResults" );
            fieldStepResults.setAccessible(true);

            ArrayList<Result> results = (ArrayList<Result>) fieldStepResults.get(objectScenario);
            for (Result result : results) {
                if (result.getError() != null) {
                    System.out.println(result.getError() + "\n\n" + result.getErrorMessage());
                    if(result.getErrorMessage().length() > 3100) {
                        return result.getErrorMessage().substring(0, 3100);
                    } else {
                        return result.getErrorMessage();
                    }
                }
            }
        }
        return "Fetching error logs from the scenario ran into some issue, please check jenkins logs";
    } catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
        return e.getMessage();
    }
}

}

And My Page config:

public class PageConfig {
public ChromeDriver browser;

public PageConfig(ChromeDriver browser){
    this.browser = browser;
}

//method: Get instance method can be called on any class, it helps to avoid to add a line every time a new PO is added to the project.
//@parameter: ClassName.class
//@example:  PageConfig.GetInstance(LoginPage.class).onLoginPage(); where onLoginPage is an method of 
LoginPage.
public <TPage extends BasePage> TPage GetInstance (Class<TPage> pageClass){
    try {
        return initElements(browser, pageClass);
    }catch(Exception e){
        e.printStackTrace();
        throw e;
    }
}

}



Read more here: https://stackoverflow.com/questions/67384119/org-openqa-selenium-nosuchsessionexception-session-id-is-null-error

Content Attribution

This content was originally published by user9686029 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: