Sikuli while loop syntax in java - sikuli

Here is a example of Sikuli script
while (exists("OK.png"),10):
click("OK.png")
How can I do the same in Java?
Here is what I tried:
Screen screen = new Screen();
Pattern image = new Pattern("OK.png");
while (screen.exists(image))
{
screen.click(image);
}
But it is failing to compile with this exception:
java: SikuliTest.java:29: incompatible types
found : org.sikuli.script.Match
required: boolean
Can anyone provide the correct syntax?

According to the documentation, exists() returns a Match object if the image was matched, or null otherwise. Try this:
while (screen.exists(image) != null)

import org.sikuli.script.FindFailed;
Screen screen = new Screen();
try{
while(screen.exists("OK.png") != null){
screen.click("OK.png");
}
}
catch(FindFailed e){
e.getStackTrace();
}

Related

Why Java (Eclipse) shows an error indicating 160.934 is a boolean?

for the line of assert below, Eclipse gives an error: Type mismatch: cannot convert from double to boolean. Anyone knows why?
public class ConversionImplTest {
#Test
public void test() {
ConversionImpl conversionImpl = new ConversionImpl();
double result = conversionImpl.milesToKilometers(100);
assert(result = 160.934);
//fail("Not yet implemented");
}
}
Please try this:
double result = 99.99;
assert(result == 66.66);
The test is successful. Why?
I think it should be result == 160.934 instead of result = 160.934
Don't forget that assert is a Java keyword, not a JUnit method, and as such it is normally ignored unless you specify -ea on the command line. JUnit runs do not normally specifiy this argument to the JVM.

Fatal error: Can't use function return value in write context SOMETIMES

I get so annoyed when I transfer websites from one machine to another and I get a bunch of errors, such as this case. But this one made it to my curiosity and this is why I'm asking a question. I have the following code:
namsepace MF;
class Box {
private static $dumpYard = array();
public static function get($name) {
return self::$dumpYard[$name];
}
public static function set($name, $value, $overwrite=false) {
if($overwrite || !isset(self::$dumpYard[$name])){
self::$dumpYard[$name] = $value;
}else{
if(DEBUG_MODE){
echo('Value for "'.$name.'" already set in box, can\'t overwrite');
}
}
}
}
So when my application gets to the following line on my LOCAL testing server:
if(!empty(\MF\Box::get('requestsSpam'))){
throw new \Exception('Please don\'t spam');
}
I get a Fatal error: Can't use function return value in write context. However this code does not throw an error on the actual hosting server of my website. How come is that?
empty() works only with a variable. It should be:
$result = \MF\Box::get('requestsSpam');
if(!empty($result)){
throw new \Exception('Please don\'t spam');
}
Why? empty() is a language construct and not a function. It will work only with declared variables, that's just how it is designed, no magic.

Underscores in MvvmCross data binding

