Issue while opening headless chrome in remote jenkins server using selenium java - java

I am having trouble in using headless chrome on jenkins remote server. the test is failed at the time of url invoking or takes to much of a time like 5+mins and test failed as it wont have waiting time for that long, i have tried different chromeoption like disable gpu, nosandbox etc still the issue is not solved.There is no issue in opening those url in normal mode but its not working on headless mode. i am using this chromeoptions for test:
enter code here
``` chromeOptions.addArguments("--window-size=1366,768");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.addArguments("--proxy-server='direct://'");
chromeOptions.addArguments("--proxy-bypass-list=*");
chromeOptions.addArguments("--start-maximized");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("no-sandbox", "--disable-infobars", "--disable-dev-shm-usage", "--disable-browser-side-navigation",
"--ignore-certificate-errors");```

Related

Run selenium with cache on centos/linux

I'm able to run selenium on non GUI centos/linux machine in headless mode.
I have been trying to run it with cache enable by passing below chromeoptions arguments.
chromeOptions.addArguments("user-data-dir=~/.config/google-chrome");
It has started fine and identified elements till login page(which is first page) and couldn't identify any locators after that.
Is it the right approach to run cache enabled selenium run?
It's not that super clear when you mention about executing your tests with cache enabled. However, adding the argument user-data-dir is the canonical way to use a specific Chrome Profile.
You can find a couple of detailed discussions in:
How to open a Chrome Profile through Python
How to use Chrome Profile in Selenium Webdriver Python 3
Adding those options helps me to prevent crashes and errors on a linux remote machine
ChromeOptions options = new ChromeOptions();
options.addArguments(
"--disable-gpu",
"--headless",
"--window-size=1920,1200",
"--ignore-certificate-errors",
"--disable-extensions",
"--no-sandbox",
"--disable-dev-shm-usage",
"--hide-scrollbars",
"--allow-running-insecure-content",
"--disable-infobars",
"--ignore-certificate-errors");
Webdriver driver = new ChromeDriver(options);

Chrome headless is not working with specific profile, it is picking random default profile

I have a problem with running my selenium test in headless mode with some specific profile (with extensions).
without headless, the script is working file (with specific profile), but with the headless mode, it is picking default profile which resulted in the failure of the script.
Tried some ways to solve this issue, but nothing worked.
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
chromeOptions.addArguments("--remote-debugging-port=23456"); - this resulted in error at this port.
code setup for starting chrome browser in headless mode.
chromeOptions.setExperimentalOption("excludeSwitches",
Arrays.asList("disable-sync", "enable-logging"));
chromeOptions.addArguments("--enable-sync");
chromeOptions.addArguments("--disable-logging");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--window-size=1280,800");
chromeOptions.addArguments("--allow-insecure-localhost");
chromeOptions.addArguments("--remote-debugging-port=45447");
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
Adding extensions are not supported with headless run.
So, used "Xvfb" virtual frame buffer to mock the chrome UI.
Remove "--headless" while starting the browser instance.

Unable to sendkeys/click with selenium Java or JavascriptExecutor

