How to TakeScreenshots Selenium ChromeOption Driver - java

Not able to capture screen shots for electron application using java .
Here is my code for webdriver setup.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/Applications/betlite.app/Contents/MacOS/appname");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new ChromeDriver(capabilities);
Using following method to capture screen , it do work for traditional ChromeDriver .
public static void captureScreenMethod() throws IOException, InterruptedException
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Thread.sleep(4000);
FileUtils.copyFile(scrFile, new File("path_of_file/screenshot.png"));
}
I am getting following error -
org.openqa.selenium.WebDriverException: unknown error: cannot get
automation extension from unknown error: page could not be found:
chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html
I am using MAC OS
Thanks

Related

Selenium - Unable to launch browser java.lang.NoClassDefFoundError: okio/Sink

Issue : When I run browser launch code then getting below error:
Env : Windows
Java 1.8
Selenium 3.141.59
Browser - Chrome 94 ( Correct crome driver used)
#Test
public void test() {
System.setProperty("webdriver.chrome.driver", "resources\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--enable-automation");
options.addArguments("--no-sandbox");
options.addArguments("--disable-extensions");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-gpu");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://test.com");
driver.manage().window().maximize();
System.out.println("Loading the application url");
}
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
java.lang.NoClassDefFoundError: okio/Sink
at okhttp3.RealCall.getResponse(RealCall.java:229)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
at okhttp3.RealCall.execute(RealCall.java:57)
at org.openqa.selenium.remote.internal.OkHttpClient.execute(OkHttpClient.java:103)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:105)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:157)
at Base.BrowserLaunch.test(BrowserLaunch.java:34)
Please let me know if any other details required.
I've faced the same issue some time ago. I have used this code and hope it will help you as well.
Properties p=Someutility.getTestConfig();
System.setProperty("webdriver.chrome.driver", p.getProperty("driverPath"));
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.navigate().to(p.getProperty("navigationUrl"));
driver.manage().window().maximize();
Where getTestConfig() is the method where I get the properties file for use like to get the driverPath and navigationUrl.
Where navigationUrl is the url where my application is running.
Here is the getTestConfig() method for reference.
public static Properties getTestConfig() throws IOException, InterruptedException {
FileReader reader = new FileReader("pathoffile/somefile.properties");
Properties p = new Properties();
p.load(reader);
return p;
}

Downloading PDF using Selenium Java not working in Chrome

