0

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

4
  • I am sorry but I am not able understand completely that. Could you please modify my code if possible and explain according to that. It would be very helpful
    – Som Ghosh
    Commented Sep 30, 2014 at 7:28
  • 1
    Please read the question, answers and linked code. This is not a coding service.
    – l0b0
    Commented Sep 30, 2014 at 7:31
  • I think (even before you read the answer below) if you read the answer linked and compare it to your code you'll find some similarities. Even if you don't understand it completely, it should be enough for you to explore your way to the solution. l0b0's comment, while a bit blunt and not entirely true in all cases (we modify code from time to time, and occasionally craft it from scratch) is true to heart in the sense that your answer does await you in that other question. And, apparently, in this one too now.
    – corsiKa
    Commented Sep 30, 2014 at 21:28
  • I tried some codes but this one helped me to understand the error in my code [link] (stackoverflow.com/questions/26114562/…)
    – Som Ghosh
    Commented Oct 1, 2014 at 5:55

1 Answer 1

4

You mean to say in one class multiple methods you want to run, then use below code and try.

    @BeforeTest
    public static void beforeMethod() {
    driver= new FirefoxDriver();
    }
    @Test
    public void main1() {
    driver.get("http://indianrail.gov.in/pnr_Enq.html");
    driver.findElement(By.id("element")).sendKeys("1234567890");
    String captcha = driver.findElement(By.id("txtCaptchaDiv")).getText();
    System.out.println("Captcha is: " +captcha);
    driver.findElement(By.id("txtInput")).sendKeys(captcha);
    }
    @Test
    public void main2() {
    driver.findElement(By.className("btn_style")).click();
    }
    @Test
    public void main3() {
    System.out.println("The title is: " +driver.getTitle());
    }
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.