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.
// 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