How to run headless mode in chrome driver using java - java

Here is my chrome driver code:-
String driverPath = driverFile.getAbsolutePath();
System.setProperty("webdriver.chrome.driver", driverPath);
Callable<ChromeDriver> startChromedriver = new Callable<ChromeDriver>() {
public ChromeDriver call() {
ChromeOptions Chromeoptions = new ChromeOptions();
Chromeoptions.addArguments("--startMaximized");
caps.setCapability("newCommandTimeout", 300);
caps.setCapability(ChromeOptions.CAPABILITY, Chromeoptions);
return new ChromeDriver(caps);
}
};
I have started the following in terminal:-
Xvfb -ac :99 -screen 0 1280x1024x16 &
export DISPLAY=:99
and then started my Junit test in Intellij
How do I run junit test in java using chrome driver on ubuntu machine?
For Firefox, I have tried and its working.
apt-get update
sudo apt-get install xvfb
sudo apt-get install -y xorg xvfb dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic
sudo Xvfb :2 -ac
export DISPLAY=:2
Setup in firefox
// Setup firefox binary to start in Xvfb
String Xport = System.getProperty(
"lmportal.xvfb.id", ":2");
final File firefoxPath = new File(System.getProperty(
"lmportal.deploy.firefox.path", "/usr/bin/firefox"));
FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
firefoxBinary.setEnvironmentProperty("DISPLAY", Xport);
// Start Firefox driver
WebDriver driver = new FirefoxDriver(firefoxBinary, null);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://google.com/");
System.out.println("page source" + driver.getCurrentUrl());

Since Chrome 59 you don't even need Xvfb.
Download Chrome driver here : https://chromedriver.storage.googleapis.com/index.html?path=2.38/
Or on macOS :
brew install chromedriver
Then add a recent version of Selenium in your pom.xml/graddle:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
Of course you will need a Chrome version > 59
And here comes the Java part:
String chromeDriverPath = "/Path/To/Chromedriver" ;
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);
I wrote a blog post with detailed instruction here :
https://ksah.in/introduction-to-chrome-headless/

use redwoodHQ server and agent for Linux .then BrOwse to server to drive agent .

Related

ChromeDriver does not handle ssl certificates when running in headless mode

Below driver works fine when i remove headless option but when i include it the test fails. Using chromedriver version 2.36.540470
public WebDriver createDriver() {
System.setProperty("webdriver.chrome.driver", "C:\\Dev\\tools\\chromedriver.exe");
final ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("useAutomationExtension", false);
chromeOptions.addArguments("window-size=1900x1200");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--headless");
final WebDriver driver = new ChromeDriver(chromeOptions);
return driver;
}
Turns out chromedriver running headless does not deal with ssl certificates very well (current open issue with them). Switching to gecko driver fixed the issue.

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally

