https://code.google.com/p/selenium/issues/detail?id=3175
Doesn't work.
So then I tried this,
ChromeOptions opts = new ChromeOptions();
opts.addArguments("--disable-javascript");
driver = new ChromeDriver(opts);
But then driver.get(website);
javascript is enabled again. When it was on data; it was disabled.
Also I tried,
DesiredCaptabilities caps = DesiredCaptabilties.chrome();
caps.setJAvaScriptEnabled(fale);
driver = new ChromeDriver(caps);
driver.get(Website);
Nothing is working. Any advice?
javascriptEnabled just works on HTMLUnitDriver.
And ChromeDriver should have JavaScript enabled to work properly in the first place, so you canĀ“t disable JavaScript if you use ChromeDriver2.
static public void DisableJS () {
driver.get("chrome://settings");
driver.switchTo().frame("settings");
driver.findElement(By.id("advanced-settings-expander")).click();
driver.findElement(By.id("privacyContentSettingsButton")).click();
//here do not allow js
driver.findElement(By.xpath("//*[#id='content-settings-page']/div[2]/section[3]/div/div[2]/label/input")).click();
driver.findElement(By.id("content-settings-overlay-confirm")).click();
}
Related
Hello guys please how can i do it?When i run not headless mode browser is in english and everything works fine, but when i run headless mode language is changed to my native language.
I am using this for headless mode.
Configuration.headless = true;
Selenide Configuration class contains
public static MutableCapabilities browserCapabilities which used within the driver startup if provided.
For Chrome:
ChromeOptions options = new ChromeOptions()
.setHeadless(true)
.addArguments("--lang=en_US");
Configuration.browserCapabilities = options;
But note --lang argument might be ignored on Linux.
For Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "en-US");
FirefoxOptions options = new FirefoxOptions()
.setHeadless(true);
.setProfile(profile);
Configuration.browserCapabilities = options;
I am setting up a chrome driver with the help of Selenium and Java. I want this driver to be executed headless but I can't find out the way. Can you explain to me what do I need to do?
My code sample:
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICITY_TIME, TimeUnit.SECONDS);
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH); // OS and Browser options
ChromeOptions options = new ChromeOptions(); // create options instance
options.addArguments("--headless"); // add an option
driver = new ChromeDriver(options); // create a driver with the specific options instance
You just need to create a ChromeOptions object in which you need to save the options for your own driver.
To add your own options just use this: options.addArguments(); and in the parenthesis insert your option in string mode.
For more details and documentation please also check here:
http://chromedriver.chromium.org/capabilities
I think this is going to work.
This declaration
WebDriver driver = new FirefoxDriver();
always opens a new instance window of Firefox. It doesn't use the already opened firefox.
Can anyone let me know how to use a already opened firefox for testing instead of opening a new one?
Use Remote Web Driver
like this .
System.Uri uri = new System.Uri("http://localhost:7055/hub");
WebDriver = new RemoteWebDriver(uri, DesiredCapabilities.Firefox());
it will use the already opened Firefox browser.
you can see the details of this approach in this blog post.
http://www.binaryclips.com/2016/03/selenium-web-driver-in-c-how-to.html
Be careful with that, because in case the driver crashes once, then all the test cases that have to be executed after that will be affected because they are using the same driver, also you will be sharing cookies, and perhaps sessions already opened previously, etc.
The more robust solution is to create a new WebDriver for each test cases because doing that you are making all your tests cases less dependent on the others.
If the reason that is motivating you is the time each WebDriver takes to be created, perhaps you could start thinking on run test cases in parallel for example with TestNG.
Thanks
Best way to do that is, extend RemoteWebDriver and override startSession method-:
Steps:
Start selenium server using command- java -jar selenium-server-standalone-3.x.x.jar. By default your session start on port 4444.
open url http://localhost:4444/wd/hub/static/resource/hub.html
start new firefox session clicking on create session button and select firefox browser.
Once the session start, copy the session id and paste it in property file or xml file where you want.
read session id form the file where you saved in following method
#Override
protected void startSession(Capabilities desiredCapabilities) {
String sid = getSessionIDFromPropertyFile();
if (sid != null) {
setSessionId(sid);
try {
getCurrentUrl();
} catch (WebDriverException e) {
// session is not valid
sid = null;
}
}
if (sid == null) {
super.startSession(desiredCapabilities);
saveSessionIdToSomeStorage(getSessionId().toString());
}
}
You should instantiate your webdriver only once, when making a test and then pass it as argument for the other classes in constructors. Something like this:
public class Test {
WebDriver driver = new FirefoxDriver();
#Test
public void testHomePage() {
HomePage hp = new HomePage(driver);
//code here }
}
public class HomePage{
private static WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;}
}
In Java, when you say new a new object is instantiated. For WebDriver, every new is a new browser window.
If you want the same browser to be used then use the same driver object.
driver.get("URL PATH");
This will go to the new Url with the already open browser.
Java example.
First, you need to have Selenium server running.
java -jar C:\selenium-server-standalone-2.53.0.jar
To start a new session (first script):
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
Then, to reuse (attach) that session (second script):
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:7055/hub"),
DesiredCapabilities.firefox());
Notice the different port number.
I've been looking for a way to set the driver preferences for chrome driver using java for the past two days with no luck.
I have however found a solution in ruby VIA RubyBindings and would like to know if there is a java equivalent line I can use for this.
The ruby code is the following:
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = "/path/to/dir"
driver = Selenium::WebDriver.for :chrome, :profile => profile
While searching I found that chrome does not have a profiler I could use like the FirefoxProfile class, so I started using the DesireCapabilities class instead. After further investigation into this problem I found that I could set the "switches" and "prefs" VIA capabilities.setCapabilitiy and ended up with the following:
Map<String, String> prefs = new Hashtable<String, String>();
prefs.put("download.prompt_for_download", "false");
prefs.put("download.default_directory", "/path/to/dir");
prefs.put("download.extensions_to_open", "pdf");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.prefs", prefs);
dr = new ChromeDriver(capabilities);
However I was not able to get this working, the default download directory was never changed to the specified directory once started. I am unsure if there is a problem with how I am trying to set this capability or if the problem lies elsewhere.
In the end I eventually used the solution proposed here:
http://dkage.wordpress.com/2012/03/10/mid-air-trick-make-selenium-download-files/
but I would like to know if it is possible to do this more cleanly but just setting the preferences directly instead of using the UI
Any help is appreciated, Thanks!
Update:
Surprisingly after updating Selenium 2 to version 2.24.1 (and to windows chrome 22), the code above with the Maps work as expected, the only problem now is that they deprecated the the use of the constructor ChromeDriver(DesiredCapabilities capabilities), and instead recommend I use the ChromeOptions class, which I cannot get working for the above scenario.
Below is the wiki page explaining the use of both ChromeOptions and DesiredCapabilities:
http://code.google.com/p/chromedriver/wiki/CapabilitiesAndSwitches
The Ruby bindings actually expands that to:
{
"download": {
"prompt_for_download": false,
"default_directory": "/path/to/dir"
}
}
Try building your Java prefs object like that and see if it works. The string vs boolean false could also be an issue.
Try this (Forgive my java which is quite rusty, but hopefully you get the idea)
Dictionary download = new Dictionary();
download["default_directory"] = "/path/to/dir";
Dictionary prefs = new Dictionary();
prefs["browser"] = download;
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.prefs", prefs);
WebDriver driver = new ChromeDriver(capabilities);
Update: I just browsed the code and it seems that what I suggested above probably won't work. The ruby chrome profile class creates zip file with chrome profile file structure in it to support chrome preference. I couldn't find such facility code in java. There is a Firefox profile in java that does the simliar thing for firefox, but obviously that won't work for chrome. So in short, this feature is not supported yet in java.
Newer versions (I tested Chrome 44.0.2403.125, Selenium 2.47.1, and ChromeDriver 2.17.340128) work with the following:
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "/path/to/directory");
options.setExperimentalOption("prefs", prefs);
ChromeDriver chromeDriver = new ChromeDriver(options);
I got the following problem:
I am running a JUnit testCase with Selenium 2.9 using HtmlUnitDriver with Browserversion Firefox_3_6. JavaScript is enabled. Now when it should call and execute the following javaScript function it does nothing:
function openIdsDocument()
{
var windowBounds = getWindowBounds();
var XMLHTTP = getAjaxRequestObject("XYZ.do?availableWidth="+windowBounds.width+"&availableHeight="+windowBounds.height, "", true);
if (XMLHTTP != null)
{
XMLHTTP.onreadystatechange = function alertAJAXResponse()
{
if (XMLHTTP.readyState == 4)
{
window.location.href = getContextPath() + "ABC.do";
}
};
XMLHTTP.send("timestamp=" + <%=System.currentTimeMillis()%>);
}
getLoadingState();
}
I want to get to ABC.do
If I execute my test with the FirefoxDriver it works.
Is there a way to get this working with HtmlUnitDriver?
My test works if I manually call driver.get("http://host/ABC.do") but that cannot be the right way to do this.
You can enable JavaScript by doing either
new HtmlUnitDriver(true);
driver.setJavascriptEnabled(true);
What you need to do is to wait until the JavaScript is executed after get(url).
You can use Thread.sleep() method for adding some delay.
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
driver.setJavascriptEnabled(true);
driver.get(url);
Thread.sleep(100);
runTest();
Update
As #Corey indicated in the comments, it could be nicer to use Explicit and Implicit Waits instead of Thread.sleep(). As I don't use them these days, I cannot confirm, though. It would be great if someone test them and update this answer.
You need to initialize the HtmlUnitDriver with enable javascript true
new HtmlUnitDriver(true);
If you wish to set the BrowserVersion as well as enable Javascript with HtmlUnitDriver, your initialization needs to look like the following (as there is no way to do both via the constructor):
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
driver.setJavascriptEnabled(true);
This will allow you to use the browser definition of your choice and use Javascript.
You may need to do this:
WebDriver driver = new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_8);
((HtmlUnitDriver) driver).setJavascriptEnabled(true);
Well, There is an easy way to enable browser capability and javascript, you can do the following:
Webdriver driver = new HtmlUnitDriver(BrowserVersion.Chrome,true);
True specifies that javascript should be enabled. #Glenn Nelson,