1
    package automation;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Login {
    public String baseURL="https://learn.stagingaia.com/login";
    public WebDriver driver;
    @BeforeTest
        public void setBaseUrl(){
        System.setProperty("webdriver.chrome.driver", "D://Selenium//chromedriver_win32//chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(baseURL);
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
    }
    @Test(priority=1)
    public void titleverification() {
        String expectedtitle="Acadify";
        String actualtitle=driver.getTitle();
        Assert.assertEquals(actualtitle,expectedtitle);
    }
    @Test(priority=2)
    public void ValidCrdentials() throws InterruptedException {
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div/div/form/div[1]/input")).sendKeys("[email protected]");
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div/div/form/div[2]/input")).sendKeys("Password@123");
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div/div/form/div[3]/button")).click();
        Thread.sleep(5000);
        String ExpectedHeading="Dashboard";
        String actulHeading=driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[2]/div/div/div/h2/span")).getText();
        Assert.assertEquals(actulHeading, ExpectedHeading);
    }
    @Test(priority=3)
    public void InvalidEmail() throws InterruptedException {
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div/div/form/div[1]/input")).sendKeys("[email protected]");
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div/div/form/div[2]/input")).sendKeys("Password@123");
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div/div/form/div[3]/button")).click();
        Thread.sleep(5000);
        String ExpectedEmailError="Dashboard";
        String ActuaEmailError=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/div/div[1]/div[2]")).getText();
        Assert.assertEquals(ActuaEmailError, ExpectedEmailError);
    }
    @AfterTest
    public void EndSession() {
        driver.close();
    }
    
}

I am using this code but it is showing that result.

PASSED: EndSession
PASSED: ValidCrdentials
PASSED: titleverification
FAILED: InvalidEmail
org.openqa.selenium.NoSuchSessionException: **invalid session id**
Build info: version: '4.7.2', revision: '4d4020c3b7'
System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '18.0.2'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [4002147c7121449294c404b500c276b5, findElement {using=xpath, value=/html/body/div[1]/div[2]/div/div[2]/div/div/form/div[1]/input}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 109.0.5414.120, chrome: {chromedriverVersion: 109.0.5414.74 (e7c5703604da..., userDataDir: C:\Users\MUHAMM~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:57842}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:57842/devtoo..., se:cdpVersion: 109.0.5414.120, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 4002147c7121449294c404b500c276b5
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:535)
    at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162)
    at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:60)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:351)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:343)
    at automation.Login.InvalidEmail(Login.java:45)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:794)
    at org.testng.TestRunner.run(TestRunner.java:596)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
    at org.testng.SuiteRunner.run(SuiteRunner.java:276)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Default test
    Tests run: 4, Failures: 1, Skips: 0
    Configuration Failures: 1, Skips: 0
    
1
  • 1
    Welcome to the community. Good to ask question in the community channel. But can you pls add what all you have tried and system and components details will help ppl to help you quickly to resolve your error/question Feb 6, 2023 at 13:03

1 Answer 1

0

There are two methods available in the WebDriver interface quit() and close().

  • driver.quit(): The quit() method quits the driver, ends the session and closes all associated browser windows opened during a test whether the test fails or passes.
  • driver.close(): The close() method closes the currently focused window, quitting the driver if the current window is the only open window. If there are no windows open, it will error out.

In quit() if you try to execute any statement, sessionID=null will be sent and you will get exception - Session ID is null. Using WebDriver after calling quit()?

In close(), random sessionID(only one) will be created and maintained for entire session. After execution of close(), session will be invalid or expired.

In short quit() the session is null and in close() session id is invalid.

To fix your error, try this code.

@AfterTest
public void EndSession() {
    if (driver == null) {
        driver.quit();
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.