RE: How to make the opening of browser(all test in one browser) static in testng for selenium

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
public void aboutVTU()
{

driver.findElement(By.id("menu-item-323")).click();

}
@test
public void Institutes()
{

driver.findElement(By.id("menu-item-325")).click();

}

som Train Asked on September 30, 2014 in Selenium WebDriver.

Sorry som, could you please elaborate your question bit more?. (Specially this:  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).

on September 30, 2014.

If your query is resolved then please accept the answer.

on October 1, 2014.
Add Comment
2 Answers
Best answer

hi sachin

I ran the code but it gave me null pointer exception
main1() ran but main2() failed

Could you please look into it

RE: How to make the opening of browser(all test in one browser) static in testng for selenium

Train Answered on September 30, 2014.

Som,

It was working fine in my machine. See below.

RE: How to make the opening of browser(all test in one browser) static in testng for selenium

Code:

 

import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
public class MultipleMethods{
 static WebDriver driver;
@BeforeTest
 public static void beforeMethod() {
 System.setProperty("webdriver.chrome.driver","D:/Selenium/Drivers/chromedriver.exe");
 driver = new ChromeDriver();
 }
@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());
 }
}

 

on September 30, 2014.

You didn’t use my code completely see below and recognize what all the mistake you have done?.

Below image from your screenshot only.

RE: How to make the opening of browser(all test in one browser) static in testng for selenium

on September 30, 2014.

Beautifully explained. Thanks Sachin.

on October 2, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.