I have written the following code to disable the Chrome PDF viewer so that the PDF file can be downloaded automatically in the C:\downloads folder when the link is opened in Chrome.
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", "C:\\downloads");
prefs.put("download.prompt_for_download", false);
prefs.put("plugins.always_open_pdf_externally", true);
options.setExperimentalOption("prefs", prefs);
options.addArguments("--test-type");
options.addArguments("--disable-extensions");
driver = new ChromeDriver(options);
Unfortunately the PDF viewer does not get disabled properly I believe. Here's what I get with this code when I open that PDF url:
Even if I enable the Download PDF files instead of automatically opening them in Chrome, I still get the above result.
Is there any other solution to get the file downloaded automatically in Chrome?
I managed automatic PDF download in Chrome with loading existing browser profile. Maybe you need just a profile without PDF viewer.
public class WebdriverSetup {
public static String chromedriverPath = "C:\\Users\\pburgr\\Desktop\\selenium-tests\\GCH_driver\\chromedriver.exe";
public static String chromeProfilePath = "C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data";
public static WebDriver driver;
public static WebDriver startChromeWithCustomProfile() {
System.setProperty("webdriver.chrome.driver", chromedriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=" + chromeProfilePath);
driver = new ChromeDriver(options);
driver.manage().window().maximize();
return driver;
}
public static void shutdownChrome() {
driver.close();
driver.quit();
}
}

Headless chrome + ignore-certificate-errors

I need to get headless chrome to ignore certificate errors. The option is ignored when running in headless mode, and the driver returns empty html body tags when navigating to an https resource.
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
This is how I am configuring my chrome driver.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
DesiredCapabilities cap=DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
chromeHeadlessDriver = new ChromeDriver(cap);
This thread confirms that --ignore-certificate-errors is ignored in headless mode.
They mention about devtool protocol.
Is it something I can invoke from java? Are there any other alternatives?
There is an excellent article on medium.com by sahajamit
and i have tested the below code, it works perfectly fine with self-signed certificate https://badssl.com/
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--headless", "--window-size=1920,1200","--ignore-certificate-errors");
DesiredCapabilities crcapabilities = DesiredCapabilities.chrome();
crcapabilities.setCapability(ChromeOptions.CAPABILITY, options);
crcapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
crcapabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY, "C:\\temp\\chrome\\chromedriver.log");
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "C:\\temp\\chrome\\chromedriver.exe");
ChromeDriverService service = null;
try {
service = new ChromeDriverService.Builder()
.usingAnyFreePort()
.withVerbose(true)
.build();
service.start();
} catch (IOException e) {
e.printStackTrace();
}
RemoteWebDriver driver = new RemoteWebDriver(service.getUrl(),crcapabilities);
driver.get("https://self-signed.badssl.com/");
System.out.println(driver.getPageSource());
driver.quit();
software/framework versions
Google Chrome Version 64.0.3282.186
Google Chrome Driver Version 64.0.3282.186
Selenium version 3.11.0
This works for me on ChromeDriver 80.
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
option.AcceptInsecureCertificates = true;
driver = new ChromeDriver(option);
#amila-kumara is working but, usage of DesiredCapabilities.chrome() gives warning to use ChromeOptions. Please see the updated answer.
Set the chrome option values
System.setProperty("webdriver.chrome.driver", Config.NDAC_WEBDRIVER_PATH);
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1200");
options.setAcceptInsecureCerts(true);
options.setHeadless(true);
Start the service
ChromeDriverService service = null;
try {
service = new ChromeDriverService.Builder()
.usingAnyFreePort()
.withVerbose(true)
.build();
service.start();
remoteWebdriverUrl = service.getUrl();
System.out.println("Starting the url " + remoteWebdriverUrl);
} catch (IOException e) {
e.printStackTrace();
}
Note: I was facing the issue while closing the driver(with RemoteWebDriver), chromedriver.exe process won't close even when you use driver.quit(). To fix the issue use ChromeDriver instead of RemoteWebDriver
RemoteWebDriver driver = new ChromeDriver(service, options);
To properly close the driver, use
driver.close();
driver.quit();

IE11 exceptions with IEDriverServer: Unable to get browser

Yesterday Internet Explorer got updated (11.0.44 update), then below code prompts me with the following error not able to get browser.
I have set HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE key of iexplore.exe with 0.
Anyone can help me out?
Code:
String IEDriver_64 = "D:/Tools/IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", IEDriver_64);
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
WebDriver driver = new InternetExplorerDriver(capabilities);
//capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
String baseUrl = "https://xxx//member/login.php";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
Thread.sleep(1000*30);
System.out.print("driver.getCurrentUrl()1="+driver.getCurrentUrl());
System.out.print("driver.getTitle()1="+driver.getTitle());
System.out.print("-------------1-------------");
String currenthandle = driver.getWindowHandle();
System.out.print("-------------2-------------");
System.out.print("currenthandle="+currenthandle);
Thread.sleep(1000*5);
driver.findElement(By.id("SUBMIT_LOGIN")).click();

Unable to create Firefox driver using Selenium java API

I am using the below code to create a firefox web driver using Selenium Java API.But it neither create firefox instance nor gives any error message.Control comes directly to finally block after some time.
Java APi used - 2.46.0
Firefox version - 32.0.1
JRE -1.8.25
Can some one help me to debug the issue?
code -
WebDriver driver;
try{
ProfilesIni profile = new ProfilesIni(); //ignore ietab+options
FirefoxProfile defualtProfile = profile.getProfile("default");
//defualtProfile.setAcceptUntrustedCertificates(false);
DesiredCapabilities capabilities = DesiredCapabilities.firefox(); //To over come ssl certificate error
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(FirefoxDriver.PROFILE,defualtProfile);
Thread.sleep((long)(2000*Math.random()));
driver=new FirefoxDriver(capabilities);
return driver;
}
catch(Exception ex)
{
logger.error("Exception - > " + ex.toString());
return null;
}
finally
{
logger.info("End");
}
There is simple code to initialize firefox driver. Try below code and let me know.
For more information refer this.
DesiredCapabilities dc=DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE, profile);
Webdriver driver = new FirefoxDriver(dc);
return driver;

Categories