0

When I am calling method from another class at that time, WebDriver opens multiple Firefox window.

This is main class:

package selenium2Pack;

import java.sql.Date;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import selenium2Pack.loginPOI;

public class CounselorEducation {

    static FirefoxDriver driver = new FirefoxDriver();
    loginPOI xpath;

    @Test
    public void AddEducation() throws InterruptedException{

        loginPOI.CounselorLogin();
        Thread.sleep(10000);
        //Click on Education tab
        driver.findElement(By.xpath("html/body/div/div[2]/div/div/div/div[2]/div/div[1]/nav/ul/li[2]/a")).click();
        Thread.sleep(5000);
        //Click on AddNew button
        driver.findElement(By.xpath("html/body/div/div[2]/div/div/div/div[2]/div/div[3]/div/div[1]/a/i")).click();

    }

}

This is calling method:

package selenium2Pack;

import javax.swing.text.Document;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class loginPOI {

    static FirefoxDriver driver = new FirefoxDriver();
    static String firstName = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[1]/div[1]/input";
    String Email,LastName,BrowseButton,Username,Password,ContactNo,Gender,DOB,Country,State,City,Street,Address,Marital_status,Logout,EducationTab;
    static String RegisterURl = "http://192.163.222.186/~puzzlelife/development/#/register";
    static String LoginURL = "http://192.163.222.186/~puzzlelife/development/#/login";
    public  void main() throws InterruptedException{
        String firstName = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[1]/div[1]/input";
        String LastName = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[1]/div[2]/input";
        String BrowseButton = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[2]/div[1]/input[1]";
        String Email = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[2]/div[2]/input";
        String Username = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[3]/div[1]/input";
        String Password = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[3]/div[2]/input";
        String ContactNo = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[3]/div[2]/input";
        String Gender = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[4]/div[2]/select";
        String DOB = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[5]/div[1]/div/span/span";
        String Country = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[5]/div[1]/div/span/span";
        String State = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[6]/div[1]/select";
        String City = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[6]/div[2]/select";
        String Street = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[7]/div[1]/input";
        String Address = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[7]/div[2]/textarea";
        String Marital_status = "html/body/div/div[2]/div/div/div/div[2]/div/div/form/div[1]/div[8]/div/select";
        String Logout = "html/body/div/header/div/div[3]/div/a[2]";
        String EducationTab = "html/body/div/div[2]/div/div/div/div[2]/div/div[1]/nav/ul/li[2]/a";
        }

    public static void CounselorLogin() throws InterruptedException{

        driver.get(LoginURL);
        Thread.sleep(15000);
        driver.findElement(By.id("username")).sendKeys("arun");
        driver.findElement(By.id("password")).sendKeys("arun");
        Thread.sleep(5000);
        WebElement submit =driver.findElement(By.xpath("html/body/div/div[2]/div/div[2]/div/div/div[2]/div/form/button"));
        submit.click();
        Thread.sleep(5000);
        String  CurrentURL = driver.getCurrentUrl();
        if(LoginURL.equals(CurrentURL))
            System.out.println( "Un-Successful to login");
            else
            System.out.println("Successful to login");
        Thread.sleep(10000);
        }

    public static void Logout() throws InterruptedException{

        //Click on Logout Button
        driver.findElement(By.xpath("html/body/div/header/div/div[3]/div/a[2]")).click();

        Thread.sleep(5000);

        //Check text on page
        if(driver.getPageSource().contains("Login"))

            System.out.println("Successfully Logout");
    else

        System.out.println("Un-Successfully Logout");

    }

    public static void DOB(){


    }

}
5
  • which script are you executing? Jan 2, 2017 at 11:02
  • public void AddEducation() throws InterruptedException{ loginPOI.CounselorLogin(); Thread.sleep(10000); //Click on Education tab driver.findElement(By.xpath("html/body/div/div[2]/div/div/div/div[2]/div/div[1]/nav/ul/li[2]/a")).click(); Thread.sleep(5000); //Click on AddNew button driver.findElement(By.xpath("html/body/div/div[2]/div/div/div/div[2]/div/div[3]/div/div[1]/a/i")).click(); } Jan 2, 2017 at 11:52
  • I am executing my code, my login code is in another class. I am calling login method by creating object Jan 2, 2017 at 11:54
  • @BhushanPatil, please edit your question and add the formatted code.
    – Rao
    Jan 2, 2017 at 12:06
  • Try to initialize the driver in one class. Here you using 'loginPOI' and 'CounselorEducation'.
    – prinz
    Jan 2, 2017 at 13:23

1 Answer 1

5

Your are initializing the Firefox Browser in both the classes

static FirefoxDriver driver = new FirefoxDriver();

i.e public class loginPOI and public class CounselorEducation

Hence you are getting multiple browsers opened. Initialize the driver only in one class.

Whenever you call a method of a class its Static Variable also get initialized. So First the driver is getting initialized in public class CounselorEducation in that class you are again calling the loginPOI.CounselorLogin(); which again invokes it's own static driver. This is why you are getting multiple windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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