Two empty instances of firefox browser opens testng selenium webdriver

2.5k views Asked by At

I have been having this problem that when i run a testng test suite firefox open three instances of browser and execute my tests on the third instance leaving first two instances completely empty, what i want my firefox driver to do is to create just one instance of the browser and perform my tests on it.My testing.xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNG Suite" parallel="false">

    <test name="Firefox Test">

        <parameter name="browserName" value="firefox"></parameter>

         <classes>
            <class name="Publisher.CreateSites" />

        </classes>

    </test> <!-- Test -->

     <test name="Chrome Test">

        <parameter name="browserName" value="chrome"></parameter>

         <classes>
            <class name="Publisher.CreateSites" />

        </classes>

    </test> <!-- Test -->
</suite> <!-- Suite -->

and my java class looks like

public class CreateSites 
{

    private ReadExcel fr = new ReadExcel();

    public WebDriver driver = new FirefoxDriver();

    @BeforeTest
    @Parameters("browserName")

    public void setup(String browserName) throws Exception{

        //Check if parameter passed from TestNG is 'firefox'

        if(browserName.equalsIgnoreCase("firefox"))
        {

            //create firefox instance
            driver = new FirefoxDriver();

        }

        //Check if parameter passed as 'chrome'

        else if(browserName.equalsIgnoreCase("chrome"))
        {

            //set path to chromedriver.exe You may need to download it from http://code.google.com/p/selenium/wiki/ChromeDriver
            System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");

            //create chrome instance
            driver = new ChromeDriver();

        }

        else
        {
            //If no browser passed throw exception
            throw new Exception("Browser is not correct");
        }

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://portal.admaxim.com/main3/home.jsf");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);

    }

