gpt4 book ai didi

java - 获取异常 : selenium. NoSuchSessionException : Session ID is null. 这里我使用 Localthread 进行并行执行

转载 作者:行者123 更新时间:2023-12-02 12:06:37 24 4
gpt4 key购买 nike

**出现以下异常:org.openqa.selenium.NoSuchSessionException: session ID 为空。调用 quit() 后使用 WebDriver?在这里,我使用 LocalThread 进行并行测试执行,但是在一个线程中的类中运行多个测试时(不是并行执行)第一个测试用例成功运行,第二个和第三个测试用例失败**

Console Output:

[TestNG] Running:
C:\Users\Afsar\AppData\Local\Temp\testng-eclipse--442788521\testng-customsuite.xml

****** In Before Method init method *********
Driver instanceis created and set current local thread
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 41879
Only local connections are allowed.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Oct 22, 2017 5:03:12 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Set current thread method is called ..
Get curent thread method is called ..
Current thread id :1 thread name is :main
Get curent thread method is called ..
Current thread id :1 thread name is :main
Get curent thread method is called ..
Current thread id :1 thread name is :main
*********Running first Test Case *********
Get curent thread method is called ..
Current thread id :1 thread name is :main
Get curent thread method is called ..
Current thread id :1 thread name is :main
**** Executing Logine Action *********
URL is sent succesfully..
Clicked on the Sign -on button
User name Entered
Password entered
Sign -in clicked
****** Executing Register Action *********
Get curent thread method is called ..
Current thread id :1 thread name is :main
Register button is found
PASSED : First test case : Login Successful..
****** In After Method tear down *********
Removing driver instance from current thread and closing all sesions
driver.quite called
****** In Before Method init method *********
Driver instanceis created and set current local thread
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 47720
Only local connections are allowed.
Oct 22, 2017 5:03:39 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Set current thread method is called ..
Get curent thread method is called ..
Current thread id :1 thread name is :main
Get curent thread method is called ..
Current thread id :1 thread name is :main
Get curent thread method is called ..
Current thread id :1 thread name is :main
*********Running second Test Case *********
**** Executing Logine Action *********
URL is sent succesfully..
****** In After Method tear down *********
Removing driver instance from current thread and closing all sesions
driver.quite called
****** In Before Method init method *********
Driver instanceis created and set current local thread
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 25049
Only local connections are allowed.
Oct 22, 2017 5:03:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Set current thread method is called ..
Get curent thread method is called ..
Current thread id :1 thread name is :main
Get curent thread method is called ..
Current thread id :1 thread name is :main
Get curent thread method is called ..
Current thread id :1 thread name is :main
*********Running third Test Case *********
**** Executing Logine Action *********
URL is sent succesfully..
****** In After Method tear down *********
Removing driver instance from current thread and closing all sesions
driver.quite called
Current thread removed and clearcurrentDriver() called.. ..
PASSED: LoginFunctionality
This is first test same as first
FAILED: LoginFunctionality1
This is second test same as first
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z'
System info: host: 'AFSAR-PC', ip: '192.168.0.100', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:410)
at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy9.click(Unknown Source)
at com.afsar.framework.pageobjects.MercuryToursHomePage.clickRegister(MercuryToursHomePage.java:58)
at com.afsar.framework.actions.MercuryHomePage_Action.Login_Action(MercuryHomePage_Action.java:13)
at com.afsar.framework.tests.LoginTest.LoginFunctionality1(LoginTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

FAILED: LoginFunctionality2
This is third test same as first
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z'
System info: host: 'AFSAR-PC', ip: '192.168.0.100', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:410)
at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy9.click(Unknown Source)
at com.afsar.framework.pageobjects.MercuryToursHomePage.clickRegister(MercuryToursHomePage.java:58)
at com.afsar.framework.actions.MercuryHomePage_Action.Login_Action(MercuryHomePage_Action.java:13)
at com.afsar.framework.tests.LoginTest.LoginFunctionality2(LoginTest.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)


===============================================
Default test
Tests run: 3, Failures: 2, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 2, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@49e4cb85: 407 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter@5594a1b5: 75 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@6e8dacdf: 19 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@1134affc: 373 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 27 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2f7c7260: 118 ms

Login Test :

