Open firefox devtools programmatically with FirefoxProfile - java

How can I launch Firefox 52 with opened devtools using selenium and Java?
Before the merge of Firebug into the Firefox DevTools I used FirefoxProfile to open the console programmatically:
profile.setPreference("extensions.firebug.console.enableSites", true);
etc.
But Firebug does not work anymore now.
So what is the proper way to launch with opened network or console tab?
And also, is there any analog extension to FirePath to launch automatically and use instead of the currently broken FirePath extension?

I managed to launch Firefox with given page and native devtools open from command line using -devtools parameter:
firefox -no-remote -profile "c:\deleteme" -devtools -url "http://example.com/"
It seems to be possible to pass that parameter to the WebDriver by calling
addCommandLineOptions("-devtools") on the FirefoxBinary instance.
(Via How can I tell selenium to start firefox with certain commandline options?.)
Or in Node perhaps by firefox.Options().setBinary(…).addArguments("-devtools")
Spotted the parameter in firefox -help | more, but alas, seems that not all information presented there as well as info given at Command_Line_Options MDC page are still valid. The -devtools one is missing at the MDN page at this moment.

Related

Edge in IE mode selenium 4 with testNG and Java

I am trying to upgrade to selenium 4 to make the tests work in Edge in IE mode. These are Java tests using testNG. The tests work fine when using selenium 3 but fail to launch the browser when using selenium 4. Below is the command used to run the test:
$ java -classpath ".\out\production\baseFramework;.\lib\*;.\bin\*" org.testng.TestNG /LoginToHomePage.xml
Below is my code to launch the browser:
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.attachToEdgeChrome();
ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
ieOptions.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
ieOptions.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, true);
ieOptions.setCapability(InternetExplorerDriver.ENABLE_ELEMENT_CACHE_CLEANUP, true);
ieOptions.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
ieOptions.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true);
WebDriver driver = new InternetExplorerDriver(ieOptions);
driver.get(url);
I have tried with no options and with a combination of all above options but none of them seem to work. There is no other error displayed other than one below line in stack trace making it difficult to debug.
*org.openqa.selenium.ie.InternetExplorerDriver.<init>(Lorg/openqa/selenium/ie/InternetExplorerOptions;)V*
Java : 1.8,
testNG : 6.14,
selenium : 4.1.2,
Edge : 99, also tried 97
Works fine from IDE and also works from command line when using selenium 3, but doesn't work from command line if selenium 4 is used. Weirdly, if i expand the command to mention each jar individually under lib, then Edge launches fine but has a problem when i use the wild card lib*. There are more than 80 jars, so running the command with expanded version is not what i prefer. I have tried to turn on debugging in IE driver options but no other logs are printed. I am not sure if its a Java issue or Selenium issue or something else.
All I want to do it to launch Edge browser and cant seem to make it work. Any idea what more I can try would be appreciated. Thanks

How to open safari without saving cookies

When i m open my Safari using WebDriver and go into my desire site it always
save Cookies so is it possible to open safari without saving Cookies ?
Selenium has no support to programmatically disable cookies in Safari so far. You have to manually disable it. You can try the following methods:
1. Write code which will delete all the files from the Safari folder. Something like this:
def clearCache():
"""This will just delete the files straight from the safari folder, guarentee a clean start"""
try:
#Using rm command assuming you are using MacOS
os.system("rm -rf $HOME/Library/Safari/LocalStorage/*.localstorage")
except Exception, e:
log.error("Error in clearCache: %s" %e)
2. Or you could manually disable cookies from the settings for a particular profile and then use that profile to initiate your Safari WebDriver.
Source

ChromeDriver - Disable developer mode extensions pop up on Selenium WebDriver automation