These are the steps that my code is running.
I start the chromedriver with the secure shell appt - no issues, it launches the browser and appt correctly
chromeOptions.addExtensions(new File("src/test/resources/win32/browserprofile/Secure-Shell-App_v0.8.43.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
I then navigate using driver get to the chrome URL setup page to send connection data.
driver.get("chrome-extension://pnhechapfaindjhompbnflcldabbghjo/html/nassh.html");
While on this page from the image below, I tried to send keys or click on any of the fields with sendkeys or click and I get the following error.
I have tried multiple ways and im getting the same results: org.openqa.selenium.WebDriverException: unknown error: Cannot set property 'value' of null
Here is my code
//Webdriver
driver.findElement(By.xpath("//*[#id='field-description']")).sendKeys("aabb");
driver.findElement(By.id("field-username")).click();
driver.findElement(By.id("field-username")).sendKeys("useridval");
driver.findElement(By.id("field-hostname")).click();
driver.findElement(By.id("field-hostname")).sendKeys("10.0.0.0");
//JavascriptExecutor
// This will execute JavaScript in your script
((JavascriptExecutor)driver).executeScript("document.getElementById('field-username').value='migsrcrfuser';");
Question: Is this even possible, I see an id and the id is unique; furthermore, I also tried xpath and received the same result. Thoughts
Breift description: Terminal emulator and SSH client.
Secure Shell is an xterm-compatible terminal emulator and stand-alone ssh client for Chrome. It uses Native-Client to connect directly to ssh servers without the need for external proxies.
https://chrome.google.com/webstore/detail/secure-shell-extension/iodihamcpbpeioajjeobimgagajmlibd
Update your ChromeDriver to 2.37 (the latest) from https://sites.google.com/a/chromium.org/chromedriver/downloads
I think that you are using Chrome v65
The issue was frame related. I used this code to identified how many frames and then send keys to the correct frame. phew!
int size = driver.findElements(By.tagName("iframe")).size();
System.out.println(size);
driver.switchTo().frame(1); // Switching the inner Frame with 1 0 for outer fram
driver.findElement(By.xpath("//*[#id='field-description']")).sendKeys("9109");

why we need to download browser(IE,Chrome) drivers

In order to execute test scripts on diff browsers, we download specific browser driver from seleniumhq.com and run the scripts.
This code works fine as it should:
System.setProperty("webdriver.ie.driver", "C:\\Users\\Public\\CIO\\resources\\iedriver.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("www.mywebsite.com");
When I already have IE Executable -'iexplore.exe' on my machine, can't selenium webdriver use that to launch IE?
Why do we specifically download IEDriver from seleniumhq.com here?
C:\Program Files\Internet Explorer\iexplore.exe
I tried to even setup that up and ran the program. Browser got launched with address as --port=1234/ however it could not navigate to respective website and eventually threw exception:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException:
Could not start a new session. Possible causes are invalid address of the remote server or
browser start-up failure.
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:18'
The "Browser Drivers" are servers that implement the WebDriver's wire protocol, and know how to convert those commands into the specific browser's proprietary native API.
The WebDriver site explains:
Selenium-WebDriver makes direct calls to the browser using each
browser’s native support for automation. How these direct calls are
made, and the features they support depends on the browser you are
using.
For example, the ChromeDriver wiki describes it as follows:
The ChromeDriver consists of three separate pieces. There is the
browser itself ("chrome"), the language bindings provided by the
Selenium project ("the driver") and an executable downloaded from the
Chromium project which acts as a bridge between "chrome" and the
"driver".
Essentially, the browser doesn't know how to "talk" WebDriver Wire Protocol, and the WebDriver doesn't know how to "talk" Browser API. In fact, each Browser has its own native API. The "browser driver" knows how to interpret the Wire Protocol and invoke that browser's API.

Set Jenkins Windows Slave Screen Resolution For Selenium Tests

I got a Jenkins windows 7 slave for testing my UI webapp using Selenium WebDriver (java, with Firefox and chrome drivers; machine is vmware vm).
Also, slave is connected to jenkins master using JNLP agent.
Problem is: Tests fail on slave, since resolution seems to be 800x600 whereas I need 1920x1080-ish.
Setting selenium web driver windows size won't do it, i.e.
driver.manage().window().setSize(new Dimension(1920, 1080));
driver.manage().window().maximize();
Any ideas? I'm trying to figure out where display resolution configuration should be done. Should it be done on JVM side (as test is execute by jenkins slave)? Or is it a windows issue?
We had a similar issue and we resolved it by setting up the Jenkins slave on windows an admin session of RDP started from a PC with a 1920x1080 resolution screen.
Start -> run -> mstsc /admin
This opens the Jenkins slave in the desired resolution.
Then, you just disconnect the RDP session and this leaves the session running in the desired resolution.
I hope this helps.
The display setup should be a one time activity performed on slave. You will have to remote into slave as admin using RDP to set resolution.
I had similar issue, then I used Phantom JS driver instead of Chrome
try this:
driver.manage().window().maximize();
driver.manage().window().setSize(new Dimension(1936, 1176));
Problem resolved from my end
Solution that worked for me is to run Chrome in a 'headless' mode (without GUI). It works with Jenkins Agent running as a service, when GUI is not available.
Here is the code sample of the web driver initialization (C#, I guess will be similar in java):
var options = new ChromeOptions();
options.BinaryLocation = #"C:\Program Files\Google\Chrome\Application\chrome.exe";
options.AddArgument("Headless");
options.AddArgument("window-size=1920,1080");
driver = new ChromeDriver(#"<path>\Selenium.WebDriver.ChromeDriver.2.37.0\driver\win32", options);

Categories

Resources