Sanjay
Sanjay

Reputation: 85

Disable popup "Restore Pages? Chrome didn't shut down correctly." in selenium webdriver Java

Though I have seen some related questions, I am putting this question again as I am still facing the issue. I have a website that asks for login verification code on first time login and then it does not ask in further login instances. If I dont use a custom browser profile, the site asks verification code every time when selenium runs the login. Hence I have used customed browser profile as below (this is not the question)

options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");

Then, After the test is finished, I quit the browser using

driver.quit();

enter image description here

Problem is, next time I run the test again, the chrome browser opens with the popup "Restore Pages? Chrome didn't shut down correctly." and the site asks for verification code again after login. Entering verification code is not part my script, so the script fails. Then I close the browser manually by clicking on the X on the top corner. Then I run the test again, chrome browser opens Normally without the popup and the site does not ask for verification code, login is successful. then, script closes browser itself by the driver.quit(). Then I run the test again, chrome browser opens with the popup and test fails again. This shows that driver.quit() is not closing the browser correctly. I have tried several settings, but still the popup is coming.

  1. preferences file -> "exit_type":"none" (as well as "normal") and exit cleanly = true
  2. site settings -> pop-ups and redirects -> Blocked (recommended)
  3. I have also tried checking alerts exists or not, but it says "No alert". Script does not detect the popup as alert.

How can I make sure the browser is opened NORMALLY every next time after driver.quit() closes the browser in the previous test run? Please help.

Below is my code: I use chrome extension, so I cannot disable extension also.

        String extFilePath = "C:\\ChromeDriver\\Salesforce_inspector.crx";

        System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File(extFilePath));
        options.addArguments("--disable-notifications");
        options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");
        options.addArguments("--start-maximized");

        WebDriver driver = new ChromeDriver(options);


        try 
        { 
            driver.switchTo().alert(); 
            System.out.println("alert accepted"); 
        }   // try 
        catch (NoAlertPresentException Ex) 
        { 
            System.out.println("No alert");
        }           

        driver.get("salesforce url");

I dont want to use incognito mode, because I need to retain the cookies and passwords to prevent the login verification code issue mentioned earlier.

Upvotes: 5

Views: 6989

Answers (1)

Gustavo Ramos
Gustavo Ramos

Reputation: 61

I solved this issue setting the exit_type to normal in the preferences after set up the user dir.

        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("profile.exit_type", "Normal");
        options.setExperimentalOption("prefs", prefs);

In your case will be something like this

        String extFilePath = "C:\\ChromeDriver\\Salesforce_inspector.crx";

    System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addExtensions(new File(extFilePath));
    options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");
    options.addArguments("--start-maximized");
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.exit_type", "Normal");
    options.setExperimentalOption("prefs", prefs);

    WebDriver driver = new ChromeDriver(options);


    try 
    { 
        driver.switchTo().alert(); 
        System.out.println("alert accepted"); 
    }   // try 
    catch (NoAlertPresentException Ex) 
    { 
        System.out.println("No alert");
    }           

    driver.get("salesforce url");

Upvotes: 4

Related Questions