3

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");
}

}
user2376425
  • 85
  • 1
  • 3
  • 12

2 Answers2

0

You need to implement a Suite Setup method that opens a new browser window and logs in. This way the method will be called once before the execution of all the tests.

To designate a method as a Suite Setup method, put it in the Suite Class, make it static and annotate it with @BeforeClass (see example in this answer).

In the code you posted, the driver variable in the first class and the driver variable in the second class are not the same variable. That's why initializing it in the first class does make the driver in the second class initialized and you get a NullPointerException.
If you want to use the same variable in both classes you need to define a base class which has a non-static variable driver (why did you make it static in the first place?), and then derive both classed from the base class.

Community
  • 1
  • 1
Joe
  • 2,994
  • 5
  • 31
  • 34
  • Well, i get "Null.PointerException" – user2376425 Jul 29 '13 at 05:47
  • Sorry to hear that, but I can't help you without more details. Could you post the code that generates this exception? – Joe Jul 29 '13 at 07:45
  • I see you edited your question to add the code. Which line throws the `NullPointerException`? – Joe Jul 29 '13 at 11:49
  • In second class, test method : " WebElement buttons = driver.findElement(By.xpath("my xpath"));" – user2376425 Jul 31 '13 at 04:20
  • Thanks for reply, as am new to this i don have much idea about it. I created baseclass and extended in both class (first, second) and now when i run my testsuite. I get exception of "No runnable methods" in baseclass – user2376425 Jul 31 '13 at 11:15
  • Sorry to hear that, but I think we are straying too far from the original question - why don't you try posting this problem to a new question? – Joe Jul 31 '13 at 14:55
-1
driver.get("URL");
WebElement login = driver.findElement(By.xpath("my xpath");

this code you have to put in @Before method instead of @Test so same session will continue

Lokesh
  • 7,810
  • 6
  • 48
  • 78