Any sample webdriver code to run all the testng tests on single firefox or IE instance

2,458 views
Skip to first unread message

Balaji Singh .Y

unread,
Jun 1, 2012, 5:07:17 AM6/1/12
to seleniu...@googlegroups.com
 
HI ALL,
 
 
Iam using Webdriver - TestNg framework in my project the problem is i have two different classes or just say only of easy understanding where in i have 7 testcasses in it.
 
When ever i excute my tests using testng it opens 7 Firefox browser instances.
 
But then i just want one Firefox  browser instance to run all my tests.
 

Plz kindly reply to this mail as i have done extensive search but failed.
 
Your ideas are greatly appreciated.
 
With Reagrds,
BALAJI SINGH .Y

Mark Collin

unread,
Jun 1, 2012, 5:15:46 AM6/1/12
to seleniu...@googlegroups.com

Sounds like you are instantiating 7 different driver objects.

 

You’ll need to show us your code.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

Balaji Singh .Y

unread,
Jun 1, 2012, 5:51:40 AM6/1/12
to seleniu...@googlegroups.com
Hi Mark,
 
Thanks for the reply , below is the code that i was using  and i have also  pasted my TestNG.xml code
 
 
package tests;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import datatable.Xls_Reader;
public class OpsLoginPage {
 
        // Global Variables
    WebDriver driver;
    Xls_Reader exl;
 public void OpsLoginPage()
 {
  
   driver=  new FirefoxDriver();
   exl=new Xls_Reader("C:\\Users\\sg0206915\\workspace\\Exceldata\\OPSDATA1.xlsx");
 }
 
 @BeforeTest
 public void acessopsloginpage()
  { 
                 // Open OPS login  Page on FireFox browser
   driver.get("http://ops.qa.elab.lastminute.com/ops/byo/search.asp?"); 
   }
  
 @Test
 public void testopslogin() throws Exception
 {
  try
  { 
  
   driver.manage().deleteAllCookies();
    
   System.out.println("1 . Inside testopslogin method");
  
     // Wait till the OPS login page gets loaded
       driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
  
     //Get the username and password from Excel sheet
       String username1=exl.getCellData("OpsData","Username", 2);
       String password1=exl.getCellData("OpsData","Password", 2);
  
     //set the user name on UI
       WebElement uname=driver.findElement(By.name("username"));
       uname.sendKeys(username1);
    
     //set the password on UI
       WebElement pwd=driver.findElement(By.name("password"));
       pwd.sendKeys(password1);
      
     //wait till username and password are entered
      driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
  
      //now click on login button
        WebElement loginbutton=driver.findElement(By.name("submit"));
        loginbutton.click();
     
    }catch(Exception e)
      {
     System.out.println("Error/Exception message :"+e);
     e.printStackTrace();
  
       }
 }
 
 @Test
 public void testByoHomapage()
 {
  System.out.println("2. hello inside testByoHomapage method");
  
  //Wait till the BYO HomePage gets loaded
  driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
  
  String Site1=exl.getCellData("BYO","Site",2);
  String LeavingCity=exl.getCellData("BYO","Site",2);
  
  System.out.println("Site name :"+Site1);
        System.out.println("LeavingCity :"+LeavingCity);
 }
 @Test
 public void testtwo()
 {
  System.out.println("3 . HELLO THIS IS SECOND TEST METHOD");
 }
 @AfterClass
 public void tampertest()
 {
  System.out.println(" 4 .hello inside tamper method");
  
  driver.close();
  //driver.quit();
 }
}
 
 
TestNG XML code
 
 

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >

<suite name="OPS_AUTOMATION">

<test name="OPS LOGIN PAGE" preserve-order="true">

<classes>

<class name="tests.OpsLoginPage">

<methods>

<include name="acessopsloginpage" />

<include name="testopslogin" />

<include name="testByoHomapage" />

<include name="testtwo" />

