1

I am using selenium and testNG framework for my project. Now what is happening is each class is opening up a browser and then run its methods, eg, if I have five classes, then five browsers will open simultaneously and then run the tests. I want to Open Browser at the start once and run all the methods and then close it.

public class openSite { public static WebDriver driver; @test public void openMain() { System.setProperty("webdriver.chrome.driver","E:/drive/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://vtu.ac.in/"); } @test //Clicking on the first link on the page public void aboutVTU() { driver.findElement(By.id("menu-item-323")).click(); } @Test //clicking on the 2nd link in the page public void Institutes() { driver.findElement(By.id("menu-item-325")).click(); } 

Now What I want is the testNG should open browser once and open vtu.ac.in once and then execute the methods aboutVTU and Institutes and give me the result

3
  • remove driver instantiating again in openMain() method.You have already declared it as static. Use @BeforeClass method to initiate webdriver. remaining else in @Test. Go through this testng.org/doc/documentation-main.html#annotations Commented Sep 30, 2014 at 9:13
  • I tried that and I am getting nullpointer exception Commented Sep 30, 2014 at 9:40
  • remove webdriver from this line ` WebDriver driver = new ChromeDriver();` that is the line causing null pointer exception Commented Oct 1, 2014 at 4:48

1 Answer 1

1

You already declared the type for driver in your field declarations. Redeclaring it in openMain() is your problem. It should look like this.

import static org.testng.Assert.assertNotNull; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class OpenSite { private WebDriver driver; @BeforeClass(alwaysRun=true) public void openMain() { System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver"); driver = new ChromeDriver(); driver.get("http://vtu.ac.in/"); } @Test //Clicking on the first link on the page public void aboutVTU() { assertNotNull(driver); driver.findElement(By.id("menu-item-323")).click(); } @Test(dependsOnMethods="aboutVTU") //clicking on the 2nd link in the page public void Institutes() { assertNotNull(driver); driver.findElement(By.id("menu-item-325")).click(); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.