How to handle downloading popup alert, while downloading a file in chrome - selenium

Point 1. If I launched normal chrome driver without loading any profile then it would block all exe (keep/discard)
Point 2. When I provided my chrome profile, these are my observations:
Clean exe downloading normally
Download error exe giving same dialogue on chrome driver as normally on chrome browser
Issue is here: those exe which are througing alert of keep/decline popup normally ...those are normally downloading on driver.
SNAPSHOT attached (http://i.stack.imgur.com/PtZ18.png)
I am loading chrome profile by this pattern
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
options.addArguments("test-type");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(options);
So this is the main Issue
One more thing: Actually these popups are not html pages. These are over layer on chrome so any technique to spy those using any other free tool? or selenium?

Try this maybe
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");

Related

Javascriptexecutor is not working while running through safari webdriver

I want to perform a click using javascriptexecutor.
JavasciptExecutor jse=(JavascriptExecutor) webDriver;
jse.executescript("arguments[0].click();",webelement)
While running from safari,it is showing "undefined is not a function<evaluating arguments[0].click "
Same works fine in chrome driver.
Am I need to add any capabilities while initiating webDriver?
I have already added capabilities.SetJavascriptEnabled(true). Still it is not working
You have to set the capabilities of driver browserwise.
When running the testcases in safari browser set a global flag for browser. You can create a config file and add "safari" or "chrome" when running it through safari or chrome.
In your source initialize your driver for safari like below :
if (browser.equals("safari")) {
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseCleanSession(true);
safariOptions.setUseTechnologyPreview(true);
driver = new SafariDriver(safariOptions);
}
Then navigate to your desired url. Here i am adding a sample source for your understanding.
driver.get("https://www.google.com");
WebElement elem = driver.findElement(By.className("gb_P"));
jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", elem);
Hope this will solve your issue. Feel free to comment here for more information.

Unable to find element on new tab link

When I run a browser with extension a pop up arise that says "Disable developer mode" to do so my script clicks on Disable, it opens a new tab with url "chrome://extensions/"
Now to click on checkbox of developer mode it always give an error "Unable to locate element."
driver.findelement(By.id("toggle-dev-on"));
http://prntscr.com/f8fbde
Here is the solution for your Question:
As per best practices to work with Selenium 3.4.0 you must download the latest chromedriver v2.29 from here, update your Google Chrome to 58.x.
Updating your chromedriver to v2.29 will solve your issue of Disable developer mode
To work with Google Chrome you can take help of ChromeOptions Class as follows:
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://gmail.com");
Let me know if this solves your Question.
You need to modify the appropriate browser profile to have JS disabled. Like for FireFox/Chrome you can tell Selenium which profile to use.
Chrome :
Map prefs = new HashMap();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
FireFox :
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.setPreference("dom.webnotifications.enabled", false);
WebDriver driver = new FirefoxDriver(ffprofile);
Hope this will work out for your case.

How to automatically close firebug tab when launching selenium firefox webdriver with firebug?

I have included firebug when launching firefox driver and it works very fine with latest selenium web driver 3.0 but meanwhile it also opens new firebug tab every time when launching browser.
As code says, i have included firebug file and added this extension in created profile. Is there any way to close the firebug tab automatically after launching the browser? If there is no automatic way then i need to use tweak to close window named "Firebug" right?
Code:
File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);
You can set the "showFirstRunPage" flag to "false" in your porfile.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("extensions.firebug.showFirstRunPage", false);
Here is a link to all firebug preferences you can set.
Firebug Preferences
An other solution is to set the "currentVersion" to a big number like this.
profile.setPreference("extensions.firebug.currentVersion", "999");
I prefer the first on ;)
I was looking for automatically closing Firebug window but i think its not possible so posting an answer to handle opened windows after launching firebox browser with firebug capabilities, unfortunately you need to deal with extra lines of code due to this issue :)
Below solution it works fine, just use it like below:
Working code:
File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);
// Close the firebug window
Thread.sleep(4000); // Firebug window takes time to open it
Set <String> windows = webDriver.getWindowHandles();
String mainwindow = webDriver.getWindowHandle();
for (String handle: windows) {
webDriver.switchTo().window(handle);
if (!handle.equals(mainwindow)) {
webDriver.close();
}
}
webDriver.switchTo().window(mainwindow);

