Selenium: Not able to take complete page screenshot using aShot library - java

Am trying to take the complete page screenshot both horizontally and vertically using Firefox gecko driver and aShot Library.
However, the results are not as expected. Take a look:
driver.get("https://google.com");
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"JPEG",new File("FullPageScreenshot.jpg"));
Looked into a lot of variants but nothing is working. Interestingly, when I try using old firefox version (46), I am able to take full screenshot without any third party library. Am trying to use latest firefox and have full screenshot functionality.
Any help?

Try:
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(ShootingStrategies.scaling(1.75f), 1000)).takeScreenshot(driver);
where 1.75f is device pixel ratio (you can run window.devicePixelRatio; in browser console to find it).
If it's still not capturing full screen, change it to 2f

While working with Selenium Java Client v3.12.0, ChromeDriver v2.40, Chrome v 67.0 using ashot-1.4.4.jar here is an example to take the complete page screenshot both horizontally and vertically using ChromeDriver and aShot Library of the url https://jquery.com/:
Code Block:
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class ashot_CompletePage {
public static void main(String[] args) throws Exception {
System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://jquery.com/");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery"));
Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png"));
driver.quit();
}
}
Screenshots:
Reference
You can find a detailed discussion in How to take screenshot with Selenium WebDriver

Related

Selenium Maven - Project suddenly stopped recognizing imports

As of yesterday, my simple project was working fine until I added WebDriverWait and ExpectedConditions to my code. I was able to invoke the browser and navigate to URLs fine, however as of today everything stopped working. I'm now getting "The import org.openqa cannot be resolved" as well as "WebDriver, WebDriverWait, WebElement cannot be resolved to a type"
My Code
package mavensample;
import.java.util.concurrent.TimeUnit; //This is the only import that doesn't get an error
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.chrome.ChromeDriver;
public class mavensample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "chromedriverpath";
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, Timeunit.SECONDS);
driver.manage().timeouts().implicitlyWait(40, Timeunit.SECONDS);
driver.get("https://initialLink.net/");
driver.findElement(By.cssSelector("a[href='https://censoredlink.net/']"));
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(“a[href=‘https://censoredlink.net/‘]”)));
}}
I have the needed dependencies installed thru Maven, and nothing that I know of has changed since yesterday in that regard.
Any help is much appreciated

Trouble with Selenium, Java, Screenshots and clicking buttons

