0

I have a number of Selenium/Java tests using testng, that start a new browser for each test, which is exactly what I need. However I now need to run a number of these single tests using the same browser.

Is this possible?

Or would this actually be one test doing all the actions of a number of tests, i.e. combine tests 1, 2 and 3 into one test?

Here is a example of the beginning of one of my tests.

@Test
static void Test_0001() throws Exception
{
    LogFile.logfile("--------------------- Start Log ---------------------");
    WebDriver driver = new ChromeDriver();
    driver.get("https://localhost:44300");
    driver.manage().window().maximize();
    driver.findElement(By.id("details-button")).click();
    driver.findElement(By.id("proceed-link")).click();
    Thread.sleep(6000);
    driver.findElement(By.name("username")).sendKeys("User");

Thanks.

Ian Thompson
  • 187
  • 1
  • 11

1 Answers1

0

Ideally you should write each test so that it can run independently. If you're merely trying to save time from browser instantiation, this is understandable, but not best practice.

You can take a look at this answer but essentially you can use @BeforeClass to create the driver object for all tests to use:

@BeforeClass
public static void setUp() {
    driver = new ChromeDriver();
}
DMart
  • 2,401
  • 1
  • 14
  • 19
  • Thanks for the answer. I totally agree, tests should be individual and that's what I have, but I now have a requirement to do a number of tests on the same browser session to ensure they all run, so really this is one test, but the information provided is very useful. – Ian Thompson Jan 07 '21 at 15:54