-1

I am trying to refresh my Selenium knowledge. So, I am writing a script to navigate through my Google account. After I successfully sign in to Google, a pop up appears under my profile icon on the upper right side in Firefox. I no longer get these pop ups when I sign manually. This is probably because of cookies or some other browser setting. I do not care about the reason why this occurs.

However, since it occurs, I believe it may be preventing my script from closing the browser with driver.close(); I also tried driver.quit(); Neither of these are causing the browser to close.

So, I thought I would try switching windows by doing an iteration through the windows. This is not allowing me to select the pop up that appears to close it.

I also tried to create an Alert alert and switch to it:

driver.switchto.alert();

driver.dismiss();

This is not dismissing this pop up in Google either.

In the end, I do not care about this pop up. I know I listed 2 separate issues here. But, in the end, I just want to close the browser. If I can also learn how to switch to this pop up and click the x to close it, that is a bonus.

//Code added here -------------------------

public void sign_out( WebDriver driver )
{
    //At this point,I am already signed in.  But, that Google pop up appears
    //The pop up says "Get to Google faster. Switch your default search engine to Google."

    //Wait for "x" to appear
    myDynamicElement = (new WebDriverWait(driver, wait_for_element_time)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(x_path_x_pop_up)));

    //This is set above as a class member
    //final String x_path_x_pop_up = "/html/body/div/div[3]/div[1]/div/div/div/div[2]/div[4]/div/a";
    WebElement x_icon = driver.findElement(By.xpath(x_path_x_pop_up));

    //Click x to close it
    x_icon.click();

    String myWindowHandle = driver.getWindowHandle();
    String subWindowHandle = null;

    Set<String> handles = driver.getWindowHandles(); // get all window handles
    Iterator<String> iterator = handles.iterator();
    while (iterator.hasNext()){
        subWindowHandle = iterator.next();
    }
    driver.switchTo().window(subWindowHandle); // switch to popup window

// driver.switchTo().window(myWindowHandle);

// This never happens now on this click of the profile icon in Google to sign out. //Click on profile icon m_profile_icon.click();

    //Wait for "Sign Out" button to appear
    myDynamicElement = (new WebDriverWait(driver, wait_for_element_time)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(x_path_sign_out_button)));

    //Get sign out button
    WebElement sign_out_button = driver.findElement(By.xpath(x_path_sign_out_button) );

    sign_out_button.click();
}

}

toolmania1
  • 309
  • 7
  • 15

2 Answers2

0

Well as long as you are working in Java you can try the Robot framework and sendKeys as a workaround (ALT+F4):

import java.lang.reflect.*;
import java.awt.Robot;
import java.awt.event.KeyEvent;


public class RobotUtilities {

    public static void sendKeyCombo(String keys[]) {
        try {

            Robot robot = new Robot();

            Class<?> cl = KeyEvent.class;

            int [] intKeys = new int [keys.length];

            for (int i = 0; i < keys.length; i++) {
                Field field = cl.getDeclaredField(keys[i]);
                intKeys[i] = field.getInt(field);
                robot.keyPress(intKeys[i]);
            }

            for (int i = keys.length - 1; i >= 0; i--)
                robot.keyRelease(intKeys[i]);
        }
        catch (Throwable e) {
            System.err.println(e);
        }
    }


    // main for testing purposes
    public static void main(String args[]) {
        String [] keys = {"VK_ALT", "VK_F4"};
        sendKeyCombo(keys);
    }

}

Alternative more robust solution using windowHandles:

I took this from here

You can switch between windows as below:

// Store the current window handle
String winHandleBefore = driver.getWindowHandle();

// Perform the click operation that opens new window

// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

// Perform the actions on new window

// Close the new window, if that window no more required
driver.close();

// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);

// Continue with original browser (first window)
Community
  • 1
  • 1
Xwris Stoixeia
  • 1,831
  • 21
  • 22
  • I tried the switch handle and did not seem to have any luck. I will try again, maybe I was doing something incorrectly. – toolmania1 Apr 04 '17 at 02:13
0

So, my problem had to do with errors in the JUnit part of my code. I commented out these sections. Once everything was error free, the driver.quit() closed the browser.

I also did some research and found that JUnit does not allow passing class values between different @Test tests. So, I also moved all of my open browser and close browser into the same @Test. This cleaned everything up as well.

toolmania1
  • 309
  • 7
  • 15