I'm using the fluent syntax and lambdas for data binding in MvvmCross. An example of this is:
var bindings = this.CreateBindingSet<MyTableCell, MyTableCellViewModel>();
bindings.Bind(titleLabel).To(vm => vm.MY_TITLE);
bindings.Apply();
Whenever I try this with an underscore in a view model property I get an exception:
Cirrious.CrossCore.Exceptions.MvxException: Unexpected character _ at
position 3 in targetProperty text MY_TITLE
I believe the error message is a result of MvvmCross parsing the data binding, yet this seems to only make sense for people using string-based data binding, not the lambda expression syntax.
Unfortunately, I cannot change the view models so I'm looking for a workaround to allow underscores in the view models. Any ideas?
I'd guess this is a general problem in the MvvmCross parser - probably in
private void ParsePropertyName()
{
var propertyText = new StringBuilder();
while (!IsComplete && char.IsLetterOrDigit(CurrentChar))
{
propertyText.Append(CurrentChar);
MoveNext();
}
var text = propertyText.ToString();
CurrentTokens.Add(new MvxPropertyNamePropertyToken(text));
}
In https://github.com/MvvmCross/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding/Parse/PropertyPath/MvxSourcePropertyPathParser.cs#L80
Which probably needs to be fixed to something like:
while (!IsComplete &&
(char.IsLetterOrDigit(CurrentChar) || CurrentChar == '_')
There are workarounds you could do, but the easiest solution is probably to fix this and rebuild, rather than to try workarounds.
But if you do want to try workarounds....
Assuming this is static (non-changing) text and this is just a one-off for now, then one workaround might be to add a property to your cell called Hack and to then bind like:
bindings.Bind(this).For(v => v.Hack).To(vm => vm);
//...
private MyTableCellViewModel _hack;
public MyTableCellViewModel Hack
{
get { return _hack; }
set { _hack = value; if (_hack != null) titleLabel.Text = _hack.MY_VALUE; }
}
Another alternative (with the same assumptions) might be to use a value converter -
bindings.Bind(titleLabel).To(vm => vm.MY_TITLE).WithConversion(new WorkaroundConverter(), null);
// ...
public class WorkaroundConverter : MvxValueConverter<MyTableCellViewModel, string>
{
protected override string Convert(MyTableCellViewModel vm, /*...*/)
{
if (vm == null) return null;
return vm.MY_TITLE;
}
}

How to serialize an object in windows phone 7

How to convert an object to JSON data in windows phone. In web application I have used following code
JavaScriptSerializer serializer = new JavaScriptSerializer();
string stringData = serializer.Serialize(object);
I want to get the same output as that of the above code in windows phone 7.
JavaScriptSerializer is not supported on Windows Phone. An alternative is to use JSON.NET (you can add it via NuGet).
The code will then look like this:
string stringData = JsonConvert.SerializeObject(object);
firrst you need to download Newtonsoft.Json dll for parse web serevice
Just follow bellow step
Step1: Add Service References by right click on add References.
Step2: Now put your web service link on Service References and press go button, And also add Namespace of service Reference
Step3: Now add using Newtonsoft.Json.Linq; name space in your .cs file
Step4: Now add bellow code in your cs file
WhatsupServices.WhatsUpServiceSoapClient ws = new WhatsupServices.WhatsUpServiceSoapClient();
ws.ContactUsJSONCompleted += ws_ContactUsJSONCompleted;
ws.ContactUsJSONAsync(txtContactUsName.Text, txtContactUsPhone.Text, txtContactUsEmail.Text, txtContactUsComment.Text);
step6: now genrate your resopnce method
void ws_ContactUsJSONCompleted(object sender, dynamic e)
{
if (e.Error != null)
{
MessageBox.Show(LogIn.NetworkBusyMsg, LogIn.MsgHdr, MessageBoxButton.OK);
busyIndicator.IsRunning = false;
}
else
{
busyIndicator.IsRunning = false;
string Result = e.Result;
JObject obj = JObject.Parse(Result);
string ResultCode = (string)obj["ResultCode"];
string ResponceMessage = (string)obj["ResponseMessage"];
if (ResultCode == "1")
{
MessageBox.Show("Thank you for your message. We'll get back to you soon.", LogIn.MsgHdr, MessageBoxButton.OK);
NavigationService.GoBack();
}
else
{
}
}
}
Hope it will help you.
If any query than comment here.I wll help you

How to export data from LinqPAD as JSON?

I want to create a JSON file for use as part of a simple web prototyping exercise. LinqPAD is perfect for accessing the data from my DB in just the shape I need, however I cannot get it out as JSON very easily.
I don't really care what the schema is, because I can adapt my JavaScript to work with whatever is returned.
Is this possible?
A more fluent solution is to add the following methods to the "My Extensions" File in Linqpad:
public static String DumpJson<T>(this T obj)
{
return
obj
.ToJson()
.Dump();
}
public static String ToJson<T>(this T obj)
{
return
new System.Web.Script.Serialization.JavaScriptSerializer()
.Serialize(obj);
}
Then you can use them like this in any query you like:
Enumerable.Range(1, 10)
.Select(i =>
new
{
Index = i,
IndexTimesTen = i * 10,
})
.DumpJson();
I added "ToJson" separately so it can be used in with "Expessions".
This is not directly supported, and I have opened a feature request here. Vote for it if you would also find this useful.
A workaround for now is to do the following:
Set the language to C# Statement(s)
Add an assembly reference (press F4) to System.Web.Extensions.dll
In the same dialog, add a namespace import to System.Web.Script.Serialization
Use code like the following to dump out your query as JSON
new JavaScriptSerializer().Serialize(query).Dump();
There's a solution with Json.NET since it does indented formatting, and renders Json dates properly. Add Json.NET from NuGet, and refer to Newtonsoft.Json.dll to your “My Extensions” query and as well the following code :
public static object DumpJson(this object value, string description = null)
{
return GetJson(value).Dump(description);
}
private static object GetJson(object value)
{
object dump = value;
var strValue = value as string;
if (strValue != null)
{
var obj = JsonConvert.DeserializeObject(strValue);
dump = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
}
else
{
dump = JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented);
}
return dump;
}
Use .DumpJson() as .Dump() to render the result. It's possible to override more .DumpJson() with different signatures if necessary.
As of version 4.47, LINQPad has the ability to export JSON built in. Combined with the new lprun.exe utility, it can also satisfy your needs.
http://www.linqpad.net/lprun.aspx