0

I'm trying to run multiple test cases that are placed in different classes, but when I run the code, the last class creates a new instance of chrome driver. This code is related to company's work hence I can't share the full code in detail but I'll try sharing as much as I can to help you understand better.

Here's the code:

Main Class:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;

public class Main1 {
    
    public static WebDriver driver;
        public static WebDriverWait w;
    
    @BeforeClass
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Person\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

}

Second Class:

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;

public class Second extends Main{
    
    @BeforeClass
    public void openWebsite() {
        driver.get("http://www.example.com");
        System.out.println("Verfying Title...");
        String expTitle = "Company Title";
        String actualTitle = driver.getTitle();
        if (expTitle.equals(actualTitle)) {
            System.out.println("Title Verified");
        } else {
            System.out.println("Title Not Verified");
        }
    }
    
    @Test(priority=1)
    public void companyCode() {
        
        w = new WebDriverWait(driver, Duration.ofSeconds(30));

        //Dropdown to select company

    }
    
    @Test(priority=2)
    public void reload() {
        
        w = new WebDriverWait(driver, Duration.ofSeconds(2));
        
        
        //Click another tab on the application and click reload
    
    @Test(priority=3)
    public void orgtree() {

        By by = By.xpath("//span[normalize-space()='node']//preceding-sibling::div[2]");
        retryingFindClick(driver, by);  //Click Expand node

        //clicking on elements on the website
        
    }
    
    public boolean retryingFindClick(WebDriver driver, By by) {
        boolean result = false;
        int attempts = 0;
        while(attempts < 5) {
            try {
                driver.findElement(by).click();
                result = true;
                break;
            } catch(Exception e) {
            }
            attempts++;
        }
        return result;
    }

}

Last class:

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;

public class Last extends Main {
    
    @Test
    public void Window1() {
        
        w = new WebDriverWait(driver, Duration.ofSeconds(2));
        
        //Click on a window on the website
        
        
    }

}

XML file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


<suite name="TestSuite1">
  <test thread-count="5" name="Test1">
    
    <classes>
        <class name="test.Main"/>
        <class name="test.Second"/>
        <class name="test.Last"/>
    </classes>
    
    
  </test> <!-- Test1 -->
</suite> <!-- TestSuite1 -->

Now the problem is, until the Second Class, the execution is taking place in the single instance of browser, but when it comes to the execution of Last Class, a new instance of browser is created and then it starts from the beginning. I want all the test cases of any class I add in xml file to run in one single instance.

There were many other methods I tried before this but I got an error every time such as "cannot instantiate Second class" or invocation error etc. This is the best method I tried and I'm not getting any error but I don't know why the Last Class is creating a new instance when it is suppose to run right after the execution of Second Class as directed in xml file

1 Answers1

0

In main class try @BeforeTest instead of @BeforeClass

also use, preserver-order = true in testng.xml to run classes in order.

murali selenium
  • 3,847
  • 2
  • 11
  • 20