Tests slow because High cpu using Headless chrome with version 87 - selenium-chromedriver

When I run tests without using headless chrome, the tests take on average 40-50 seconds. When I run the tests using headless chrome it takes a lot longer (190 seconds on average). I'm using chromedrivermanager which runs on 87.0.4280.20.
The browser class without using headless:
private String baseUrl = ConfigHandler.getPropertyValue("url");
private WebDriver driver;
public Browser() {
WebDriverManager.chromedriver().setup();
Map<String, Object> prefs = new HashMap<>();
ChromeOptions chromeOptions = new ChromeOptions();
String FilesPath = System.getProperty("user.dir") + File.separator + SeleniumUtilities.getDownloadsPath();
prefs.put("download.default_directory", FilesPath);
chromeOptions.setExperimentalOption("prefs", prefs);
this.driver = new ChromeDriver(chromeOptions);
this.driver.manage().window().maximize();
}
Browser class using headless chrome:
private String baseUrl = ConfigHandler.getPropertyValue("url");
private WebDriver driver;
public Browser() {
WebDriverManager.chromedriver().setup();
Map<String, Object> prefs = new HashMap<>();
ChromeOptions chromeOptions = new ChromeOptions();
System.out.println("working on server");
chromeOptions.addArguments("--window-size=1400,900");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--no-proxy-server");
chromeOptions.addArguments("--proxy-server='direct://'");
chromeOptions.addArguments("--proxy-bypass-list=*");
String FilesPath = System.getProperty("user.dir") + File.separator + SeleniumUtilities.getDownloadsPath();
prefs.put("download.default_directory", FilesPath);
chromeOptions.setExperimentalOption("prefs", prefs);
this.driver = new ChromeDriver(chromeOptions);
}
Any Ideas?
EDIT:
I noticed in headless chrome the cpu get really high, unlike without using headless. Why the cpu so high when running in headless?

Related

chromeOptions.add_experimental_option("prefs", prefs) is not working when setting up default downloads directory

I am using the following for setting up downloads directory.
chromeOptions = Options()
prefs = {"download.default_directory" : "F:/downloadedfiles"}
chromeOptions.add_experimental_option("prefs", prefs)
#chromeOptions.add_argument("download.default_directory=F:/downloadedfiles")
driver = webdriver.Chrome(executable_path=r"servers\chromedriver.exe", options=chromeOptions)
But when I run the program, it is still downloading in "C:\Downloads" directory.
Try This:
ChromeOptions chromeOptions = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "F:/downloadedfiles");
(OR)
ChromeOptions chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "F:/downloadedfiles"}
Set Chrome Options and Driver
chromeOptions.setExperimentalOption("prefs", prefs);
chromedriver = "path/to/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver, options=chromeOptions)
Reference : https://sites.google.com/a/chromium.org/chromedriver/capabilities

How to download a file with Selenium and ChromeDriver

I have a website to test with selenium and ChromeDriver (on windows), where I like to test the functionality to export data and import it again.
The export creates a xml file that is downloaded on ones computer. When running this with webdriver, Chrome asks me whether to keep the file or discard it, as it might be a potential threat.
How can I switch off this behavior inside my test ? Is there a chrome setting I can use, so that a file is no matter what downloaded ?
Thanks
Try this. Executed on windows
(How to control the download of files with Selenium Python bindings in Chrome)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Users\xxx\downloads\Test",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
The below Program will help you to download the files in Chrome with
Desired-Capabilities. It is a rich class having lot of utilities, you can go through it in your free time.
public class DownloadChromeFile {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
String downloadFilepath = "c:\\download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
options.addArguments("--disable-extensions"); //to disable browser extension popup
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); // Bydefault it will accepts all popups.
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);
driver.get("Your Application Url");
driver.findElement(By.xpath("Export Button xpath")).click();
}
}

Setting device name in serenity.properties file for chrome driver

How do I set the mobile emulation for Nexus 5 view in Serenity managed chrome driver?
I tried going through this link:
https://johnfergusonsmart.com/configuring-chromedriver-easily-with-serenity-bdd/
Which explain setting preferences for chrome.
Chrome preferences
You can also provide more advanced options using the setExperimentalOption() method:
Map<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downLoadDirectory);
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("pdfjs.disabled", true);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
In Serenity, you would pass these using properties prefixed with the chrome_preferences prefix, e.g.
chrome_preferences.download.default_directory = /my/download/directory
chrome_preferences.profile_default_content_settings.popups = 0
chrome_preferences.pdfjs.disabled=true
From this, I tried setting the mobileEmulation as
chrome.capabilities.mobile_emulation.device_name= Google Nexus 5
chrome.options.mobileEmulation.deviceName= Google Nexus 5
and a few other logical variants, but none of them succeeded.
The best way I found to help me in this issue it to create a custom WebDriver.
I had to create a class which extends DriverSource. And then link it to the Serenity Properties file. This will give me the driver I need.
http://www.thucydides.info/docs/serenity/#_custom_webdriver_implementations
You can add your own custom WebDriver provider by implementing the
DriverSource interface. First, you need to set up the following system
properties (e.g. in your serenity.properties file):
webdriver.driver = provided
webdriver.provided.type = mydriver
webdriver.provided.mydriver = com.acme.MyPhantomJSDriver
thucydides.driver.capabilities = mydriver
Your custom driver must implement the DriverSource interface, as shown
here:
public class MyPhantomJSDriver implements DriverSource {
#Override
public WebDriver newDriver() {
try {
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
// Add
return new PhantomJSDriver(ResolvingPhantomJSDriverService.createDefaultService(),
capabilities);
}
catch (IOException e) {
throw new Error(e);
}
}
#Override
public boolean takesScreenshots() {
return true;
}
}
This driver will now take screenshots normally.