     @Test(priority=0)
      public void loginTest()
      {
            fr = new ReadExcel();
            fr.readexel("TestNGExcelFile", "Create_Site");
            driver.findElement(By.id("loginForm:userName")).click();
            driver.findElement(By.id("loginForm:userName")).clear();
            driver.findElement(By.id("loginForm:userName")).sendKeys(fr.List1.get(0));

            driver.findElement(By.id("loginForm:password")).click();
            driver.findElement(By.id("loginForm:password")).clear();
            driver.findElement(By.id("loginForm:password")).sendKeys(fr.List1.get(1));
            driver.findElement(By.id("loginForm:submit")).click();
            driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
      }
     @Test(priority=1)
     public void moveToPublisher() throws InterruptedException
     {
         driver.findElement(By.xpath(".//*[@id='homeForm:menuPublisherOption1']/img")).click();
         Thread.sleep(4000);
     }
     @Test(priority=2)
     public void clickOnCreateSiteLink()
     {
         driver.findElement(By.xpath(".//*[@id='deliveryImg']")).click();
         driver.findElement(By.xpath(".//*[@id='homeForm:welcomePubView:createSiteLbl']")).click();
         driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
     }
     @Test(priority=3)
     public void selectAccount() throws InterruptedException
     {
         Thread.sleep(3000);
         Select account = new Select(driver.findElement(By.xpath(".//*[@id='availableSubAccountsList']")));
         account.selectByVisibleText("droidpubsub");
         driver.findElement(By.xpath(".//*[@id='selectSubAccount_div']/table/tbody/tr[2]/td[2]/input[1]")).click();
     }
     @Test(priority=4)
     public void selectBasicInfo()
     {
         driver.findElement(By.xpath(".//*[@id='accordion']/ul/li[2]/a")).click();

         driver.findElement(By.id("categoryForm:name")).click();
         driver.findElement(By.id("categoryForm:name")).clear();
         driver.findElement(By.id("categoryForm:name")).sendKeys(fr.List1.get(2));

         driver.findElement(By.id("categoryForm:externalSiteID")).click();
         driver.findElement(By.id("categoryForm:externalSiteID")).clear();
         driver.findElement(By.id("categoryForm:externalSiteID")).sendKeys(fr.List1.get(3));

         driver.findElement(By.id("categoryForm:siteURL")).click();
         driver.findElement(By.id("categoryForm:siteURL")).clear();
         driver.findElement(By.id("categoryForm:siteURL")).sendKeys(fr.List1.get(4));

         driver.findElement(By.id("categoryForm:description")).click();
         driver.findElement(By.id("categoryForm:description")).clear();
         driver.findElement(By.id("categoryForm:description")).sendKeys(fr.List1.get(5));

     }
     @Test(priority=5)
     public void setTargeting() throws InterruptedException
     {
         driver.findElement(By.xpath(".//*[@id='accordion']/ul/li[3]/a")).click();
         Thread.sleep(2000);

         // Select values from Demographic tab
         // Select Gender.
         WebElement selectGender = driver.findElement(By.id("categoryForm:demographics_avail_gender"));
         Select genderOptions = new Select(selectGender);
         for(int i= 0; i<2; i++)
         {
         genderOptions.selectByIndex(i);

         }
         driver.findElement(By.xpath(".//*[@id='genderDemographicDiv']/table/tbody/tr[3]/td[2]/input[1]")).click();

         // Select Age.
         driver.findElement(By.xpath(".//*[@id='ageDemographicTabBtnTd']")).click();
         WebElement selectAge = driver.findElement(By.id("categoryForm:demographics_avail_age"));
         Select Ageoptions = new Select(selectAge);
         for(int i= 0; i<5; i++)
         {
             Ageoptions.selectByIndex(i);

         }
         driver.findElement(By.xpath(".//*[@id='ageDemographicDiv']/table/tbody/tr[3]/td[2]/input[1]")).click();

         // Select Occupation type.
         driver.findElement(By.xpath(".//*[@id='occupationTypeDemographicTabBtnTd']/a")).click();
         WebElement selectOccupationType = driver.findElement(By.id("categoryForm:demographics_avail_occupationType"));
         Select occupationTypeOptions = new Select(selectOccupationType);
         for(int i= 0; i<9; i++)
         {
             occupationTypeOptions.selectByIndex(i);

         }
         driver.findElement(By.xpath(".//*[@id='occupationTypeDemographicDiv']/table/tbody/tr[3]/td[2]/input[1]")).click();

        // Select Occupation Industry.
         driver.findElement(By.xpath(".//*[@id='occupationIndustryDemographicTabBtnTd']/a")).click();
         WebElement selectOccupationIndustry = driver.findElement(By.id("categoryForm:demographics_avail_occupationIndustry"));
         Select occupationIndustryOptions = new Select(selectOccupationIndustry);
         for(int i= 0; i<13; i++)
         {
             occupationIndustryOptions.selectByIndex(i);

         }
         driver.findElement(By.xpath(".//*[@id='occupationIndustryDemographicDiv']/table/tbody/tr[3]/td[2]/input[1]")).click();



     }
     /*@Test
      public void logout()
      {
          driver.findElement(By.xpath(".//*[@id='dd_Icon']")).click();
          driver.findElement(By.xpath(".//*[@id='homeForm:top:lnkLogoutTxt']")).click();
      }

      @AfterTest
      public void closeBrowser()
      {
          driver.quit();
      }*/
}
1

There are 1 answers

0
SiKing On

These are the only relevant parts of your code:

public class CreateSites 
{
    public WebDriver driver = new FirefoxDriver();

    @BeforeTest
    public void setup(String browserName) throws Exception {
            //create firefox instance
            driver = new FirefoxDriver();
     }

    /*
    @AfterTest
    public void closeBrowser()
    {
        driver.quit();
    }*/
}

everything else is not relevant to your problem.

The first line of your class new FirefoxDriver() opens a Firefox browser. Before every test, you again call new FirefoxDriver() which opens another browser. Lastly, you commented out the driver.quit(), which would normally close the browser!

What you probably want is:

public class CreateSites 
{
    public WebDriver driver;  // do not open Firefox here!

    @BeforeTest
    public void setup(String browserName) throws Exception {
            //create firefox instance
            driver = new FirefoxDriver();
     }

    @AfterTest  // make sure this runs!
    public void closeBrowser()
    {
        driver.quit();
    }
}