0

in Chrome browser. authentication popup code not working with Selenium using Java if your connection is not secure notification appears

I have handle authentication popup in my application using robot class and thread.

for handling this authentication popup i have created separate thread and use robot class and it work fine and achieved expected result.

but in one case my code is not working.

Problem: In my application provided functionality copy URL and paste in browser(Chrome ) then authentication popup appears and its expected.

some time when URL taking time to load then chrome browser display notification before popup appears. if Browser notification appears the my code is not working.

please see Screenshot and my code.

enter image description here

// Code for Chrome browser

>else if (CONFIG.getProperty("browserType").equals("Chrome")){              
>System.setProperty("webdriver.chrome.driver",CONFIG.getProperty("driverPath")+"chromedriver.exe");         
>String downloadFilepath = filePath;             
>HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
>                  chromePrefs.put("profile.default_content_settings.popups",0);
>                //1-Allow, 2-Block, 0-default
>chromePrefs.put("profile.default_content_setting_values.notifications",> 0);
>chromePrefs.put("download.default_directory", downloadFilepath);
> chromePrefs.put("profile.content_settings.plugin_whitelist.adobe-flash-player", 1);
>chromePrefs.put("profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player", 1);           
>chromePrefs.put("PluginsAllowedForUrls", CONFIG.getProperty("websiteUrl")); 
>  ChromeOptions options = new ChromeOptions();
> HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
>options.setExperimentalOption("prefs", chromePrefs);
>     
>options.addArguments("disable-popup-blocking");                          
>options.addArguments("--disable-notifications");
>options.addArguments("disable-infobars");
>options.addArguments("--disable-web-security");
>options.addArguments("--allow-running-insecure-content");
>options.addArguments("--test-type");
>options.addArguments("--start-maximized");               
>// Enable Flash for this site                            
>                 DesiredCapabilities cap = DesiredCapabilities.chrome();
>                 cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
>                 cap.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION,
> true);              
>                 cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);           
>                 cap.setCapability(ChromeOptions.CAPABILITY, options);          
>                 driver = new ChromeDriver(cap);
>      
>                 APP_LOGS.debug("Chrome Browser launch successfully");
>                 isBrowserOpened=true;
>                   String waitTime=CONFIG.getProperty("default_implicitWait");
>                   driver.manage().timeouts().implicitlyWait(Long.parseLong((waitTime).trim()),
> TimeUnit.SECONDS);
>                   driver.manage().timeouts().pageLoadTimeout(150, TimeUnit.SECONDS);              
            }

//inner class for handle authentication popup thread
public class HandleAuthWindow implements Runnable {

      @Override
      public void run() {
          try {
              AuthWindow();
          } catch (Exception ex) {
              System.out.println("Error in Login Thread: " + ex.getMessage());
          }
      }

      public void AuthWindow() throws Exception {

          //wait - increase this wait period if required
          Thread.sleep(10000);

          //create robot for keyboard operations
          Robot rb = new Robot();

          //Enter user name by ctrl-v
          StringSelection username = new StringSelection(CONFIG.getProperty("username"));
          Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);            
          rb.keyPress(KeyEvent.VK_CONTROL);
          rb.keyPress(KeyEvent.VK_V);
          rb.keyRelease(KeyEvent.VK_V);
          rb.keyRelease(KeyEvent.VK_CONTROL);

          //tab to password entry field
          rb.keyPress(KeyEvent.VK_TAB);
          rb.keyRelease(KeyEvent.VK_TAB);
          Thread.sleep(2000);

          //Enter password by ctrl-v
          StringSelection pwd = new StringSelection(CONFIG.getProperty("password"));
          Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
          rb.keyPress(KeyEvent.VK_CONTROL);
          rb.keyPress(KeyEvent.VK_V);
          rb.keyRelease(KeyEvent.VK_V);
          rb.keyRelease(KeyEvent.VK_CONTROL);

          //press enter
          rb.keyPress(KeyEvent.VK_ENTER);
          rb.keyRelease(KeyEvent.VK_ENTER);

          //wait
          Thread.sleep(3000);
      }
  }

//Calling this thread in our Test case please see below // Open new tab in current browser and shift focus on new tab

((JavascriptExecutor)driver).executeScript("window.open('about:blank','_blank')");
    >Thread.sleep(1000); 
    >ArrayList<String> tabs=new
    > ArrayList<String>(driver.getWindowHandles());   
    > 
    > driver.switchTo().window(tabs.get(1));  
    > try { 

    >//create new thread for interaction with windows authentication      

    >(new Thread(new HandleAuthWindow())).start();  
    >//openning copyodta url          
    > driver.get(DataLink);    } catch (Exception e) { 
    >     System.out.println("Exception Found while handling Auth Popup window
    > and launching CopyOdata Url , ");  );        }

Any help is appreciated Thanks

VpnT
  • 65
  • 2
  • 10

1 Answers1

0

You can use below code to disable notification for chrome :

ChromeOptions ops = new ChromeOptions();
ops.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "path to driver");
WebDriver driver = new ChromeDriver(ops);

For firefox you can try this :

System.setProperty("webdriver.gecko.driver","path to driver");
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.setPreference("dom.webnotifications.enabled", false);
WebDriver driver = new FirefoxDriver(ffprofile);

It is deprecated but still a solution you can try.

For reference : https://superuser.com/questions/959269/disabling-the-enable-notifications-popup-in-firefox
You can do it manually.

Hope it helps you.

dangi13
  • 1,275
  • 1
  • 8
  • 11
  • i already use ChromeOption in my code , it doesn't help to solve the problem. – VpnT Jul 06 '18 at 11:44
  • You can refer to link : https://support.mozilla.org/en-US/questions/1140700 it might help you out. please do let us know – dangi13 Jul 06 '18 at 12:32
  • Thanks, but firefox browser is not supported by my application, i am looking solution for chrome browser. – VpnT Jul 06 '18 at 13:05
  • May be this can help you . https://stackoverflow.com/questions/34343423/disable-chrome-notifications-selenium. If you found a solution than do let us know – dangi13 Jul 06 '18 at 13:19