Selenium- Popup got blocked if I clicked Link using Javascript Executor in Chrome browser

Problem Description - Upon clicking a link on the page, popup got blocked automatically.
file = new File("tools/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
caps.setCapability("ignoreZoomSetting", true);
caps.setCapability("nativeEvents", false);
driver = new ChromeDriver(caps);
Environment used – Selenium WebDriver – 2.43.0 ChromeDriver, Windows 7
Note – it is working fine on Firefox & IE, this issue is happening on Chrome only.
Please assist on this.
This is a Chrome Option to disable all popups for the site
Open Chrome.
Find a page that has pop-ups blocked for you.
At the end of the address bar, click the pop-up blocker icon chrome op-up
locked.
Click the link for the pop-up window you'd like to see.
To always see pop-ups for the site, select Always show pop-ups from [site].
Once you have this set use the profile it is saved against to load for the test
Another option is to open the site and Shift F5 to do a Cache Refresh
Load a Profile. The code below is C# and you haven't specified a language. Please see the links provided for Java examples
ChromeOptions options = new ChromeOptions();
userDataPath = "C:/Users/user_name/AppData/Local/Google/Chrome/User Data";
options.AddArguments("user-data-dir=" + userDataPath);
options.AddArguments("--start-maximized");
driver = new ChromeDriver("pathToChromeDriver", options);
You can pass the chromeOption to allow pop-up as shown in below:
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.popups", 1);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

Browser Plugin Testing With Selenium

I am writing a webapp that has a browser plugin component for both firefox and chrome. My current testing system uses a series of Selenium tests created through Selenium IDE.
Is it possible to also have selenium install, activate, and delete browser plugins for firefox and chrome (possibly other browsers as well)?
I think the biggest concern is that installing/enabling the browser plugin requires a browser restart, and I'm not sure if that would through selenium off.
The acquisition of the plugin is easily handled by visiting an internal site-link to a php-script that detects your browser.
The answer is Yes, Selenium 2 supports (remote) installation of browser extensions.
The Chrome and Firefox WebDriver support the installation of extensions, remotely. Here's sample code for Chrome and Firefox:
Chrome
File file = new File("extension.crx"); // zip files are also accepted
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);
// Option 1: Locally.
WebDriver driver = new ChromeDriver(options);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Firefox
File file = new File("extension.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
// Option 1: Locally
WebDriver driver = new FirefoxDriver(firefoxProfile);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
I have also implemented automated installation of Opera and Safari extensions, and they have been merged upstream:
OperaDriver: https://github.com/operasoftware/operadriver/pull/93
SafariDriver: https://github.com/SeleniumHQ/selenium/pull/87
Opera
This API is similar to the FirefoxDriver.
File file = new File("extension.oex"); // Must end with ".oex"
OperaProfile operaProfile = new OperaProfile();
operaProfile.addExtension(file);
// Option 1: Locally
WebDriver driver = new OperaDriver(operaProfile);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.opera();
capabilities.setCapability("opera.profile", operaProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Safari
This API is similar to the ChromeDriver.
File file = new File("extension.safariextz");
SafariOptions options = new SafariOptions();
options.addExtensions(file);
// Option 1: Locally.
WebDriver driver = new SafariDriver(options);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.safari();
capabilities.setCapability(SafariOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Internet Explorer
Good luck.
Short answer: no
Installing a browser extension is outside of the scope of handling in Selenium.
In Chrome, it displays a modal window that is not "clickable" with Selenium when you want to add a plugin or app. Chrome does not require restarting.
Firefox has the same kind of behaviour to prompt for extension permissions.
You can try something that resides outside of the browser to do what you want. Sikuli might do the trick.