AutomationTest
AutomationTest

Reputation: 109

TestNG Parallel Testing, Opens two browser but runs all tests only in 1 browser

Hi All I have an XML class

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
    <test name="Auto">
        <classes>
            <class name="Auto.....Name_of_class1......"/>
        </classes>
    </test>

    <test name="Bad">
        <classes>
            <class name="Bad.....Name_of_class2......"/>
        </classes>
    </test>
     </suit>

When I run this XML file, first it opens two Browsers ( Chrome ) then run both classes. But here is the confusing part for me

When the browsers are open, out of these two browsers, only one browser will test cases. In my cases, I am running two classes parallel and each class has 2-3 test cases. but all of the test cases will run in one browser. 2nd browser just open and goes to the bases URL

Also, I have tried changing parallel to "class", "True" but no success. Results set are also very messy, Please take look at the screenshot

enter image description here

Both classes calls :-

public static WebDriver initDriver() {
    
            if (driver == null) {
    
                switch (prop.getProperty("browser")) {
    
                case "chrome":
                    WebDriverManager.chromedriver().setup();
                    driver = new ChromeDriver();
                    driver.manage().deleteAllCookies();
                    break;
    
                case "firefox":    
                    driver = new FirefoxDriver(fOptions);
  
                    break;
    
                default:
                        System.out.println("Wrong driver was chosen!");
    
                }
    
                action = new Actions(driver);
                driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                driver.manage().window().maximize();
            }
            return driver;
    
        }

Upvotes: 0

Views: 3574

Answers (1)

SiKing
SiKing

Reputation: 10329

This is purely a Java problem and has very little to do with (or ).

A static field belongs to the class (and not the instance of a class) and so it exists only once no matter how many times you call that class. You can read more about this here and here and many other sources.

As it is, every time your code executes driver = new ChromeDriver() (or driver = new FirefoxDriver()) it launches a new browser, which you observed, and overwrites the field driver with the new value.

In order to run your tests in parallel, you must make the driver not static. You will also have to adjust your code to access this field in a non-static way.

Upvotes: 2

Related Questions