Home > Enterprise >  Unable to run parallel tests via testNG
Unable to run parallel tests via testNG

Time:07-05

Using a POM model approach here. When trying to run tests in parallel, although 3 instances of chrome are launched the test is carried out in only one. The rest don't run at all with URL shown as data; . Can anyone help me out here?

**Test Code**
    
public class Tests {
    private WebDriver driver = null;
    private static String OptionalProductQuantity = "6";
    private static String ChatMessage = "Hi";
    ChromeOptions options = new ChromeOptions().setBinary("C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe");



    @BeforeTest
    public void oneTimeSetUp() {
        
        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");


    }

    @BeforeMethod
    public void setUp() {
        driver = new ChromeDriver(options);
        

    }

    @Test(description = "Adding product to the cart")
    public void AddtoCart() {


        Commons common = new Commons(driver);
        common.navigateTo(Constants.TRUCKX_URL);
        common.clickElement(AddtoCart.Checkout);
        
    }
    @Test(description = "Validating the homepage's UI is fine ")
    public void ValidateHomepageUI() {

        Commons common = new Commons(driver);
        common.navigateTo(Constants.TRUCKX_URL);
         
    }  
    @AfterMethod
    public void exitBrowser() {
        driver.quit();
    }    
}

TestNG XMML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" >
    <test name="Test" >
        <classes>
            <class name="Tests_UI.Tests" />
        </classes>
    </test>
</suite>

CodePudding user response:

I think you missing thread count parameter.

enter image description here

Details are here :

https://www.tutorialspoint.com/what-exactly-does-a-thread-count-do-in-testng#:~:text=Thread count is basically a,be run for this execution.

CodePudding user response:

You are using the same Driver object for all your 3 cases. Even though there are 3 threads which would be spawned by TestNG, the driver object is not per thread, leading to the behavior you see. You need to use ThreadLocal objects in your beforeMethod or listeners to provide one instance of webdriver per TestNG thread.

  • Related