We are using sikuli for image comparison in our automation, but i'm getting below error when i ran the test.
We are using sikuli because we are doing image comparison for small part of the web page.
Error i'm getting:
FindFailed: can not find P(D:\Automation\test-data\Student.jpg) S: 0.99 on the screen.
Line 1574, in file Region.java
code is:
public static void FindPattern(String BaseImage)
{
try{
Pattern imagepattern = new Pattern(BaseImage);
imagepattern.similar((float)0.99);
Screen screen=new Screen();
screen.find(imagepattern)
}catch(Exception e){
e.printStackTrace();
}
}
Does the same image work if you test it from Sikuli IDE? If it does then there could be couple of things which are causing the failure.
1.Application and script sync issue. You might be looking for the image even before it is available on the page.
2.The screenshot may be having too much of background in it.
Related
I have been trying to automatize some tasks on my computer and did choose Sikuli from Java to do so (I work with Java everyday and didn't know any automation tool using java, sikuli was the first I found). I use java with maven and eclipse as IDE. I have added Sikuli as a Maven dependency.
<dependency>
<groupId>com.sikulix</groupId>
<artifactId>sikulixapi</artifactId>
<version>2.0.5</version>
</dependency>
I tried to do some simple stuff. I did a few screenshots of parts of my screen using windows' screenshot tool, and wanted sikuli to hover it. It works quite fine for one image, but not at all for the others. It seems that the bigger the image the better it works as I did not have success for any small images. The one working is a screen of a whole window (reduced to ~1/4 of my screen).
I also tried to find a button inside this window, to find the windows logo on bottom left, to find a screen of my package explorer, but none work correctly.
I played with similar() using various values, but it didn't improve the results. In some cases (button inside the window) it did find a result for some low similar value, but it was another button. The weird part is : its finding this other button which is bright blue, while the one i'm looking for is purple.
My pc background never changes, I did some screen.highlight() and its looking at the correct screen (dual screen). It's not an issue with the path to images (already solved this one).
Do you have any idea of what I could try ? I have read about people having different success rate depending on whether they were using Sikuli IDE or another IDE. So maybe I could give sikuli IDE a try.
I can give code samples as soon as I am back home.
The code I'm using to test :
public class CleanTest {
static Screen screen = new Screen();
public static void main(String[] args) throws FindFailed, AWTException, IOException, InterruptedException {
String pathYourSystem = System.getProperty("user.dir") + "\\";
System.out.println(pathYourSystem);
Pattern pLauncher = new Pattern(pathYourSystem+"img\\full_launcher.PNG").similar(0.9d);
Desktop.getDesktop().open(new File("path_to_an_exe_opening_a_launcher"));
screen.wait(pLauncher, 300);
screen.mouseMove();
System.out.println("launcher found");
}
}
It works with the "full launcher" image, but it doesn't find a sub-part of the launcher (a button). I tried to make some code to test if there was some threshold for the similar parameter :
double similarValue = 1d;
Pattern pLauncher = new Pattern(pathYourSystem+"img\\the_button.PNG").similar(similarValue);
Desktop.getDesktop().open(new File("path_to_an_exe_opening_a_launcher"));
while(!screen.has(pLauncher)) {
similarValue-=0.1;
pLauncher = new Pattern(pathYourSystem+"img\\login.PNG").similar(similarValue);
}
System.out.println(similarValue);
screen.mouseMove();
it finds something at around 0.5, but it's a totally different button.
Thank you !
EDIT: if someone has the same issue, try to use sikulix IDE to take the screenshots. It works with the screenshots taken by the IDE.
This is a simple test, that completely stays within the SikuliX features.
import org.sikuli.basics.Debug;
import org.sikuli.script.*;
public class SikulixTest {
public static void main(String[] args) {
System.out.println("SikulixTest");
Screen scr = new Screen();
// craete an image to be searched on the screen
Image img = new Image(scr.userCapture());
// try to find it
Match mImg = scr.exists(img);
if (mImg != null) {
// show sthg. when found
Debug.info("%s", mImg);
mImg.highlight(2);
}
}
}
This is RaiMan from SikuliX
From what I've read the following code:
File s = ((TakesScreenshot)driver_).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(s,new File("C:\\scr.png"));
} catch (IOException exception) {
exception.printStackTrace();
}
Should take a full page screenshot. But in my case it will only take the screenshot of whatever is currently visible in the browser window. Is this the expected behaviour or did something go wrong in the code?
Yes it is expected behavior from firefox.
If you want to take full page screenshot you can use something like this to zoom out the whole contents to visible area
executor = (JavascriptExecutor)driver.getDriver();
executor.executeScript(
"document.body.style.zoom=
(top.window.screen.height-70)/
Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);");
File scrFile = ((TakesScreenshot)driver.getDriver()).getScreenshotAs(OutputType.FILE);
It will try to bring all the content to visible area, although you can still miss some content from the very bottom area.
Does anybody know how to make full screenshot of display using webdriver?
In Selenium documentation I read that it is possible: (http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/TakesScreenshot.html)
..
The screenshot of the entire display containing the browser
..
I use Selenium Grid. My hub is on linux and node on windows. I tried to use Robot, however it takes screenshot on linux, however needs on windows
If you want a screenshot of your currently running browser instance then you can do it using following code:
public static void captureScreen(WebDriver driver, String screenshotFileName)
{
String screenshotsFile = screenshotsFolder+screenshotFileName+imageExtention;
try {
File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenShot, new File(screenshotsFile));
} catch (IOException e) {
}
}
But if you want to take screenshot of your active window (other than browser only) then you can use Robot class.
Edit:
Although it's too late for you, the link below contains your answer. I think it may be helpful for others who are searching for the same thing.
Screen shot issue in selenium webdriver
You need to use FirefoxDriver to screenshot whole page.
Chrome and IE doesn't support it.
hi try it like below it will take the complete webpage screen shot
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//The below method will save the screen shot in defined drive with name "screenshot.png"
FileUtils.copyFile(scrFile, new File("yourPath\\screenshot.png"));
Im experimenting with Allure aShot() class to take screenshots of specific WebElements on a website I'm working on, and below is the code I used in selenium to make it happen.
Please visit this link, which shows aShot() project documentation:
https://github.com/yandex-qatools/ashot
So My QUESTION is that, where are these AShot() screenshots of the WebElement actually being saved? I used testNG to execute below method and successfully able to generate allure reports but I can't see these screenshots in those reports or anywhere in my framework. Please check below code, it is very difficult to pinpoint the location of these images.
So again, my basic question is: How do we specify selenium to store these AShot() screenshots of the WebElement into a particular file that we want?
I've tried to cast Screenshot class mentioned below to BufferedImage or TakesScreenshot class and use ImageIO.write or FileUtils.copyFile methods to copy these images into a file and store these images there, but I get an error saying, for example, "java.lang.ClassCastException: ru.yandex.qatools.ashot.Screenshot cannot be cast to org.openqa.selenium.TakesScreenshot" and I've tried other methods as well, unsuccessfully.
Please help me resolve this issue, how do we know/specify where these AShot() screenshots are being saved?
public WebDriver driver;
#Test
public void getAShotImage() {
driver.get("http://....../");
WebElement element = driver.FindElement(By.xpath(".............."));
AShot shot = new AShot();
shot.takeScreenShot(driver, element);
OR
shot.coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver, element);
}
You can attach your screenshots by returning byte array from the method. Look at the following example:-
#Attachment(value="Screenshot", type="image/png")
private static byte[] captureScreenshot(Webdriver driver)
{
byte[] screenshot = null;
screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
return screenshot
}
Above code does not use Ashot but it shows how you need to attach files. Also, if you are still not able to see your screenshots, check if your steps are displayed in your report.If not then probably you are missing javaagent as discussed in FAQ
AShot returns the Screenshot object, that contains the image of your element and information for comparison of screenshots. In this case you can use getImage() method to get the image.
new AShot().takeScreenshot(....).getImage();
The Screenshot will contain the byte array instead of BufferedImage soon.
This question already has answers here:
How can I take a screenshot with Selenium WebDriver?
(48 answers)
Closed 6 years ago.
I am trying to take a screenshot of a webpage while selenium is running. I am using the following code for this purpose
WebDriver augmentedDriver = new Augmenter().augment(seleniumDriver);
File scrFile = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
Now it serves my purpose perfectly well except that whenever this method gets called the browser automatically gets into the default size and maximizes again.
And this continues every time the screenshot function gets called.
I am able to solve the problem If I am NOT using the selenium webdriver for taking the screenshots and using other java functions.
I wanted to know if anyone had similar problems/why I am having this problem. And is there any workaround?
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\screenshot.jpg"));
This code will definitely help
It tries to adapt to page size and take the screenshot as small as possible or as large as needed for the whole page to fit. Apart from being annoying, it should not be a cause of any other problems and is therefore considered a better solution than taking a screenshot of just the actual viewport which could be missing some important piece of the page you're trying to examine.
If you're not happy about it, use Robot and its createScreenCapture() method.
Or, but it will only work for Firefox, you may try overriding the FirefoxDriver's method for screenshots. Not tested, no idea whether you'll be allowed to do it or not.
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("FirefoxDriver.prototype.screenshot = function(a){};");
and (if that's not enough) maybe even
js.executeScript("FirefoxDriver.prototype.saveScreenshot = function(a,b){};");
Inferred from here. The actual screenshooting code is here. You can replace the FirefoxDriver.prototype.screenshot function with your own that wouldn't take the maximum scrollable values for height and width...
The code to take the screenshot make use of getScreenshotAs method of TakesScreenshot interface. Following code will take screenshot of the webpage opened by webDriver instance.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg"));
Now in order to take screenshot in case of test failure we will use AfterMethod annotation of TestNG. In the AfterMethod annotation we will use ITestResult interface's getStatus() method that returns the test result and in case of failure we can use the above commands to take screenshot. Code snippet to take screenshot on test failure-
#AfterMethod
public void takeScreenShotOnFailure(ITestResult testResult) throws IOException {
if (testResult.getStatus() == ITestResult.FAILURE) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg"));
}
}
For complete sample script refer to http://artoftesting.com/automationTesting/screenShotInSelenium.html
I have created a method for this, which is very easy to use. It takes the screenshot of the whole web page. It counts and name the screen shot depending on the number of screenshots, the method also stores the screenshot as .png file into src.
public static void screenshot(WebDriver driver)
{
System.out.println("Taking the screenshot.");
console_logs("Taking the screenshot.");
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File("src//tempOutput//webPage_screenshot_"+count+".png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
//return scrFile;
}
Using the method:
screenshot(driver);