I'm having the following issue:
When I'm running my automation tests, I keep getting the following alert "Disable Developer Mode Extension" in Chrome.
Is there a way to remove/disable this?. It is a blocker for me as it is making me fail some tests.
Thanks in advance
Did you try disabling the developer extensions with command line param?
Try with the following Selenium WebDriver java code:
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
driver = new ChromeDriver(options);
I cannot disable extensions because I'm developing & testing one.
What I'm doing to dismiss this popup is the following:
I load chrome with my extension using Selenium.
I then immediately create a new window (via the SendKeys(Control-N) method). This predictably brings up the "Disable Developer Mode Extensions" popup after 3 seconds in the new window.
I can't tell programmatically when it pops up (doesn't show in screenshots) so instead I simply wait 4 seconds.
Then I close the tab via driver.Close(); (which also closes this new window). Chrome takes that as "cancel", dismissing the popup, leaving the original window and tab.
I find this necessary because the popup interferes with normal selenium browser interaction, like SendKeys, which I'm using to switch tabs and windows.
As of Chromedriver v2.33, the correct way to avoid this message is to pass load-extension to the excludeSwitches argument of the chromeOptions object. The following Java code should do the trick, although I haven't tested it, as I am running Python:
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Collections.singletonList("load-extension"));
As others have pointed out, the culprit is probably the Chrome Automation Extension, which is loaded automatically by Chromedriver when it launches Chrome.
Chromedriver v2.33 introduced the new switch to prevent the extensions from being loaded:
Updates to excludeSwitches capability that now allows to exclude --load-extension switch.
I suspect that this solution does not require you to disable all extensions. You should still be able to manually load others.
This has been automatically fixed with a combination of ChromeDriver.exe V2.23 + Chrome 53.0.
To understand which chrome version will work with which driver, we can use the following well detailed doc: https://sites.google.com/a/chromium.org/chromedriver/downloads
Enjoy Automated Testing!!
I worked around this issue by using AutoIT.
First, you'll need to create the script.
closechromewarning.au3:
WinWaitActive("[CLASS:Chrome_WidgetWin_1]")
Send("{ESC}")
The script needs to be compiled to a .exe, then place the .exe in the path so it can be run.
Function that closes the warning, using c# syntax:
public void CloseChromeDialog()
{
System.Threading.Thread.Sleep(5000);
Process.Start(#".\closechromewarning.exe");
}
Sleep(4000) did work, but I upped it to Sleep(5000) just to be sure.
Calling CloseChromeDialog():
if(browser == chrome) //pseudo code
CloseChromeDialog();
resolved in chrome 54 and chromedriver 2.25
I too faced this problem. The solution is, if you are using maven then just add:
-Dchrome.switches=--disable-extensions
It will disable all the extensions and you will not face this problem.
I am using selenium Webdriver 2.53 and chrome version 56.0.2924.87 and the chrome driver.exe which I am using is 2.27. with this combination it is working with the
System.setProperty("webdriver.chrome.driver", "./utilities/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
DesiredCapabilities caps = new DesiredCapabilities().chrome();
caps.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(caps);
Try to add setProperty above ChromeDriver instance
System.setProperty("webdriver.chrome.driver","C:/[PATH]/chromedriver.exe");
driver = new ChromeDriver(capabilities);
pywinauto works
import pywinauto
window_title = "Disable Developer Mode Extensions"
app = pywinauto.Application().connect(name_re=window_title)
win_ext = app.window(name=window_title)
win_ext.close()
This is because one of your extensions is running in developer mode. Go through your extension list and disable extensions one-by-one until you find the culprit(s).

To run tests on different browsers using Selenium webdriver+ TestNG+Java

I am trying to run my tests using Selenium web driver + TestNG + Java, everything runs fine with Firefox, but when I tried to extend it to different browsers like IE and Chrome, I have included code for it, but it doesn't seem to work. I am using some excel files to pass input while automating it. Thanks in advance
Required Configuration with IE
1.The IEDriverServer exectuable must be downloaded and placed in your PATH.
2.On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".
3.The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
Check if java plugin is installed in your browser.
For more info go to http://code.google.com/p/selenium/wiki/InternetExplorerDriver
You cannot work with chrome/IE same as Firefox. In order to work with that you need chrome/IE driver, which is an executive file. The code for invoking the chrome and IE is a little different than Firefox.
For chrome you can take help from here
For IE you can take help from here
example for invoking driver
if(BrowserName.equalsIgnoreCase("Firefox")){
driver = new FirefoxDriver();
}else if(BrowserName.equalsIgnoreCase("Chrome")){
String ChromeDriverPath= "D:\\ChromeDriver\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
driver=new ChromeDriver();
}else if(BrowserName.equalsIgnoreCase("IE")){
IEDriverPath32bit= "D:\\IEDriver\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", IEDriverPath32bit);

How do I run Firebug within Selenium WebDriver (Selenium 2)?

What's the best way to activate Firebug in Firefox when running Selenium 2?
Edit: Ok, I realize "best" is open to interpretation, but the profile-based solution really used to be a pain with selenium 1.0. So any alternative is considered better until proved worse ;)
You can create your profile in code and dynamically add required add-ons. Let's assume that you saved Firebug XPI into the C:\FF_Profile folder as firebug.xpi (go to Firebug download page, right-click on the "Add To Firefox" and save as C:\FF_Profile\firebug.xpi).
In code:
final String firebugPath = "C:\\FF_Profile\\firebug.xpi";
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugPath));
// Add more if needed
WebDriver driver = new FirefoxDriver(profile);
This is described in WebDriver FAQ
Do you mean having firebug installed in the browser instance that webdriver launches? If so, you can pass an extension when you instantiate the driver, but the eaisest way is to create a firefox profile with firebug installed and then use the following code before you instantiate the driver:
System.setProperty("webdriver.firefox.profile", "NAME_OF_FIREFOX_PROFILE_WITH_FIREBUG");
Just reference your profile by name. Example in Ruby:
#driver = Selenium::WebDriver.for :firefox, :profile => "default"
Then, load Firefox normally, and add your desired extensions. They will now show up in your Selenium test runs.
Apparently the way the firefox-profile options are consumed has changed in Selenium WebDriver.
The old commandline (Selenium RC):
java -jar selenium-2.28.0.jar -firefoxProfileTemplate ~/.mozilla/firefox/3knu5vz0.selenium
Updated for WebDriver: (note that it wants the profile name rather than the directory)
java -jar selenium-2.28.0.jar -Dwebdriver.firefox.profile=selenium
modify your firefox location to something like
C:\Users\user-name\AppData\Roaming\Mozilla\Firefox\Profiles\sgmqi7hy.default
launch your firefox from selenium / webdriver
make all your required settings
close and restart firefox browser from selenium / webdriver
that's it, it solves your problem !!
I found a profiles.ini in ~/.mozialla/firefox/. In there was a profile named default, which I specified a like the following and then firefox was opened in test just like I opened it regularly (with all plugins etc).
java -jar selenium.jar -Dwebdriver.firefox.profile=default
If none of the above option works. Then try this.
1) Open terminal and type below command (close all existing firefox
sessions first)
firefox -p
2) This will open an option to create a new Firefox profile.
3) Create a profile lets say "SELENIUM".
4) Once the firefox is open straight away install firebug or any
other plugins extension that you want. once done close the window.
5) Now load this new profile via selenium , use below java
statements.
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("SELENIUM");
WebDriver driver = new FirefoxDriver(ffprofile);
6) Done. Enjoy.
I have observed that the firebug is adding to browser and it is disabled by default and not enabled ,when i add firebug to firefox at runtime by using webdriver. So to make it enable we may need to add the below line to profile.
profile.setEnableNativeEvents(true);
Assuming that, Firebug is installed. Your objective is to run Firebug. Firebug can be run/execute by pressing F12 key. So Firebug can be run by following command of Selenium WebDriver with Java:
Actions action = new Actions(driver);
action.sendKeys(Keys.F12).build().perform();

Categories