In the end I solved my problem in the following way
package test_suite;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import pageObjects.CreateNewStudyPage;
import pageObjects.LoginPage;
@RunWith(Suite.class)
@SuiteClasses({
LoginPage.class,
CreateNewStudyPage.class,
//OpenAllTabsAndExports.class
})
public class Test_Suite {
public static WebDriver driver;
@BeforeClass
public static void setUpClass() throws MalformedURLException {
System.setProperty("webdriver.chrome.driver", "D:\WebDriver_Selenium/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("http://10.3.8.50");
}
@AfterClass
public static void setDownClass() {
driver.quit();
}
}
First test performed by the application
package pageObjects;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import test_suite.Test_Suite;
public class LoginPage {
@Test
public void testLogin() {
WebDriver driver = Test_Suite.driver;
driver.findElement(By.id("sponsorname")).clear();
driver.findElement(By.id("sponsorname")).sendKeys("Test");
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("admin1");
driver.findElement(By.id("pwd")).clear();
driver.findElement(By.id("pwd")).sendKeys("123");
driver.findElement(By.cssSelector("button.btn.btn-success")).click();
}
}
Then without closing the browser, the test suite continues executing the following test
package pageObjects;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
import test_suite.Test_Suite;
public class CreateNewStudyPage {
private StringBuffer verificationErrors = new StringBuffer();
@Test
public void testCreateNewStudy() throws InterruptedException {
WebDriver driver = Test_Suite.driver;
if(isElementPresent(By.id("userMenu"), driver)) {
driver.findElement(By.xpath("//*[@id='dropdownMenu1']/i/i")).click();
for (int second = 0;; second++) {
if (second >= 30) fail("timeout");
try { if (isElementPresent(By.xpath("//*[@id='userMenu']/div/ul/li[3]/a"), driver)) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.xpath("//*[@id='userMenu']/div/ul/li[3]/a")).click();
}
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.xpath("//input[@value='Add']"), driver)) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.xpath("//input[@value='Add']")).click();
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.id("StudyCode"), driver)) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.id("StudyCode")).clear();
driver.findElement(By.id("StudyCode")).sendKeys("Study_Code_Automation28");
driver.findElement(By.id("GSKBmarcref")).clear();
driver.findElement(By.id("GSKBmarcref")).sendKeys("BMARCREF");
driver.findElement(By.id("StudyShortName")).clear();
driver.findElement(By.id("StudyShortName")).sendKeys("Short Name");
new Select(driver.findElement(By.id("SvtVersionId"))).selectByVisibleText("1");
new Select(driver.findElement(By.id("BudgetTypeId"))).selectByVisibleText("Substudy");
new Select(driver.findElement(By.id("PhaseName"))).selectByVisibleText("I");
new Select(driver.findElement(By.id("PatientTypeId"))).selectByVisibleText("Inpatient");
private boolean isElementPresent(By by, WebDriver driver) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
Here the key is in the first lines of the classes where the test is going to take place, and it is to create the driver assigning the value of the driver of the Test Suite class
WebDriver driver = Test_Suite.driver;
And in this way the same browser is always used for all the tests that you run within the test suite.