2
votes

is there a way to use the same webdriver object for multiple tests/classes when run within a testng suite? What I mean is to start the WebDriver instance with one class, but continue using it over subsequent classes/tests in the suite?

The problem I am facing is that 10 differect classes have this statement:

WebDriver driver = new FirefoxDriver();

and it open 10 different instances even after I restrict TestNG with parallel mode set a s false. It really sucks up my system memory.

I wat a solution to use same WebDriver and run different classes over without having to multiple browser and Webdriver insntances.

7

7 Answers

3
votes

Put all your classes in a <test> tag and initialize the driver in a @BeforeTest method which you store in the base class so that all your test methods can access it.

0
votes

I'm not sure what it is in java but in c# you just instantiate the webdriver as a field

private WebDriver driver = new FirefoxDriver();

then just reference driver from that point on out

driver.findElements(By.TagName('h1'));
0
votes

ISFW provides nice way to handle it. It will creates new instance if required otherwise utilize the same one. However you don't require to write code for setup and teardown. You need to set browser string in property file and it will manage for RC or webdriver for given browser string.

0
votes

I have achieved this as following:

1: created a startup class

public class Start {
    public static WebDriver wd;

    @BeforeSuite
    public void Setup() {
    System.setProperty "webdriver.chrome.driver","D:\\chromedriver.exe");
    wd = new ChromeDriver();
    wd.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    wd.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    wd.manage().window().maximize();
    wd.get("URL");}
    }

2: reference the static driver instance in other classes by its class qualifier as

public void VerifyTitle() {
    Assert.assertEquals(Start.wd.getTitle(), "Title", "Incorrect Title");}

3: add the class and the setup method in testing.xml file as

<suite name="QA">
   <test name="1Class">
      <classes>
         <class name="just.Start">
            <methods>
               <include name="Setup"></include>
            </methods>
         </class>
         <class name="just.LoginPage">
            <methods>
               <include name="VerifyTitle"></include>
            </methods>
         </class>
         <class name="just.HomePage">
            <methods>
               <include name="VerifyUserLogin"></include>           
            </methods>
         </class>

      </classes>
    </test> 
  </suite>    

I have implemented this in Java.

-1
votes

If you are going to run the test cases in parallel then you would need to have a separate webdriver instance associated with each test. You can't share the instance across tests in a multithreaded environment.

As for abstracting the initialization of the webdriver instance, there are several ways out there. You could perhaps write a base TestCase class that would build the webdriver instance. Each of your test cases will then extend TestCase class, along those lines.

In TestNG you can control the thread count by specifying the parameter -threadcount in testng.xml. Check out the documentation here. If you are launching TestNG programatically then you can use setThreadCount method. Check Java docs here.

-1
votes

Under one package:

Test1.java -> Have @BeforeTest and @AfterTest to launch the browser and close the browser respectively.

Eg:

public class Test1 {

    public static WebDriver driver;

    @BeforeTest
    public void open_browser()
    {
        driver = new FirefoxDriver();
    }

    @AfterTest
    public void close_browser()
    {
        driver.close();
    }
}

Login.java

public class Login {

@Test
public static void logintest{

    Webelement ele1 = Test1.driver.findElement(...);
}

}

In your XML file:

<test name = "MyTest">
    <classes>
        <class name = "Test1"></class>
        <class name = "Login"></class>
    </classes>
</test>

This will make use of driver instance created in the Test1 file.

Hope it helps !! :)

-1
votes

The Best Way to handle this is:

Have one BaseClass where you just initate the Browser.

public class BaseClass
{
  public static Webdriver driver;
@BeforeSuite
  public void Launch_Browser()
  {
  webDriver Driver= new FirefoxDriver();
  }
}

Then Extend the BaseClass to your all other classes -by usings extend

public class Class1 extends BaseClass
 {
   @Test
    public void TC1()
      {
          //PASS
        }   

 }

and so on. The important thing is you should declare webdriver driver as public static in BaseClass.