<include name="tampertest" />

</methods>

</class>

</classes>

</test>

</suite>

With Regards,
Balaji Singh

--
BALAJI SINGH .Y

Mike Riley

unread,
Jun 1, 2012, 1:32:34 PM6/1/12
to seleniu...@googlegroups.com, balaj...@gmail.com
Yep, that would create 7 browsers alright.

TestNG will create a separate instance of the class for each test you have.  You have the driver being instantiated in the constructor for the class, so naturally you get 7 browser sessions.

Instead you need to create a method with the @BeforeClass annotation and create the driver instance in there.  Also, make driver static, so it is shared by each instance of the class.  You are properly using @AfterClass to close the driver, so now you just need to create it the same way and I think you will be fine.

Mike

To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.



--
BALAJI SINGH .Y

Krishnan Mahadevan

unread,
Jun 1, 2012, 1:52:09 PM6/1/12
to seleniu...@googlegroups.com, balaj...@gmail.com
Mike I doubt if that is the case because he is referring to indivdual methods within the same class and in that case TestNg wont be creating multiple instances of the same class but use the same object vi reflection to invoke all the methods. If what you were saying were to be true then you should see the same behavior when you right click a class which has 7 methods and say run as testng test from within an IDE like eclipse for eg.,
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/pvtIUcnd5b4J.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

Mike Riley

unread,
Jun 1, 2012, 2:24:20 PM6/1/12
to seleniu...@googlegroups.com, balaj...@gmail.com
Well I haven't used TestNG that way, except for way back when I was first playing with it.  I have seen that (TestRunner?) one of the methods in the TestNG stack was creating a new class for all my test methods.  I only have a default constructor in there, so that wasn't an issue, but class-level variables were not preserved, showing that I was dealing with a new class each time.

If that wasn't the case, it would have instantiated a single class and called each test method of the class in turn, leaving the class-level variable intact between each test method.

Mike
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

chiru k

unread,
Jun 2, 2012, 6:34:24 AM6/2/12
to seleniu...@googlegroups.com
Hi


If you want your tests run in a single browser you will do one thing, please comment all the fire fox driver calling methods in your all classes except first class and comment all teardown methods in all classes except last class

I think it resolves your issue what happen please let me know

BALAJI SINGH

unread,
Jun 3, 2012, 3:19:46 AM6/3/12
to seleniu...@googlegroups.com


I tried with it worked out this way.

Iam trying to create static webdriver reference object as shown in the below class and instnatiatin it in "@BeforeTest Method "  that sloved the issue of opening many firefox instances so that all my testng tests are runing on single firefox browser instance.

Moreover i had an issue  like Second test was  running before   to first test , even this issue got sloved by having an simple anology like " @Test(dependsOnMethods = { "testopslogin" } )" in second method , where testopslogin is the first method.
And now i could see that all tests are running on single firefox broser instance and all tests are runing in order.
Below is the code that i modified and used.
package tests;


import java.io.IOException;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.ie.InternetExplorerDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterTest;

import  org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeTest;


import datatable.Xls_Reader;


public class OpsLoginPage {


// Global Variables

static WebDriver driver;

Xls_Reader exl;


@BeforeTest

public void testAccessOpsLoginPage()

{


driver= new FirefoxDriver();

exl=new Xls_Reader("C:\\Users\\sg0206915\\workspace\\Exceldata\\OPSDATA1.xlsx");


//Open OPS login Page on Internet Explorer

driver.get("http://ops.qa.elab.lastminute.com/ops/byo/search.asp?");

driver.manage().deleteAllCookies();

}

@Test

public void testopslogin() throws Exception

{

try

{

System.out.println("1 . Inside testopslogin method");


// Wait till the OPS login page gets loaded

driver.manage().timeouts().implicitlyWait(6,TimeUnit.SECONDS);


//Get the username and password from Excel sheet

String username1=exl.getCellData("OpsData","Username", 2);

String password1=exl.getCellData("OpsData","Password", 2);


//set the user name on UI

WebElement uname=driver.findElement(By.name("username"));

uname.sendKeys(username1);


//set the password on UI

WebElement pwd=driver.findElement(By.name("password"));

pwd.sendKeys(password1);


//wait till username and password are entered

driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);


//now click on login button

WebElement loginbutton=driver.findElement(By.name("submit"));

loginbutton.click();


}catch(Exception e)

{

System.out.println("Error/Exception message :"+e);

e.printStackTrace();


}

}

