Unable to take screenshot of CEF application using Selenium - selenium

I am using Selenium to automate the CEF application. I am successfully able to perform operations like click etc. But not able to take the screenshot using Selenium driver. As it is very much required feature for automation. How can I do this?
I'm using the following:
CEF application - sample application provided by CEF
selenium jar - selenium-server-standalone-3.0.1
cef_binary_3.2924.1564.g0ba0378_windows64_client
chromedriver
Find the below code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
public class Example {
public static void main(String[] args) {
// Path to the ChromeDriver executable.
System.setProperty("webdriver.chrome.driver", "D:/CEFTesting/chromedriver.exe");
// Path to the CEF executable.
ChromeOptions options = new ChromeOptions();
options.setBinary("D:/CEFTesting/cef_binary_3.2924.1564.g0ba0378_windows64_client/Release/cefclient.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com/xhtml");
sleep(3000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
sleep(5000); // Let the user actually see something!
String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
System.out.println(screenshotBase64);
sleep(5000); // Let the user actually see something!
driver.quit();
}
}
I am facing the error.

I'm using the following:
CEF application - Sample application provided by CEF (link - https://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver.md)
selenium jar - selenium-server-standalone-3.0.1
cef_binary_3.2924.1564.g0ba0378_windows64_client
chromedriver
Here is the code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
public class Example {
public static void main(String[] args) {
// Path to the ChromeDriver executable.
System.setProperty("webdriver.chrome.driver", "D:/CEFTesting/chromedriver.exe");
// Path to the CEF executable.
ChromeOptions options = new ChromeOptions();
options.setBinary("D:/CEFTesting/cef_binary_3.2924.1564.g0ba0378_windows64_client/Release/cefclient.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com/xhtml");
sleep(3000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
sleep(5000); // Let the user actually see something!
String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
System.out.println(screenshotBase64);
sleep(5000); // Let the user actually see something!
driver.quit();
}
}

Related

How to execute Firefox developer console commands from Selenium?

I would like to execute Firefox's screenshot --fullpage command from inside a Selenium java script.
The command is documented in Take a full page screenshot with Firefox
Is it possible?
You can just take a screenshot from within your Java code. From this answer: https://stackoverflow.com/a/3423347/8020699
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
This guy suggests using a library called aShot to take full page screenshots. Here's the link to the aShot jar. Here's the code he gives:
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class FullPageScreenshot {
public static void main(String args[]) throws Exception{
//Modify the path of the GeckoDriver in the below step based on your local system path
System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
// Instantiation of driver object. To launch Firefox browser
WebDriver driver = new FirefoxDriver();
// To oepn URL "http://softwaretestingmaterial.com"
driver.get("http://www.softwaretestingmaterial.com");
Thread.sleep(2000);
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png"));
}
}

Not able to click the Inline element using selenium webdriver

enter image description hereNot able to click the inline element using selenium webdriver.
Here is the URL
https://www.google.com/.
Besides Images link (Right side top) there is a square icon. Need to click that icon and select Maps.
Screenshot attached.
I used xpath, cssselector, ID, Name but nothing is working.
Could anyone help me on this.
Code:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbwa")).click();
driver.findElement(By.className("gb_3")).click();
}
}
This code is somehow not working in Firefox because it opens the Products page when you click on element - By.id("gbwa"). But if you try the same in Chrome, it works fine. Only thing you would need to change is By.className("gb_3") with By.xpath("//ul[#class='gb_ka gb_da']/li[3]").
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbwa")).click();
driver.findElement(By.xpath("//ul[#class='gb_ka gb_da']/li[3]")).click();
For Firefox, you can modify the code so that when the products page is opened, you can click on the Maps option from there.
Some dynamic waits need to be added for controls. Additionally, I noticed that, if google is not set as your home page then you see a message 'Come here often? Make Google your homepage.'. This message needs to be handled, otherwise it will fail the access to the apps icon.
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 10);
if (driver.findElements(By.xpath("//a[#title='No thanks']")).size() !=0) {
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[#title='No thanks']"))));
driver.findElement(By.xpath("//a[#title='No thanks']")).click();
}
driver.findElement(By.xpath("//div[#id='gb']//div[#id='gbwa']/div/a[#title='Google apps' and #role='button']")).click();
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[#id='gb8']/span[text()='Maps']"))));
driver.findElement(By.xpath("//a[#id='gb8']/span[text()='Maps']")).click();
}
}
Try this code and let me know, if you have further queries.
I've tested the code below and it works.
driver.get("https://www.google.com");
driver.findElement(By.cssSelector("a[title='Google apps']")).click();
new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.id("gb8"))).click();
You basically need to click the Apps icon, wait for the Maps icon to be clickable, and click it.
I'm assuming you don't work for Google so in that case, why test the UI? You can just directly navigate to https://maps.google.com and skip the UI clicks.
How about this ... worked for me.
driver.get("https://www.google.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='gbwa']//a[#title='Google apps']"))));
driver.findElement(By.xpath(".//*[#id='gbwa']//a[#title='Google apps']")).click();
//click map icon
driver.findElement(By.cssselector("a#gb8")).click();
Use this, working for me.
Use Chrome driver instead of Firefox driver.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GoogleMaps {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id='gbwa']")).click();
driver.findElement(By.xpath("//li/a[#id='gb8']")).click();
Thread.sleep(6000);
driver.quit();
}
}
Output :

