I've Googled, searched SO, and looked at the Selenium Javadocs but haven't found an answer to this question.
Is there any way, using Selenium WebDriver, to accept those "Share Location" popups that get displayed by browsers (Chrome, Firefox, IE) when a website is trying to track your location?
Also, I'm writing up my Selenium code in Java and using the Selenium Client Drivers.
I've tried running the Selenium IDE (the Firefox add-on) and recording me accepting the "Share Location" popup that Firefox popsup but nothing gets recorded in the Selenium IDE.
If the popup is part of the browser then the answer is no. If you want to interact with something displayed in a second window ( not part of the browser) then yes. There should be a .switch_to method.
Although if you can have this page remember your settings you can load a profile when you create a new instance of the browser.
In windows you can create a profile in firefox by going to the start menu and typing "firefox.exe -p" here you can create a new profile where you you would have a "remember me check box" checked.
As far as loading a profile in Java + Selenium I'm not sure how you would do this. I use Ruby + Selenium and this is how I do it.
$driver = Selenium::WebDriver.for :firefox, :profile => "MyProfile"
hope this helps
I have got the solution to this in firefox but still working on chrome. The below code should help and fetch your exact location on maps:
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
myprofile.setPreference("geo.prompt.testing", true);
myprofile.setPreference("geo.prompt.testing.allow", true);
myprofile.setPreference("geo.wifi.uri", "https://location.services.mozilla.com/v1/geolocate?key=%MOZILLA_API_KEY%");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE,myprofile);
After this just pass the FirefoxProfile instance in the driver method just as below(for the above code):
driver = new FirefoxDriver(myprofile);
That should be fine, write all the other stuff you want and execute!!
Related
I am creating automated test cases with Selenium WebDriver 2. My code is set up, so that FireBug automatically is opened when Selenium WebDriver opens FireFox. A few seconds later I ask Selenium to close it again. For some reason, if I don't do this, sometimes FireBug won't be available to me at a later stage.
My problem:
When I use the TestNG suite to run my test suite, some of the test cases end up opening FireBug but not closing it again. My guess is, that this is due to existing browser windows already being open. The "close FireBug code" is then misused to open FireBug instead of closing it.
For this reason, I would like to make a check to see if FireBug is open, and if so, I would like to close it.
Opening code:
String firebugPath = TO_Constant.fireBug();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugPath));
profile.setPreference("extensions.firebug.showFirstRunPage", false);
profile.setPreference("extensions.firebug.allPagesActivation", "on");
driver = new FirefoxDriver(profile);
Code to close FireBug:
Actions action = new Actions(driver);
action.sendKeys(Keys.F12).build().perform();
I can find ways to do this in javascript, using window.console. Is there any way to do something similar in java?
console.log('is DevTools open?', window.devtools.open);
You can also listen to an event:
window.addEventListener('devtoolschange', function (e) {
console.log('is DevTools open?', e.detail.open);
});
It doesn't work when DevTools is undocked. However, works with the Chrome/Safari/Firefox DevTools and Firebug.
The answer is by Sindre Sorhus and has been taken from here!
Find out whether Chrome console is open
i am trying to open webDriver with chrome extension (Modify header value) using below code
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
but this code is opening the webDriver instance with the extention in another window can any one know how to invoke it with chrome extension not in a new tab.
You may refer with this thread. Be noted that Selenium supports interaction with web view only. You can check this tutorial on how to test your Chrome Extension with selenium. However, you can also use of sikuli (Automation tool that make use of image recognition), to click the chrome add-on. After that add-on popup will be another browser window, so use switch window to perform actions on add-on popup. Hope this helps!
friends i am using selenium webdriver to fetch pagesource in my java desktop application. I am getting problem with only one URL which is "http://www.greenbergdental.com/offices/university" every time I try to get this my current java thread was stuck and whole application was stopped. Please suggest me some solution regarding this I have tried Pagetimeout, Scripttimeout.
Note: I can not enable javascript for some reason. so please dont give me suggestions like turn on javascript that I can't.
websiteURL ="http://www.greenbergdental.com/offices/university";
WebDriver htmlUnitDriver = new HtmlUnitDriver();
htmlUnitDriver.manage().timeouts().pageLoadTimeout(15,TimeUnit.SECONDS);
htmlUnitDriver.manage().timeouts().setScriptTimeout(15,TimeUnit.SECONDS);
htmlUnitDriver.get(websiteURL);
String pagesource = htmlUnitDriver.getPageSource();
Try running for a specific browser (though web apps better be neutral of recent browser is used). If you are using latest firefox browser, try this:
HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);
For chrome:
HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.CHROME);
Also, you could go for setting driver with capabilities:
HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(capabilities);
I am developing test automation using Selenium with Java. I would like to install/add extension to chrome instance launched through Selenium. One way to install/add extension is using .crx file as follow.
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(ext_path));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
But I would like to install/add extension from Chrome Web Store. Can anyone please suggest how to achieve this.
Thanks.
You can install extension (packed/unpacked) to chrome with options.addExtensions/addArguments as mentioned here:- https://sites.google.com/a/chromium.org/chromedriver/extensions
alternative: Chromedriver every time opens new chrome instance with temporary profile for the execution. To avoid this you can use existing profile with desired chrome extension installed OR you can send commands to already running instance of chrome which has desir d extension installed.
usually, inline/webstore installation of a Chrome extension can't be tested with ChromeDriver/Selenium alone, because of confirmation dialog.
you can use platform-specific hacks to click on the button (e.g. via AutoIt),
I am trying to implement some Selenium 2 Webdriver tests with JUnit. The documentation on SeleniumHQ.org and the web is confusing to me because it seems to jump back and forth between Selenium RC and Webdriver. Plus, my Java is not very strong. I've took a few courses years ago, but haven't used it much. I want to have JUnit tests run from a headless CI server, and have Firefox run on a remote client system by using Webdriver.
From what I've gathered, I can use the following code to open a Webdriver-controlled instance of Firefox on my local system. The web site I'm testing has an untrusted SSL/TLS certificate, so I need to tell the Firefox driver to accept untrusted certificates. This works great locally:
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true); // NOTE: this is the default behavior
RemoteWebDriver driver = new FirefoxDriver(profile);
Selenium selenium = new WebDriverBackedSelenium(driver, baseurl);
But I can't figure out how to do this on the remote system using Webdriver. The two approaches seem totally incompatible. The code above does not fit in any way into the following code that I have been using for using Webdriver remotely:
Selenium selenium = new DefaultSelenium(host, port, browser, baseurl);
selenium.start();
Now, I have spent many hours working with custom Firefox profiles on the remote test system. It worked in the summer of 2012, but after recent OS and browser updates, it stopped working. It seems much better to create the Firefox driver profile and call setAcceptUntrustedCertificates(true). Is it possible to use Webdriver to run tests in a browser on a remote system and also have the browser ignore untrusted SSL/TLS certificates?
As mentioned in your question, you don't need to set any property for accepting untrusted certificates explicitly. By default webdriver accepts untrusted certificates. Rather than using a webdriverbacked selenium, you should use the remotewebdriver directly like:
Webdriver wd = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());
Here http://localhost:4444/wd/hub is the URL of the Hub to which tests should be send for execution. When you start the tests, hub will look for remote nodes that have registered with firefox capability.
Personally I would suggest to read documentation at http://code.google.com/p/selenium/wiki/Grid2 rather than seleniumhq.org. As far as I know, selenium team is trying to get the seleniumhq documentation updated. You can also contribute to it :)
First of all i will recommend sticking to webdriver if you are using webdriver backed selenium just for profile. You can define profile to be used on your local machine as
File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1");
WebDriver driver = new FirefoxDriver(firefoxProfile);
To Answer Your Question: I will quote Simon Stewart's solution from here:
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
DesiredCapabilities caps = DesiredCapabilities.firefox();
caps.setCapability(FirefoxDriver.PROFILE, profile);
Use this profile to create the remote driver.
Now if this doesn't work than may be we can write-up a bug (or at least a feature request).
post edit: I can not really test this solution as I dont have a cert issue site readily available. So in a way I would be looking towards you feedback to get the real picture... :)
When I asked this question, I didn't understand the distinction between Selenium objects and WebDriver objects. Even though I was specifically trying to learn about Selenium 2's "WebDriver" feature, I foolishly thought that I could write a "Selenium 2 Webdriver" project with Selenium 2 objects. That may sound obvious to someone who has experience with these tools, but that distinction was still not clear in my mind after reading "Selenium 2" books and project documentation.
As I result, I was writing Java code to instantiate a Selenium object to examine a web page, and trying to pass the Selenium object a WebDriver object, in the hope that the test would run on a remote server.
Now it seems clearer: The Selenium and WebDriver projects merged into a new umbrella project named (confusingly) Selenium 2.0, but they are distinct and separate tools within Selenium 2. If I want to use the WebDriver API, it seems that I must convert any existing Selenium objects to WebDriver objects. There appears to be no useful interaction between the two tools.
For example, in my project I had the following code. It ran great on my local desktop system's web browser:
Selenium selenium = new DefaultSelenium(host, port, browser, baseurl);
selenium.get(urlPath);
selenium.type(username_field, username);
selenium.type(password_field, password);
selenium.click(login_button);
But I want to be able to run that test on a headless continuous integration server, not my desktop system. I have converted the code to use a WebDriver object instead of a Selenium object. Now it runs on a remote system connected to a Selenium Grid 2 server:
WebDriver driver = new RemoteWebDriver(new URL("http://10.0.0.29:4444/wd/hub"), capability);
driver.get(urlPath);
driver.findElement(By.name(username_field)).sendKeys(username);
driver.findElement(By.name(password_field)).sendKeys(password);
driver.findElement(By.className(login_button)).submit();
I hope other people wanting to learn how to use WebDriver in Selenium 2 will not waste as much time as I did reading about Selenium objects, thinking that the Selenium object is part of WebDriver. My current [n00b] advice is to ignore anything that mentions Selenium objects, and focus purely on finding out as much as you can about WebDriver objects. A good place to start is the WebDriver documentation at SeleniumHQ.org:
http://seleniumhq.org/docs/03_webdriver.jsp#webdriver-and-the-selenium-server
As A.J. suggested in his answer, also take a look at the Selenium Grid documentation:
http://code.google.com/p/selenium/wiki/Grid2
And PS: a remote Selenium 2 Webdriver instance accepts untrusted SSL/TLS certificates automatically, by default. No code required.