@Test(dependsOnMethods = { "testopslogin" } )

public void testByoHomapage()

{

try

{

System.out.println("2. hello inside testByoHomapage method");


//Wait till the BYO HomePage gets loaded

driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);


String Site1=exl.getCellData("BYO","Site",2);

String LeavingCity=exl.getCellData("BYO","Leaving _From",2);


System.out.println("Site name :"+Site1);

System.out.println("LeavingCity :"+LeavingCity);


}catch(Exception e)

{

System.out.println("Error/Exception message :"+e);

e.printStackTrace();


}

}

@Test(dependsOnMethods = { "testByoHomapage" })

public void testtwo()

{

System.out.println("3 . HELLO THIS IS third TEST METHOD");

}

@AfterClass

public void tampertest()

{

System.out.println(" 4 .hello inside tamper method");


//driver.close();

//driver.quit();

}

}
 
 
 
My Testng.XML
===========

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="OPS_AUTOMATION" verbose="3" parallel="false">

<test name="OPS LOGIN PAGE" preserve-order="true" >

<classes>

<class name="tests.OpsLoginPage">

<methods>

<include name="testopslogin"/>

<include name="testByoHomapage"/>

<include name="testtwo"/>

</methods>

</class>

</classes>

</test>

</suite>

Mike Riley

unread,
Jun 4, 2012, 12:06:12 PM6/4/12
to seleniu...@googlegroups.com
I believe that if you have an @BeforeXXX annotation you generally will want to use the matching @AfterXXX annotation, at least as a general rule.

I would change your @BeforeTest to be @BeforeClass here.  If it fails all the rest will be skipped automatically  I am personally using the @BeforeSuite, which amounts to the same thing, but since my suite is composed of multiple classes I need to use that.  In my case I also have the @BeforeSuite method do my login, because if the login fails there is no point in doing any of the tests.

Mike



On Sunday, June 3, 2012 12:19:46 AM UTC-7, BALAJI SINGH wrote:


I tried with it worked out this way.

I am trying to create static webdriver reference object as shown in the below class and instantiation it in "@BeforeTest Method "  that solved the issue of opening many firefox instances so that all my testng tests are running on single firefox browser instance.

Moreover i had an issue  like Second test was  running before   to first test , even this issue got sloved by having an simple anology like " @Test(dependsOnMethods = { "testopslogin" } )" in second method , where testopslogin is the first method.

And now i could see that all tests are running on single firefox browser instance and all tests are running in order.

Gulshan Saini

unread,
Jun 4, 2012, 12:40:30 PM6/4/12
to seleniu...@googlegroups.com
No, its wrong way - Remove static modifier. You should change @BeforeTest to @BeforeClass.
Next thing is remove other includes except <include name="testtwo"/> as other methods are dependent on "testtwo".

Give it a try and update your results.

Thanks
Gulshan

rashmeemayee mohapatra

unread,
Jan 29, 2014, 12:17:55 PM1/29/14
to seleniu...@googlegroups.com
Hi , I am new to TestNg, I have 21 testcases ,and each test case has some verification part.so how i will make my classes in structured format,(naming convetion, how many classes i will create ), i struct in creating testNg.xml  ,and i have to extract data from a json file..Need your help......Thanks Rashmee
Reply all
Reply to author
Forward
0 new messages