newcoder1010 Posted September 26, 2022 Share Posted September 26, 2022 Hello, First Test: public class FirstTest extends GridSessionBase { @Test public void test1() { System.out.println("First test"); printSessionThreadID(); } } Second Test: public class SecondTest extends GridSessionBase{ @Test public void test1() { System.out.println("Second test"); printSessionThreadID(); } } Base: public class GridSessionBase{ WebDriver driver; // invoked once before executing each @Test method @BeforeMethod public void beforeMethod(Method method) { System.setProperty("webdriver.chrome.driver", "Automation_Tools\\ChromeDriver\\chromedriver.exe"); driver = new ChromeDriver(); System.out.println("beforeMethod"); SessionId s = ((RemoteWebDriver) driver).getSessionId(); System.out.println("Before method session ID: " + s); long id = Thread.currentThread().getId(); System.out.println("Before method thread ID: " + id); } // all the tests are calling this method. public synchronized void printSessionThreadID() { System.out.println("printSessionThreadID"); SessionId s = ((RemoteWebDriver) driver).getSessionId(); System.out.println("Session ID: " + s); long id = Thread.currentThread().getId(); System.out.println("Thread ID: " + id); // if session id and thread id match // if() { // // print class name and test method name of the caller // } } } XML file to run the tests in parallel: Below I have two <test> tags. It means all the TestNG methods listed in each <test> tag will run in the same thread. Test1 will run in one thread and Test2 will run in another thread. Both <test> tags will run in parallel. <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Access Regression Suite" parallel="tests"> <test name="Test1"> <classes> <class name="GridSessionJava.FirstTest"/> </classes> </test> <test name="Test2"> <classes> <class name="GridSessionJava.FirstTest" /> <class name="GridSessionJava.SecondTest" /> </classes> </test> </suite> When I run xml file, here is the output: Quote beforeMethod beforeMethod Before method session ID: 9b993ec9e1e67b2dd9775d398ed974fb Before method session ID: 7261a9b1dbadaf149f72b38c4355375e Before method thread ID: 18 Before method thread ID: 19 First test printSessionThreadID Session ID: 9b993ec9e1e67b2dd9775d398ed974fb Thread ID: 18 First test printSessionThreadID Session ID: 7261a9b1dbadaf149f72b38c4355375e Thread ID: 19 Starting ChromeDriver 105.0.5195.52 (412c95e518836d8a7d97250d62b29c2ae6a26a85-refs/branch-heads/5195@{#853}) on port 59998 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully. Sep 26, 2022 9:54:19 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected upstream dialect: W3C Sep 26, 2022 9:54:19 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch WARNING: Unable to find an exact match for CDP version 105, so returning the closest version found: 104 Sep 26, 2022 9:54:19 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch INFO: Found CDP implementation for version 105 of 104 beforeMethod Before method session ID: 6ca9632512259997c7c59c02509892fb Before method thread ID: 19 Second test printSessionThreadID Session ID: 6ca9632512259997c7c59c02509892fb Thread ID: 19 printSessionThreadID() is being called three times. In printSessionThreadID() method, I like to know which test is calling this method. How do I write if statement in printSessionThreadID() method to check if session ID and thread ID match(or just matching thread ID) and print a message afterward? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now