I am using Junit 4 and Selenium webdriver in my automation process. I have multiple test cases and each test case requires login functionality.
I want to run all the test cases in the same browser window and maintain the login session instead of opening the new browser for each test case and do login everytime. (In my current scripts, am initiating webdriver in each test case and it opens a new window for each test case and do login every time)
I want to run a test suite, in which i want to run all my test cases in the same browser window. Please provide me a solution. Code :
public class first {
public static WebDriver driver;
@BeforeClass
public static void beforeClass()
{
System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");
System.out.println("Before class");
driver = new ChromeDriver();
}
@Test
public void login()throws Exception
{
driver.get("URL");
WebElement login = driver.findElement(By.xpath("my xpath");
login.findElement(By.id("username")).sendKeys("username");
login.findElement(By.id("password")).sendKeys("pwd");
driver.findElement(By.xpath("my xpath")).click();
}
}
Created second class :
public class second {
public static WebDriver driver;
{
@Test
public void nextstep()throws Exception
{
WebElement buttons = driver.findElement(By.xpath("my xpath"));
buttons.findElement(By.className("Classname")).click();
}
}
Test suite class :
@RunWith(Suite.class)
@SuiteClasses({first.class, second.class})
public class testsuite
{
public static WebDriver driver;
@BeforeClass
public static void setUpClass()
{
System.out.println("Master Setup");
}
}