1

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

}

1
  • Can you provide a full stack trace to see in which line you are getting this error? Commented May 4, 2021 at 11:54

1 Answer 1

0

the error shows that your driver close before the execution of the code, so the code of @After is directly executed so what I think is that you forget to use @Test in your set up page because there is @Before and @After but I don't see @Test.

Sign up to request clarification or add additional context in comments.

3 Comments

Im using the Cucumber before and after so i dont need @test, I have to use an old chromedriver version, would I need to downgrade my selenium version to match that?
normally the version of your chromedriver should much your browser version to work correctly
and for your code could you at least change the position of your @After method for exemple put it down

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.