I have a large set of tests running on a site.
When I run the test locally on Windows - they all pass on 100%. The test are designed and running on Google Chrome.
Now, we have started to run the tests on Linux via Jenkins jobs on headless mode. Some tests now fail on 0% or only pass on 20% or even 10%. In my code I'm finding elements by ID, xpath or css and simple click on them. And I use the WebDriverWait object for waiting - both for the element to be present and to be clickable.
Example of my code:
WebDriverWait wait = new WebDriverWait(browser, secondsToWait);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(elementID)));
lastFoundElement = wait.until(ExpectedConditions.elementToBeClickable(By.id(elementID)));
clickLastFoundElement();
In my report I see mostly that the elements were not found and that I passed the timeout set in the wait object.
How to make headless tests be more stable?
Why the headless state causes so many problems?
It is actually known issue to Selenium community that headless is not stable as it should be, read here about the issue more
Chrome runs very unstable when headless mode is activated.
There are multiple different issues and bugs depending on: Chrome Version, ChromeDriver Version and the executed tests.
Issues and Fixes (occured so far):
Chrome does not start in headless mode
Exception:
No informative Error message. Only a timeout exception when navigate() is called on the driver:
org.openqa.selenium.TimeoutException: timeout
Fix:
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
Chrome is very slow in headless mode
Fix:
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
Chrome does not operate correctly after a certain time, when tests run very long resp. many actions are executed in one test session
Exception:
... Timed out receiving message from renderer: ...
Fix (not tested yet!):
options.addArguments("--disable-browser-side-navigation");
You possibly missing setting the headless window size.
Try this settings:
Map<String,String> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadsPath); // Bypass default download directory in Chrome
prefs.put("safebrowsing.enabled", "false"); // Bypass warning message, keep file anyway (for .exe, .jar, etc.)
ChromeOptions opts = new ChromeOptions();
opts.setExperimentalOption("prefs", prefs);
opts.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080","--ignore-certificate-errors","--no-sandbox", "--disable-dev-shm-usage");
driver = new ChromeDriver(opts);
Here I put much more setting used by me for the headless chrome.
I hope this will be useful for you.
Please include screen size in the chrome option.
ChromeOptions options = new ChromeOptions();
options.addArguments(Arrays.asList("--window-position=0,0"));
options.addArguments(Arrays.asList("--window-size=1840,1080"));
Related
Prerequisites:
There is an iframe on page with input fields which I need to get.
Before interacting with fields I must switch to iframe.
Versions
Selenium has version 4.7.2 (I mean artifact with id = selenium-devtools-v108)
standalone-chrome:108.0.5359.124
chromedriver-108.0.5359.71
I faced issue related to switching to iframe. ChromeDriver cannot do this operation and fails with error:
org.openqa.selenium.WebDriverException: org.openqa.selenium.TimeoutException: timeout: Timed out receiving message from renderer: 60.000
Code which doesn't work:
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(
By.cssSelector("#loginFrame")
));
ChromeOptions
options.setExperimentalOption("excludeSwitches", List.of("--enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--disable-extensions");
options.addArguments("--disable-gpu");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--no-sandbox");
I've already tried to downgrade ChromeDriver, Chrome, Selenium - nothing changed.
The most interesting is that tests are retried after fail 3 times and it doesn't work at first time, but it works as expected at second and third time at the same page.
Preventing questions like "Is there iframe?", I may say "Yes, there is". I checked it by separating waiting for element and switching to it: element has been found, but switching couldn't be executed.
And this code works fine on older version of page and sometimes - on current version. But the only difference between versions is changed styles of elements as I see in DOM (source code is external).
And so I have no idea what it is.
It was happening because of frozen request which was in infinite loading and blocked Chrome. Blocking that request has solved my issue.
I'm trying to load a website with Chrome browser in headless mode using Selenium web driver. I face an issue with some specific websites. The page is loading, in the first 2-3 seconds it shows a page with "please enable javascript..." and after 3 seconds, page source goes blank.
I'm using Selenium and especially Chrome for long time and I am familiar with the platform. For the purpose of this case, I'm using Chrome Version 73.0.3683.86 , ChromeDriver 2.46.628411 (which is compatible according to Which ChromeDriver version is compatible with which Chrome Browser version?) on a Mac OS. selenium java version is latest - 3.141.59
I suspect that headless Chrome cannot handle specific content-type such as "svg" and any other GUI related HTTP response.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://identity.tescobank.com/login");
Thread.sleep(3000);
System.out.println(driver.getPageSource());
driver.quit();
Expected result is to have the page source same as it is showing in non-headless mode.
Headless Chrome should be able to handle everything the normal Chrome can do:
It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.
(see https://developers.google.com/web/updates/2017/04/headless-chrome)
Since only the login page of a bank causes you trouble, my guess is that the security of the page detects an anomaly and decides not to serve you.
One way they can do that is by looking at the User Agent string which contains HeadlessChrome.
That said, unless you're writing integration tests for the bank, your behavior is at least suspicious. If you have a valid and legal concern, clear it with the bank first. They might take actions against you, otherwise. Blocking your IP address (which could affect many people) or asking the police to have a word with you.
I was facing similar issue in my script, after login. Somehow refreshing the page resolved the issue.
driver.navigate().refresh();
I'm trying to automate some interactions with our flash app as part of our CI process. I'm running into troubles with enabling flash when running chrome headlessly (via xvfb-run) with Selenium Standalone Server. I've done a lot of searching, but thus far haven't come up with anything that works.
I'm currently using this, but am open to switching to different versions if there's a known working config somewhere...
Selenium Standalone Server 3.11
Chromedriver 2.33
Chrome 65.0.3325.181
Java 8
When I first got this started I would get a warning on the page saying I needed to enable Adobe Flash Player. I got "past" that message by using the following from https://sqa.stackexchange.com/questions/30312/enable-flash-player-on-chrome-62-while-running-selenium-test:
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.plugins", 1);
prefs.put("profile.content_settings.plugin_whitelist.adobe-flash-player", 1);
prefs.put("profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player", 1);
// Enable Flash for this site
prefs.put("PluginsAllowedForUrls", "ourapp.com");
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
driver.get("ourapp.com");
When loading our app, the page now gives a slightly different message which I haven't been able to get past. Is there a way to get around this, or is there any other way to enable Flash by default?
Restart Chrome to enable Adobe Flash Player
Thanks in advance for the help!
Thanks to a coworker for pointing out this post indicating that plugins don't work in headless chrome. https://groups.google.com/a/chromium.org/forum/#!searchin/headless-dev/flash%7Csort:date/headless-dev/mC0REfsA7vo/rKAZdRrCCQAJ
Fortunately in my case I was already using xvfb as a virtual display, so removing the "headless" argument from my ChromeOptions was all I needed to get everything running.
EDIT*
While true that I did not need to run in headless mode while using xvfb, I ended up finding that the real 'solution' (more of a workaround) to my problem was to upload a custom chrome profile into my docker image. Doing so allowed me to set all the preferences I needed, and none of the code posted in the original post is required. Would much prefer to achieve this programatically, but this at least gets me what I need for now. Figured I'd post in case someone else runs into this in the future..
I am working with Java Selenium. I came across the following error:
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.gezinomi.com/");
Picture of error:
This is not an error, since if you click Cancel option, it'll just follow the path normally.
However, it is probably causing the test to fail, once the browser remains waiting the confirmation. You can disable it through Selenium Java code, as already shown here:
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.gezinomi.com/");
That popup was problematic while running tests remotely on SauceLabs. I tried the ChromeOptions argument "--disable-extensions" which didn't work. I gave up, accepting that a chunk of screen would not be visible.
As soon as I stopped researching, I came across this reference which clearly defines the Chrome option as "--disable-extensions-file-access-check".
I don't know why this was so hard to find, but hopefully it can help others waste LESS of their time.
We are using testng with selenium. We can use IE, Firefox or Chrome but almost always use Chrome. At the beginning of each test, a driver instance is obtained (as shown below) depending on the browser. This has been working fine but lately, and then just once, one test failed with the error message "org.openqa.selenium.SessionNotCreatedException: session not created exception
from unknown error: failed to close UI debuggers" I did some searches in Google and other people had similar problems but I did not see a lot of resolution. My guess is that it was some temporary technical glitch or network problem. But I am curious whether anyone has gotten this error, and moreover what it might mean? Not sure whether it helps, but it was developed on Eclipse, and a jar created (with a bat file to execute the tests). The tests were being run from this jar file and only one failed, and only once.
// capabilities for different browsers.
extraCapabilities.setCapability(CapabilityType.PROXY, seleniumProxyObject);
extraCapabilities.setCapability("ie.setProxyByServer", true);
extraCapabilities.setCapability("ie.usePerProcessProxy", true);
extraCapabilities.setCapability("download.default_directory", System.getProperty("java.io.tmpdir"));
// create driver with additional capabilities
webDriver = DriverFactory.getDriverInstance(driver, browser, extraCapabilities);