Export HAR using chromedriver

Is it possible to export HAR using chromedriver similar to what I can do with netexpert+firebug with Firefox?
Yes, using BrowsermobProxy you can generate HAR file using chromedriver.
Here is a script in python to programatically generate HAR file using Selenium, BrowserMob Proxy and chromedriver. Python Packages for selenium and browsermob-proxy are needed to run this script.
from browsermobproxy import Server
from selenium import webdriver
import os
import json
import urlparse
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()
chromedriver = "path/to/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
url = urlparse.urlparse (proxy.proxy).path
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(url))
driver = webdriver.Chrome(chromedriver,chrome_options =chrome_options)
proxy.new_har("http://stackoverflow.com", options={'captureHeaders': True})
driver.get("http://stackoverflow.com")
result = json.dumps(proxy.har, ensure_ascii=False)
print result
proxy.stop()
driver.quit()
You can enable performance log via chromedriver and analyze the network traffic to build HAR on your own.
Please checkout the code at
https://gist.github.com/Ankit3794/01b63199bd7ed4f2539a088463e54615#gistcomment-3126071
Steps:
Initiate ChromeDriver instance with enabling Logging Preference
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("ignore-certificate-errors");
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("start-maximized");
// More Performance Traces like devtools.timeline, enableNetwork and enablePage
Map<String, Object> perfLogPrefs = new HashMap<>();
perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools");
perfLogPrefs.put("enableNetwork", true);
perfLogPrefs.put("enablePage", true);
chromeOptions.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
// For Enabling performance Logs for WebPageTest
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
capabilities.setCapability("goog:loggingPrefs", logPrefs);
capabilities.merge(chromeOptions);
Get "message" JSONObject from Performance Logs
private static JSONArray getPerfEntryLogs(WebDriver driver) {
LogEntries logEntries = driver.manage().logs().get(LogType.PERFORMANCE);
JSONArray perfJsonArray = new JSONArray();
logEntries.forEach(entry -> {
JSONObject messageJSON = new JSONObject(entry.getMessage()).getJSONObject("message");
perfJsonArray.put(messageJSON);
});
return perfJsonArray;
}
Get HAR by passing PerfLogs
public static void getHAR(WebDriver driver, String fileName) throws IOException {
String destinationFile = "/HARs/" + fileName + ".har";
((JavascriptExecutor) driver).executeScript(
"!function(e,o){e.src=\"https://cdn.jsdelivr.net/gh/Ankit3794/chrome_har_js#master/chromePerfLogsHAR.js\",e.onload=function(){jQuery.noConflict(),console.log(\"jQuery injected\")},document.head.appendChild(e)}(document.createElement(\"script\"));");
File file = new File(destinationFile);
file.getParentFile().mkdirs();
FileWriter harFile = new FileWriter(file);
harFile.write((String) ((JavascriptExecutor) driver).executeScript(
"return module.getHarFromMessages(arguments[0])", getPerfEntryLogs(driver).toString()));
harFile.close();
}

How to handle SSL errors for chrome and internet explorer in selenium Webdriver testing?

How we can handle SSL certificate errors for chrome and internet explorer with selenium web driver. When I am working with Firefox it is working fine. Could you please provide me the solution to handle SSL certificate error. Below is the code i tried.
// For Chrome
#Test
public void CRconfiguration() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\Selenium softwares\\drivers\\chromedriver.exe");
_driver = new ChromeDriver(capabilities);
System.setProperty("webdriver.chrome.driver",
"D:/Softwares/Selenium softwares/drivers/chromedriver.exe");
//_driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
login();
_driver.close();
}
//For Internet Explorer
#Test
public void IEconfiguration() throws Exception {
System.setProperty("webdriver.ie.driver",
"D:/Softwares/Selenium softwares/drivers/IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setJavascriptEnabled(true);
//capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
_driver = new InternetExplorerDriver(capabilities);
_driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
login();
_driver.close();
}
For Chrome
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\chromedriver.exe");
WebDriver driver1 = new ChromeDriver();
driver1.get("https://www.flipkart.com/co");
driver1.navigate().to("javascript:document.getElementById('overridelink').click()");
For IE:
System.setProperty("webdriver.ie.driver", "D:\\Selenium\\IEDriverServer.exe");
WebDriver driver2 = new InternetExplorerDriver();
driver2.get("https://www.flipkart.com");
driver2.navigate().to("javascript:document.getElementById('overridelink').click()");
WebDriver driver = new 'your Driver'();
driver.get("your app URL");
driver.navigate().to("javascript:document.getElementById('overridelink').click()");