This is another question about Selenium and clicking. I have been struggling for about two days and can't get it to work - I have tried the answers in the internet and now I need a concerted effort. Thanks in Advance!!
I am working on the following site http://144.76.109.38/peTEST - this might help if you want to retrace my steps.
I am trying to fill out the login form, and then click on Login and see the answer page.
Here is my code:
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.Select;
public class toJava {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","/home/tallen/RTI/lib/geckodriver/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http:144.76.109.38/peTEST");
File SF2 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(SF2, new File("./out-004.png"));
}catch(IOException ioe){
System.out.println("There was an IO error");
}
driver.findElement(By.id("user_login_name")).click();
WebElement WE4 = driver.findElement(By.id("user_login_name"));
WE4.sendKeys("Superuser");
driver.findElement(By.id("user_password")).click();
WebElement WE6 = driver.findElement(By.id("user_password"));
WE6.sendKeys("Jkerouac1!");
WebElement WE7 = driver.findElement(By.xpath(".//*[#type='button'][#onclick='login()'][#value='Login']"));
WE7.sendKeys(Keys.ENTER);
File SF8 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(SF8, new File("./out-005.png"));
}catch(IOException ioe){
System.out.println("There was an IO error");
}
driver.quit();
}
}
So basically I am opening up the page, taking a screenshot entering the user name and password, clicking Login and then taking another screenshot.
The compile and the run on this are clean - that is no exceptions and no problems. I even get two screenshots. The first screenshot shows the login page - with no data entered. The second screenshot shows the login page filled, the button I want to click marked, but not the "welcome Page" that you would get if you successfully log in. That the screenshot shows the button marked, I know that I have found the element. I have give the login info in the script, in case you want to try it out with Selenium first.
Why is the login button not being "clicked." I have tried click, perform, etc. to no avail. I have even tried putting in implicit waits - still nothing.
I have tried to Advanced Usage Interactions - and still nothing.
I am pretty new to Selenium and Java and am hoping that it is just something stupid that I am overlooking. But after looking through the Web, the solutions there are just not helping.
I am working on Debian-70-Wheezy-64-LAMP
My Selenium Libraries are from client-combined-3.0.1-nodeps.jar
My Geckodriver is v0.11.1-linux64
Thanks for the Help!!!
Hi, I don't know if it can cause a problem but anyway for the login button I would use WE7.click(); I just think that it's easier to understand what you're trying to do with the element.
I've been working with the GeckoDriver for a while and talking with some experienced people in the area and they told me that Gecko has many problems that are not fixed yet.
They always recommended me not to use GeckoDriver because it seems to fail very often and told me to use FirefoxDriver instead.
To try it this way, and this is important, you'll just need to keep working with an older version of Firefox as the version 46 that is compatible with FirefoxDriver (that version worked for me and you can download it from places like this) and avoid using GeckoDriver.
Also the version 47 seems to work with the FirefoxDriver as I've found here.
Remember: When you install one of these previous versions of Firefox, don't forget to go to settings and disable the automatic updates and background updates because if you don't do this, you'll end soon again with the latest version that requires GeckoDriver.
In addition you can try some validation as the following:
if(WE7.isDisplayed() && WE7.isEnabled()){
WE7.click();
}
This kind of validations would help in case that the page isn't fully loaded at the moment that you're trying to take action over the web element. If the element is not ready, you will click it without errors but it just won't work
Hope this works for you too!

What Firefox Driver does Selenium use?