I am trying to Automate a regular procedure using Java with Selenium. I tried to use Chrome as my browser. But I am getting an error and can't able to load the browser.
Below is the error:
Starting ChromeDriver (v2.3) on port 52793 Exception in thread "main"
org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally (Driver info: chromedriver=2.3,platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 22.50 seconds Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
Chrome version used: Version 63.0.3239.132 (Official Build) (64-bit)
Code used:
public static void main(String[] args) throws Throwable {
System.setProperty("webdriver.chrome.driver", "D:\\Automation\\Thomas_Auto\\Driver\\chromedriver.exe");
ChromeDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://vrijuit.nl/nl/vakanties");
driver.findElement(By.cssSelector("i.basic-sprite.date")).click();
driver.findElement(By.xpath("(//a[contains(text(),'1')])[56]")).click();
driver.findElement(By.cssSelector("a.ui-state-default.ui-state-active.ui-state-hover")).click();
driver.findElement(By.linkText("info & prijs")).click();
driver.findElement(By.cssSelector("input.btn.btn-select")).click();
driver.findElement(By.cssSelector("button.btn-call-to-action")).click();
enter image description here
Update your ChromeDriver and chrome to latest version and try again.
I ran your code with ChromeDriver version 2.35 and chrome Version 64.0.3282.140 (Official Build) (64-bit) on my environment and there was no issue.
You can disable extensions via chromeoptions.
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
ChromeDriver driver=new ChromeDriver(options);
It seems your ChromeDriver doesn't support driver.manage().window().maximize();
Try ChromeOptions to achieve the same ...e.g.
System.setProperty("webdriver.chrome.driver","D://Automation/Thomas_Auto/Driver/chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("start-maximized");
driver = new ChromeDriver(chromeOptions);

Error : 1506741262570 Marionette INFO Listening on port 2828 using Selenium

I want to open Google using Selenium using Firefox. The firefox version I am using is 52.3.0 (64-bit). I am trying in this way;
System.setProperty("webdriver.gecko.driver","C://geckodriver-v0.19.0-win64_2//geckodriver.exe"); // Setting GECKODRIVER
WebDriver WD = new FirefoxDriver();
WD.get("http://www.google.com");
But on running the program, it goes to sleep and the output comes as;
1506741259735 geckodriver INFO geckodriver 0.19.0
1506741259744 geckodriver INFO Listening on 127.0.0.1:31605
1506741260475 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\SPERID~1\\AppData\\Local\\Temp\\rust_mozprofile.viiF05x2u2Ct"
1506741262570 Marionette INFO Listening on port 2828
What's wrong here? I am unable to understand that why it is working as?
Use this to define the Firefox version you want to use.
Most of the issue I have faced trying Selenium is around version issues, with no clear messages or info on which versions are compatible.
System.setProperty("webdriver.firefox.bin", "/path/to/another/firefox/dot/exe");
Assuming you are using the latest Selenium-Java client v3.6.0 and geckodriver v0.19.0, while mentioning the absolute path of the geckodriver.exe you need to use either the single forward slash (/) or the escaped back slash (\\) as follows:
System.setProperty("webdriver.gecko.driver", "C:/Utility/BrowserDrivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://stackoverflow.com");
System.out.println("Application opened");
System.out.println("Page Title is : "+driver.getTitle());
driver.quit();
I believe the problem is the Firefox version.
I downloaded Firefox ESR (52.3.0) for use with Selenium IDE, but when I used WebDriver it was also automatically using the ESR version of Firefox and failing in the way you describe.
When I uninstalled Firefox ESR version, WebDriver automatically used Firefox 55.0.3 and it worked fine.
Edit: I decided I still wanted ESR, so I re-installed it and changed the default install path from "C:\Program Files\Mozilla Firefox" to "C:\Program Files\Mozilla Firefox ESR" therefore Selenium WebDriver couldn't find it easily and used the newer version of Firefox.
Try setting the Marionette capability to true,
FirefoxOptions options = new FirefoxOptions();
options.setCapability("marionette", true);
WebDriver WD = new FirefoxDriver(options);

Standalone chrome.exe connection to selenium test

As you can find at cannot stop Chrome from updating from 43 to 44 I am having problem with chrome 44, as RobW suggested (in comments) i got a stand alone chrome.exe which has the version that I need, but now I do not know how to connect it to my test application which is written by Selenium & JAVA.
You need to point the "binary" to your standalone Chrome using ChromeOptions:
ChromeOptions options = new ChromeOptions()
options.setBinary(new File("/path/to/chrome"));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

ChromeDriver blocks and does not fail after unexpected Alert box

I am running selenium-server-standalone-2.17.0 (for IE and Firefox) and ChromeDriver 18.0.1022.0 (standalone) on a test box (Windows 7 64bit) which I use to run Java selenium tests against.
For some reason, when running my tests against ChromeDriver, the first time it encounters an unexpected Alert box, it blocks idefinitely and the ChromeDriver log says
WARNING: Executing: executeScript
I configured ChromeDriver using the guide http://code.google.com/p/selenium/wiki/ChromeDriver and set the timeout of all the drivers with
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
Update:
I figured out how to initialize the remote ChromeDriver in a clean way with
URL url = new URL("http://192.168.1.15:4444/wd/hub");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
Webdriver chromeDriver = new RemoteWebDriver(url, capabilities);
this ran best with the URL pointing to a selenium-server running chromedriver in a child process. You can make selenium-server run the ChromeDriver by starting it like this:
java -jar C:\selenium-server.jar -Dwebdriver.chrome.driver=C:\path\to\chromedriver.exe
I still have the same problem with Chrome getting stuck at the unexpected Alert box, but the selenium log gave me at bit more info:
INFO - Done: /session/1328623219287/element/253/click
INFO - Executing: [execute script: return !!document['readyState'];, []] at URL: /session/1328623219287/execute)
Still have no idea what is causing this... can anyone help?
This is how I initialize ChromeDriver:
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY,
"PathToWhereChromeDriverIsAvailable");
ChromeDriverService service = ChromeDriverService.createDefaultService();
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
ChromeDriver cd = new ChromeDriver(service, options);
With the Alert() i have just plain guess - probably it hangs out while executing the script - so basically you are not waiting for page to load, but for script to end executing. However, I do not have solution for this...

Categories