i am not able to launch the web browser using TestNG

below is the code : im not able launch the browser using the code . please suggest the solution
import org.openqa.selenium.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.*;
public class NewTest {
public WebDriver driver;`enter code here`
#Test
public void VerifyHomepageTitle() {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://google.com");
driver.quit();
}
}
You are closing the web page immediately after opening using 'driver.quit()'. Comment this code and try you will see the difference.

Need help on Desiredcapabilities class

Hi I am learning selenium grid for the first time.I am unable to understand why Desired capabilities class is used to set browser name and platform details in a remote machine.
please let me know if there is any proper documentation on it apart from google-wiki page
Thanks
prathima
Not all remote machines (or sessions) support the features requested by a user. E.g. user may want to use Chrome on Windows but some machines are only running Firefox on Linux.
The desiredCapabilities is used to describe the features of a session requested by user. You can refer to this link and this link for more info.
- when user send request to remote machine to run a particular test on
a particular browser on a particular platform.
- It doesn't mean that all the remote machine will support all the
features requested by user such as (Particular browser on a
particular machine).
**Example:**
- If user request to execute a testcase on internet explorer on Linux
platform. It doesn't mean that always Linux machine has internet
explorer it would have firefox not internet Explorer(Because it is
open source).
- Linux machine doesn't support the features requested by a User. so
that, that point of time we will use DesiredCapabilites class to set
the platform , Browser Name and version of browser of Remote Machine
to execute the Test.
Example:
package grid;
import static org.junit.Assert.*;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class SimpleGridTest
{
WebDriver driver;
String baseUrl , nodeUrl;
#Before
public void setUp() throws Exception
{
WebDriver driver;
String baseUrl , nodeUrl;
baseUrl = "https://www.facebook.com";
nodeUrl = "http://192.168.10.21:5568/wd/hub";
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WIN8_1);
driver = new RemoteWebDriver(new URL(nodeUrl),capability);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);
}
#Test
public void test() throws InterruptedException
{
driver.manage().window().maximize();
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.id("Email")).sendKeys("abc#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("1234");
driver.findElement(By.id("signIn")).click();
}
#After
public void tearDown() throws Exception
{
driver.quit();
}
}

Element is not clickable at point using chrome driver

I am trying to download payslip as PDF from greytip web portal using chrome driver.i am trying to click on link salary by using "driver.findElement(By.linkText("Salary")).click();".But i am unable to click the link and failed with following exception.
Error
org.openqa.selenium.WebDriverException: Element is not clickable at point (198, 139). Other element would receive the click: ... (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 37 milliseconds
And also when ever i ran the script chrome open one extra tab for bit torrent.so here when i ran the program one tab is opened for "https://psdpl.greytip.in" and another tab is opened for bittorrent.How can i handle not to open another bittorrent tab when i ran the program.
Here i am attaching the code and screen shots.enter image description here
Code
package com.webdriver.tests;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class PaySlipPDF {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\soleti\\D-Drive\\Selenium\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "https://psdpl.greytip.in/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testPayslip() throws Exception {
driver.get(baseUrl + "/login.do");
driver.findElement(By.id("j_username")).clear();
driver.findElement(By.id("j_username")).sendKeys("101786");
driver.findElement(By.id("j_password")).clear();
driver.findElement(By.id("j_password")).sendKeys("password");
driver.findElement(By.id("login-button")).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//driver.findElement(By.xpath("//*[#id='home-page']/div[1]/div[1]/ul/li[2]/a")).click();
WebElement elementToClick = driver.findElement(By.xpath("//*[#id='home-page']/div[1]/div[1]/ul/li[2]/a"));
System.out.println(elementToClick);
// Scroll the browser to the element's Y position
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");
// Click the element
elementToClick.click();
//driver.findElement(By.linkText("Salary")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("View Payslips")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
new Select(driver.findElement(By.id("payroll"))).selectByVisibleText("Mar 2012");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.className("btn btn-gts-print")).click();
}
#After
public void tearDown() throws Exception {
//driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
It is the BitTorrent toolbar that is causing the issue. It is launched into Chrome by an extension. Either uninstall it completely, or force Selenium to tell Chrome to disable all extensions. This can be done using the ChromeOptions class:
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/chrome/ChromeOptions.html
Use the addArgument method and give it this arguement, which tells Chrome to disable all user extensions:
--disable-extensions
Check the browser settings and see if the default homepage has been changed by the bit torrent toolbar that is installed. If not needed, try to uninstall the bitTorrent toolbar from the browser and rerun your selenium program. Hope this helps.
This problem is specificaly related to chrome installed on your system..Uninstall the google chrome and re-install it..That will fix your problem..:)