I’m trying to use Selenium WebDriver with Eclipse and JUnit and a Firefox browser to do a series of Link tests. The trouble I’m having is the Firefox browser is opening some links in a new Window. Note: these are not Alerts.
I have the browser set to open all links in the same window, and if I navigate to the chosen links manually it does what it’s supposed to do. These are the settings of the default browser at present.
browser.link.open_newwindow; 1
browser.link.open_newwindow.disabled_in_fullscreen; true
browser.link.open_newwindow.override.external: 1
browser.link.open_newwindow.restriction; 2
When I run the same thing in Eclipse as a JUnit test from Eclipse the link opens up in a new window!
I commented out driver.close() and checked the browser that Eclipse was using and all the settings are different. There is even another setting that isn’t in my browser.
browser.link.open_newwindow; 2
browser.link.open_newwindow.disabled_in_fullscreen; false
browser.link.open_newwindow.override.external: -1
browser.link.open_newwindow.restriction; 2
browser.link.open_external 2
If I use a profile or force it to use the default profile it the browser it uses has these settings.
browser.link.open_newwindow; 2
browser.link.open_newwindow.disabled_in_fullscreen; true
browser.link.open_newwindow.override.external: 1
browser.link.open_newwindow.restriction; 2
browser.link.open_external 2
From reading previous posts I was of the understanding that Eclipse/Selenium/JUnit would use the default browser installed on my PC. Or at least the default browser profile
My Code:
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class Pro2 {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
driver = new FirefoxDriver(ffprofile);
// driver = new FirefoxDriver();
baseUrl = "https://www.google.ie/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
My questions are:
Is it using a different driver(or driver profile)?
If so how do I set the config settings so that all links open in the current window?
You can modify firefox profile before you use it in your tests by calling SetPreference method of the FirefoxProfile class:
ffprofile.SetPreference("browser.link.open_newwindow", "1");

Does ChromeDriver support Rotatable for mobileEmulation

I'm testing a web app with Selenium 2.44 and ChromeDriver 2.13. I'm using Chromes mobile emulation to mock connecting from a mobile device. I need to change the screen orientation mid test from portrait to landscape and I can't seem to get it working.
Sample code for trying to augment WebDriver to Rotatable is below but it is throwing a ClassCastException when trying to cast to Rotatable. Can someone tell me if I am doing something wrong or if ChromeDriver does not support rotation?
package test;
import org.openqa.selenium.Rotatable;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.AddRotatable;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
System.setProperty(
"webdriver.chrome.driver",
"Path/To/chromedriver.exe"
);
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Google Nexus 4");
Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
capabilities.setCapability(CapabilityType.ROTATABLE, true);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://m.rte.ie");
// Try and rotate
Augmenter augmenter = new Augmenter();
augmenter.addDriverAugmentation(CapabilityType.ROTATABLE, new AddRotatable());
WebDriver augmentedDriver = augmenter.augment(driver);
ScreenOrientation currentOrientation = ((Rotatable) augmentedDriver).getOrientation();
System.out.println(String.format("Current Orientation is: %s", currentOrientation));
driver.quit();
}
}
It appears as though Chrome supports switch orientation as is documented here: (https://developer.chrome.com/devtools/docs/device-mode) if you look at the section on, 'Swap dimensions'. I have tried this manually in the browser and it works fine. Just wondering if ChromeDriver does not support the Rotatable interface, is there a way to update the screen dimensions on the fly?
It was a tough one. Hope this saves a decent amount of effort for someone. As for now, ChromeDriver only supports portrait orientation in mobile emulation mode. There are no workarounds whatsoever to pass it as an option and no ability to trigger from javascript either.
Yet, there is a solution that seems to work for me. What you need to do is simply start the ChromeDriver with userAgent option which represents your device. For instance, here is what it looks like for iPad emulation:
Map<String, Object> mobileEmulation = new HashMap<>();
String userAgent = "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
"(KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10";
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-agent=" + userAgent);
options.addArguments("window-size=1039,859");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
Also, you might want to set the dimensions that resemble the emulated device. It's a bit tricky, since only the window size could be set in ChromeOptions. And again, with the use of this tool: Chrome viewport dimensions plugin you can find the correct ratio for your resolution. In my case, viewport's 1024x768 correspond to 1039x859 of window-size.
P.S. it could be done only on driver start. There is no possibility to change the orientation on the fly
it seems that it accepts "deviceOrientation" as a capacity with values "portrait" and "landscape", but unfortunately this is not yet implemented.
Waiting for the same answer.

Script running fine in FF fails to run in IE

I am using Selenium Webdriver(Java), i want to use IE driver for my testing, however i come up with the problem, can anyone help me out of this please, The script which are running fine in firefox fails to run in IE, I am just opening a google page and searching some word but my code only opens the google page write the keyword but unable to hit the serch button on google page using IEdriver, after too much google i found one thimg that when IE browser get opens it will opened in IE8 Compatibility view and due to this its attributes like id, name get changed as compared to FF, but when i changes this to IE8 view manually the properties are same as FF,(Press F12 key on keyboard open developers tool on IE) So can anyone please let me know how to overcome this or how to open the IE browser in IE8 mode, or anyone knows any different solution of using IE for selenium webdriver.
My code is as follows
package backOffice;
import java.io.File;
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.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName;
import bsh.ParseException;
public class Time
{
private WebDriver driver;
private String baseUrl= "http://www.google.co.in/";
public static void main(String args[]) throws InterruptedException
{
Time tm=new Time();
tm.trial();
}
private void trial() throws InterruptedException
{
File file = new File("C:/Documents and Settings/Administrator/Desktop/32- bit_IEDriverServer_Win32_2.31.0/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); caps.setCapability("ignoreZoomSetting", true);
driver=new InternetExplorerDriver(caps);
driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("harshal kakade");
driver.findElement(By.id("gbqfb")).click();
driver.findElement(By.linkText("Harshal Kakade - India | LinkedIn")).click ();
}
}
Thanks,
Harshal.
try to look at the internet options -> security and uncheck "Enable Protected Mode". Probably there is the problem.

Categories