public class LoginTest{

private WebDriver driver;
@BeforeMethod
@Parameters("BROWSER")
public void init(@Optional("Chrome")String BROWSER) throws Exception{
System.out.println("****** In Before Method init method *********");
System.out.println("Driver instanceis created and set current local thread");
//Opening desired Browser driver instance
driver=DriverFactory.OpenBrowser(BROWSER);
//Setting Current driver with threadLocal
Driver.setCurrentDriver(driver);
Driver.getCurrentDriver().manage().window().maximize();
Driver.getCurrentDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//This is Baseclass constructor (Initializaton)
new BaseClass(Driver.getCurrentDriver());
}

@Test(description="This is first test same as first ")
public void LoginFunctionality() throws Exception{

System.out.println("*********Running first Test Case *********");
MercuryHomePage_Action.Login_Action();
MercuryHomePage_Action.Register_Action();

System.out.println("PASSED : First test case : Login Successful..");

}

@Test(description="This is second test same as first ")
public void LoginFunctionality1() throws Exception{

System.out.println("*********Running second Test Case *********");
MercuryHomePage_Action.Login_Action();
MercuryHomePage_Action.Register_Action();

System.out.println("PASSED : second test case : Login Successful..");

}

@Test(description="This is third test same as first ")
public void LoginFunctionality2() throws Exception{

System.out.println("*********Running third Test Case *********");
MercuryHomePage_Action.Login_Action();
MercuryHomePage_Action.Register_Action();

System.out.println("PASSED : third test case : Login Successful..");
}

@AfterMethod
public void teardown(){

System.out.println("****** In After Method tear down *********");
System.out.println("Removing driver instance from current thread and closing all sesions");
//Driver.getCurrentDriver().quit();
Driver.QuitDriver();
}

@AfterTest
public void aftertest(){
Driver.clearCurrentDriver();
}
}

Driver Factory Class:
public class DriverFactory {

//private static WebDriver driver;

//@Parameters("BROWSER")
public static WebDriver OpenBrowser(String BROWSER) throws Exception{
WebDriver driver=null;
try{
if(BROWSER.equals("Mozilla")){
driver = new FirefoxDriver();
Log.info("New driver instantiated");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Log.info("Implicit wait applied on the driver for 10 seconds");
//driver.get(Constant.URL);
Log.info("Web application launched successfully");
}else if(BROWSER.equalsIgnoreCase("Chrome")){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Afsar\\git\\HybridFramework_POM\\HybridFramework\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();
Log.info("New driver instantiated");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Log.info("Implicit wait applied on the driver for 10 seconds");
//driver.get(Constant.URL);
Log.info("Web application launched successfully");
}
}catch (Exception e){
Log.error("Class Utils | Method OpenBrowser | Exception desc : "+e.getMessage());
}
return driver;
}

Driver class:

private static ThreadLocal<WebDriver> CURRENT_DRIVER = new ThreadLocal<>();

public static WebDriver getCurrentDriver() {
System.out.println("Get curent thread method is called ..");
Long callingThreadID = new Long(Thread.currentThread().getId());
System.out.println("Current thread id :"+callingThreadID +" thread name is :"+Thread.currentThread().getName());
return CURRENT_DRIVER.get();
}

public static void setCurrentDriver(WebDriver driver) {
System.out.println("Set current thread method is called ..");
CURRENT_DRIVER.set(driver);
}

public static void clearCurrentDriver() {

System.out.println("Current thread removed and clearcurrentDriver() called.. ..");
if (CURRENT_DRIVER.get() != null) {
CURRENT_DRIVER.get().quit();
CURRENT_DRIVER.remove();
}

CURRENT_DRIVER.remove();
}

public static void QuitDriver() {

System.out.println("driver.quite called ");
if (CURRENT_DRIVER.get() != null) {
CURRENT_DRIVER.get().quit();
//CURRENT_DRIVER.remove();
}

//CURRENT_DRIVER.remove();
}

*请在这种情况下帮助我,您可以在 GitHub 存储库中找到该项目: https://github.com/afsarali273/HybridFramework_POM *

最佳答案

问题是,在运行第一个测试时会调用 driver.quit() ,因为在 After 方法注释中, driver.quit() 在第一个测试用例之后被调用。这样,第二个和第三个测试用例的驱动程序为空。使用下面的代码。它会工作得很好:)

@AfterMethod
public void teardown(){
System.out.println("****** In After Method tear down *********");
System.out.println("Removing driver instance from current thread and closing all sesions");
//Driver.getCurrentDriver().quit();
}

@AfterTest
public void aftertest(){
Driver.QuitDriver();
}

关于java - 获取异常 : selenium. NoSuchSessionException : Session ID is null. 这里我使用 Localthread<webdriver> 进行并行执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46873803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com