简体   繁体   中英

How to initialize String originalHandle = driver.getWindowHandle(); once?

I am using TestNG with Selenium. I am trying to use driver.getWindowHandle(); to switch between pop-ups, iframes and such.

The thing is, if I declare it like this in the TNGDriver class

public String originalHandle = driver.getWindowHandle();

I get a java.lang.NullPointerException (obviously, because this is initialized before the driver). How can I declare it once and start using it in other classes? Keep in mind my classes are extended between them and I need to use this originalHandle variable inside methods in other classes, eg:

public void clickOnFacebookIcon() {
    Assert.assertTrue(true, driver.findElement(By.id(FACEBOOK_ICON)).getText());
    driver.findElement(By.id(FACEBOOK_ICON)).click();   
    for(String handle : driver.getWindowHandles()) {
        if (!handle.equals(originalHandle)) {
            driver.switchTo().window(handle);
            driver.close();
        }
    }   
    driver.switchTo().window(originalHandle);
}

Here are my other classes:

TNGDriver class

public class TNGDriver {

public static WebDriver driver; 
public static final String CHROME_DRIVER_PATH = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe";   
private WebDriverWait wait; 

@SuppressWarnings("deprecation")
public void init() {        
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--incognito");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);      
    driver = new ChromeDriver(capabilities);    
    driver.manage().window().maximize();            
}   

public WebDriverWait getWait() {
     wait = new WebDriverWait(driver, 60);
     return wait;
}

Testcase1 class

public class Testcase1 extends Registration {

    TNGDriver tngDriver = new TNGDriver();

    @BeforeTest
    public void setup() {
        tngDriver.init();
    }

    @Test(priority = 1)
    public void step1_clickOnSignIn() {
        clickOnSignIn();
    }
    @Test(priority = 2)
    public void step2_clickOnFacebookIcon() {
        clickOnFacebookIcon();
    }

You can use a desing pattern to do that

https://en.wikipedia.org/wiki/Singleton_pattern

With this pattern you will only have one instance of the object.

class Singleton 
{ 
    // static variable single_instance of type Singleton 
    private static Singleton single_instance = null; 

    // variable of type String 
    public String originalHandle = driver.getWindowHandle(); 

    // private constructor restricted to this class itself 
    private Singleton() 
    { 
       //Do something on constructor
    } 

    // static method to create instance of Singleton class 
    public static Singleton getInstance() 
    { 
        if (single_instance == null) 
            single_instance = new Singleton(); 

        return single_instance; 
    } 
} 

To access it you may do something like this

 Singleton x = Singleton.getInstance(); 
 //To access the String variable 
 x.s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM