How to assert a screen with Sikuli java API(Not Sikuli script) - java

In Sikuli script, we have different ways of asserting like Screen.exists(a.png) etc for image assertion. But can someone please tell me how to assert an application screen with a screenshot file.
My requirement is that, I'm logging into a windows application, and after logging in I want to verify the screen that appears with a screenshot file(.png) captured earlier.
Sikuli api 1.0.2
Windows 7 64 bit

This should help you-
int timeout = <Specify timeout in millis>;
String imagePath = <give the path of your screenshot image here>;
ImageTarget target = new ImageTarget(new File(imagePath));
ScreenRegion result = desktop.wait(target,timeout);
Here if result is null after the timeout, it would mean the image found in the application does not match the one at the path of the screenshot you would have mentioned.

Related

Implementing an image upload button using Jython & Swing

I'm developing a cross platform desktop application using Jython and Swing, and I found a hitch.
I would like to develop a button that allows me to load an image in its background and to change it when I reclick on the image loaded.
I attach as an example some pictures on how I would like my widget to be.
Upload pane without image
And then, when I load an image:
Upload Pane with image
I tried with the following code:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
fileName = fd.getFile()
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
"""'self.bigDict['imgButton']' is the current JButton"""
self.bigDict['imgButton'].setIcon(ImageIcon(fileAbsPath))
When I click on "Open", the image is not inserted into the button. I do not understand why.
I also tried with the following code:
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
img = ImageIO.read(getClass().getResource(fileAbsPath))
self.bigDict['imgButton'].setIcon(img)
In the above example the following error is reported:
img = ImageIO.read(getClass().getResource(fileAbsPath))
TypeError: getClass(): expected 1 args; got 0
I would be curious to know why the button is not updated with the image loaded, and why the above error in java would not happen.
Thanks in advance!
getClass() needs 1 argument: the implicit this argument. You must call the method on an object, or use the MyClass.class notation.
The problem is very simple.
When an image is loaded with FileDialog it is "virtually located" in the FileDialog window, but the image does not exist. I realized this when I tried to copy the image from the absolute path to the destination folder, using shutil.copy2(self.imgAbsPath, destinationPath+'/'+self.imgName), which reported an error message indicating that the image does not exist.
To provide the concrete path of a file, it is necessary to add information about the folder where it is located.
In practice you have to create the relative path, before generating the absolute one:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
self.imgName = fd.getFile()
relativePath = fd.getDirectory() + fd.getFile() """Here is the missing piece!"""
if self.imgName != None:
self.imgAbsPath = os.path.abspath(relativePath) """Here the absolute path is correctly obtained!"""
self.bigDict['imgButton'].setIcon(ImageIcon(ImageIcon(self.imgAbsPath).getImage().getScaledInstance(130, 130, Image.SCALE_DEFAULT)))
"""The image, before being inserted into the button, is slightly resized, to avoid graphic bumps!"""
I hope I've been helpful.

How to capture error message text in appium

Error message is against EditText field. We don't have any direct locator available which points to error message. Can anyone suggest what alternate we have to handle this in appium.
Try this method if you are not able to assert Error message against EditText field.
Trigger text message on the screen
Capture screenshots
Convert image to text file
def assettoast(string)
sname = (0...8).map { (65 + rand(26)).chr }.join
$driver.driver.save_screenshot("#{sname}")
#Make sure tesseract is installed in the system. If not you can install using "brew install tesseract" in mac
system ("tesseract #{sname} #{sname}")
text_file="#{sname}.txt"
var= get_string_from_file(string, text_file)
raise if var != true
end
Check whether error message is there in text file
def get_string_from_file(word, filename)
File.readlines(filename).each do |line|
return true if line.include?(word)
end
end
There is no you will get using uiautomatorviewer and Google's original UiAutomator. Check old thread
However you may try Appium own implementation UiAutomator2 and Appium desktop inspector, maybe it will parse error.
Otherwise there is new appium end-point to compare images:
/session/:sessionId/appium/compare_images
You can use it to verify the error on screen

OpenCVFrameGrabber javacv in MATLAB doesnt work

I am using this code in MATLAB R2015a:
javaaddpath('javacv.jar')
import org.bytedeco.javacv.*
grabber = OpenCVFrameGrabber(0)
grabber.start()
img = grabber.grab()
The first time I use this code, it works, opens the camera and grub some image.
After I use grabber.stop(), the code doesn't work any more.
Even if restart MATLAB, and make sure I can open the camera in other programs and made sure the camera is available.
When I run this line: grabber.start(), a new window 'Video Source' is opened.
It does not open when I use the code for the first time.
Then I press ok, and there is exception:
Java exception occurred:
org.bytedeco.javacv.FrameGrabber$Exception:
cvCreateCameraCapture() Error: Could not create
camera capture.
at
org.bytedeco.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:179)
How I can solve it?
as #Samuel Audet as Mention,
I switch to VideoInputFrameGrabber( for windows )
for linux we need to use FrameGrabber.createDefault(0) , 0 is device index as webcam is at 0 by default
Now the code is look like that, and it work. (Maybe It what i was using in the first time??)
javaaddpath('javacv.jar')
import org.bytedeco.javacv.*
grabber = VideoInputFrameGrabber(0)
grabber.start()
img = grabber.grab()
EDIT:
as #Samuel Audet as Mention,
For cross platform code, I switch to FrameGrabber.createDefault()
javaaddpath('javacv.jar');
import org.bytedeco.javacv.*
grabber = FrameGrabber.createDefault(0);
grabber.start();
img = grabber.grab();

How to deal with file uploading in test automation using selenium or webdriver

I think that everybody who uses Webdriver for test automation must be aware of its great advantages for web development.
But there is a huge issue if file uploading is part of your web flow. It stops being test automation. The security restriction of browsers (invoking file selection) practically makes it impossible to automate tests.
Afaik the only option is to have Webdriver click the file upload button, sleep the thread, have developer/tester manually select the file, and then do the rest of the web flow.
How to deal with this, is there a workaround for it? Because it really can't be done like this. It wouldn't make sense.
This is the only case I know of when browser security restrictions do not apply:
<script language=javascript>
function window.onload(){
document.all.attachment.focus();
var WshShell=new ActiveXObject("WScript.Shell")
WshShell.sendKeys("D:\MyFile.doc")
}
</script>
Webdriver can handle this quite easily in IE and Firefox. Its a simple case of finding the element and typing into it.
driver = webdriver.Firefox()
element = driver.find_element_by_id("fileUpload")
element.send_keys("myfile.txt")
The above example is in Python but you get the idea
Using AWT Robots is one option, if you're using Java, which you are. But it's not a good option, it is not very dependable, and not clean at all. Look here
I use HttpClient and run a few tests outside of Selenium. That's more dependable and cleaner.
See the code below. You'll need more exception handling and conditionals to get it to suit your job.
HttpClient c = new HttpClient();
String url = "http://" + cargoHost + ":" + cargoPort + contextPath + "/j_security_check";
PostMethod post = new PostMethod(url);
post.setParameter("j_username", username);
post.setParameter("j_password", password);
c.executeMethod(post);
url = "http://" + cargoHost + ":" + cargoPort + contextPath + "/myurl.html";
MultipartPostMethod mPost = new MultipartPostMethod(url);
String fileNameWithPath = this.getClass().getClassLoader().getResource(filename).getPath();
File f1 = new File(fileNameWithPath);
mPost.addParameter(elementName, f1);
mPost.addParameter("action", "upload");
mPost.addParameter("ajax", "true");
c.executeMethod(mPost);
mPost.getResponseBodyAsString();
The suggestion of typing into the text box works only if the textbox is enabled.
Quite a few applications force you to go through the file system file browser for obvious reasons.
What do you do then?
I don't think the WebDriver mavens thought of just presenting keys into the KeyBoard buffer (this used to be a "no brainer" in earlier automation days)
===
After several days of little sleep, head banging and hair pulling I was able to get some of the Robot-based solution suggested here (and elsewhere).
The problem i encountered was that the dialog text box that was populated with the correct file path and name could not respond to the KeyPress/Release Events of terminating the file name with VK_ENTER as in:
private final static int Enter = KeyEvent.VK_ENTER;
keyboard.keyPress(Enter);
keyboard.keyRelease(Enter);
What happens is that the file path and file name are typed in correctly but the dialog remains opened - against my constant hoping and praying that the key emulation will terminate it and get processed by the app under testing.
Does anyone know how to get this robot to behave a bit better?
Just thought I'd provide an FYI to author's original post of using ActiveX. Another workaround would be to integrate with desktop GUI automation tools to do the job. For example, google "Selenium AutoIt". For a more cross-platform solution, consider tools like Sikuli over AutoIt.
This of course, is not considering WebDriver's support for uploads on IE & Firefox via SendKeys, or considering for other browsers where that method doesn't work.
After banging my head on this problem for far too many hours, I wanted to share with the community that Firefox 7.0.1 seems to have an issue with the FirefoxDriver sendKeys() implementation noted above (at least I couldn't get it to work on my Windows 7 x64 box), I haven't found a workaround, but updating to Firefox 8.0.1 seems to have fixed the problem. For those of you wondering, it's also possible to use Selenium RC to solve this problem (though you need to account for all of your target operating systems and the native key presses required to interact with their file selection dialogs). Hopefully the issues I had to work around save other people some time, in summary:
https://gist.github.com/1511360
If you have your are using a grid, you could make the folder of the testfiles open for sharing.
This way you could select the upload input field and set its value to \\pc-name\myTestFiles
If you're not, you should go with local files on each system.

Take Screenshot of Android screen and save to SD card

There are a few questions here on SO about capturing screenshots of an android application. However, I haven't found a solid solution on how to take a screenshot programatically using the android SDK or any other method.
So I thought I would ask this question again in the hopes that I can find a good solution, hopefully one that will allow capturing full length images that I can save to the SD card or somewhere similar.
I appreicate any help
This is not possible directly on the device/emulator, unless it is rooted.
to honest all I need it for is the emulator as this is for a testing application on a PC
This sounds like a job for monkeyrunner.
monkeyrunner tool can do the job for you with bit of adb command, [python script]
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
//waits for connection
device = MonkeyRunner.waitForConnection()
//take the current snapshot
device.takeSnapshot()
//stores the current snapshot in current dir in pc
device.writeToFile('current.png')\
//copy it to the sd card of device
os.subprocess.call('adb push current.png /sdcard/android/com.test.myapp/current.png')
Note: call this jython script file
monkeyrunner.bat <file name>
You will most likely not be happy with this answer, but the only ones that I have seen involve using native code, or executing native commands.
Edit:
I hadn't seen this one before. Have you tried it?:
http://code.google.com/p/android-screenshot-library/
Edit2: Checked that library, and it also is a bad solution. Requires that you start the service from a pc. So my initial answer still holds :)
Edit3: You should be able to save a view as an image by doing something similar to this. You might need to tweek it a bit so that you get the width/height of the view. (I'm inflating layouts, and specify the width/height when I layout the code)
View content = getView();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
File file = new File(pathAndFilename);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
You can look at http://codaset.com/jens-riboe/droidatscreen/wiki (with a write up at http://blog.ribomation.com/2010/01/droidscreen/): this is a Java library that uses adb to capture a screen shots. I've been able to (with a lot of elbow grease) modify the source to let me automatically capture a timed series of screen shots (which I use for demo videos).
You can see the class structure at http://pastebin.com/hX5rQsSR
EDIT: You'd invoke it (after bundling all the requirements) like this:
java -cp DroidScreen.jar --adb "" --device "" --prefix "" --interval

Categories