How to check in sikuli that image is exist in screen or not and perform if else condition on that result - sikuli

I am trying to automate a desktop application in sikuli. What i am doing copying data from from existing user and creating new user using some of that data. In form there are two check boxes. If that check boxes is ticked for existing user, then while creating new user i need to check the text box. for that am taking checked text box image and giving in if condition. If checked text box image is there in that page i will carry value 1 to a variable else value will be 0. according to that value am performing check uncheck function in new user creation page. But the issue what i am facing is, am not able to check if the image exist in that page or not in sikuli. Please anybody help me. my code is giving below
int bomanager=0;
int boswitchboard=0;
System.out.println("boswitchboard value before assign it to 1" + bomanager);
if (screen.exists("images/backofficeswitchboardwithtick.png") != null)
{
boswitchboard=1;
System.out.println("boswitchboard value after assign"+boswitchboard);
}
System.out.println("bomanager value before assign it to 1" + bomanager);
if(screen.exists("images/backofficemanagerwithtick.png") != null)
{
bomanager=1;
System.out.println("bomanager value after assign it to 1"+bomanager);
}
then using this value need to perform below function.
System.out.println("Before condition" + bomanager);
if (bomanager ==0){
screen.click("images/backofficemanagerwithtick.png");
}
screen.setAutoWaitTimeout(10);
System.out.println("Before condition" + boswitchboard);
if(boswitchboard==0){
System.out.println("Inside To tick Condition" + boswitchboard);
System.out.println("Ticking the SwitchBorad when itsnot already ticked");
screen.click("images/backofficeswitchboardwithtick.png");
}

I'm asssuming you're looking to use "if exists" method here
if exists(img, timeout):
click(img)
else:
....

with the method exists() I usually use:
if(exists("yourUrlImage")!=null):
(do something when exists)
else:
(do another thing when not exists)
Because that will return a "match" object.
Hope this helps

Related

Can I include conditional logic in VS Code snippets?

I would like to write a snippet in VS Code that writes a "switch" expression (in Javascript), but one where I can define the number of cases.
Currently there is a snippet that produces the outline of a switch expression with 1 case, and allows you to tab into the condition, case name, and the code contained within.
I want to be able to type "switch5" ("5" being any number) and a switch with 5 cases to be created, where I can tab through the relevant code within.
I know the snippets are written in a JSON file, can I include such conditional logic in this, or is it not possible?
Thanks!
The short answer is that you cannot do that kind of thing in a standard vscode snippet because it cannot dynamically evaluate any input outside of its designated variables with some limited workarounds like I'll mention next.
You might - I and others have written answers on SO about his - type your various case values first and then trigger a snippet tat would transform them into a switch statement. It is sort of doing it backwords but it might be possible.
There are extensions, however, that do allow you to evaluate javascript right in a snippet or setting and output the result. macro-commander is one such extension. I'll show another simpler extension doing what you want: HyperSnips.
In your javascript.hsnips:
snippet `switch(\d)` "add number of cases to a switch statement" A
``
let numCases = Number(m[1]) // 'm' is an array of regex capture groups
let caseString = ''
if (numCases) { // if not 'switch0'
let tabStopNum = 1
caseString = `switch (\${${tabStopNum++}:key}) {\n`
for (let index = 0; index < m[1]; index++) {
caseString += `\tcase \${${tabStopNum++}:value}:\n\t\t\$${tabStopNum++}\n`
caseString += '\t\tbreak;\n\n'
}
caseString += '\tdefault:\n'
caseString += '\t\tbreak;\n}\n'
}
rv = `${caseString}` // return value
``
endsnippet
The trickiest part was getting the unknown number of tabstops to work correctly. This is how I did it:
\${${tabStopNum++}:key}
which will resolve to ${n:defaultValue} where n gets incremented every time a tabstop is inserted. And :defaultValue is an optional default value to that tabstop. If you don't need a defaultValue just use \$${tabStopNum++} there.
See https://stackoverflow.com/a/62562886/836330 for more info on how to set up HyperSnips.

Is there a way I can check to see if the "go to page based on answer" is checked in Google Forms

When using .setChoices() It looks for an array of created choices. If it is a multiple choice question it can be formatted either createChoice(value) or createChoice(value, navigationType).
When looking at a choice you put in something like: var cPage = chkItem.getChoices()[j].getGotoPage();
This will produce either the page object or a Null value.
The problem is when a question is set to "go to page based on answer" and they have not set a page for every entry, and left the default to continue it also reads it (in my case cPage), as null. This means if I want to go through each choice to capture it, modify it, then push it back out to the question, it my ending Array that I push out consists of both Null and Objects, which produces an error.
My workaround for questions without pages is for the script to forcibly change the question to handle page navigation but set everyone to CONTINUE.
I would like to find a way to check if the question has "go to page based on answer" checked and if not then be able to create choices using just the value.
Figured it out: I need to check the first choice if it has a PageNavigationType(). If it does then the box is checked. If null then it is not checked. I have not written the code to fully test this but the theory should work. (I was checking against just the goToPage type which is why it didn't work before. )
var choicecount = chkItem.getChoices().length-1;
var hasNavType = chkItem.getChoices()[0].getPageNavigationType(); //if null
for (var j = 0; j <= choicecount; ++j) {
var cValue = chkItem.getChoices()[j].getValue();
var cPage = chkItem.getChoices()[j].getGotoPage();
var cNav = chkItem.getChoices()[j].getPageNavigationType();
// Logger.log(j+" "+cPage);
if (cValue != reValues[0]){
if (hasNavType == null){
newChoices.push(chkItem.createChoice(cValue));
} else {
if (cPage == null){
newChoices.push(chkItem.createChoice(cValue,cNav));
} else {
newChoices.push(chkItem.createChoice(cValue,cPage));
}
}
}
}

Creating a user generated list in flash

I'm trying to create a flash application that will keep track of user generated values. The app should basically allow the user to input the name of the item and it's cost. The total costs should then be added up to show a total value to the user. I can probably figure out how to add the values together, but I'm not really sure how to allow the user to create a list and then allow the user to save it. Can anyone point me towards a tutorial or point me in the right direction?
I am using variables to add user inputed numbers to come up with a total. The first problem is that actionscript 3.0 does not allow variables for texts. I just converted it to 2.0 to fix this. The second problem, is when I test the app and put in my values and click submit, I get NaN in the total values field. Is there a reason why it wouldn't add the values?
Here is the code I used for the submit button:
on (release) {
total = Number(rent) + Number(food) + Number(travel) + Number(entertainment) + Number(bills);
}
Am I missing anything?
Can I give the input text instance names and then give them variables? How are some ways to go about this?
Thanks for the help!
Have an object array, say for example
var stack:Array = new Array();
Then push the item name and it's cost to that array when user inputs, like
stack.push({item:AAA, cost:xx});
So that you can generate the list whenever you want with that array.
You have to see how this works in code. A list in actionscript could be stored inside an array, vector, dictionary or even an Object.
Var myList:Array = [];
myList.push({name: "item 1", cost: 5 });
myList.push({name: "item 2", cost: 7.5 });
If you want to grab the 'product' of "item 1" from the list, you have to create a function for that, lets call it getProductByName
function getProductByName(name:String):Object
{
for each(var product:Object in myList)
{
if (product.name === name) return product;
}
return null; // no match found
}
You can call that function like this:
var product = getProductByName("item 1");
trace(product.cost); // 5
And you can alter the product, so lets make it more expensive
product.cost += 1;
trace(product.cost); // 6
Have fun! If you are using classes, you would create one for the product, with public name and cost, and in that case you'de better use a vector, to ensure working with the right type.
This is what fixed the issue for me in action script 3.0:
myButton.addEventListener(MouseEvent.CLICK, addThem);
function addThem(e:MouseEvent)
{
totalField.text = String ( Number(field1.text) + Number(field2.text) + ....);
}
I also had to name the instances appropriately.

AdvancedDataGrid: dataTip = headerText

I have a pretty straightforward question which google fails to give me the answer to :
I have an AdvancedDataGrid where i build the columns dynamically (variable number of columns) in ActionScript, and i want the dataTip to display the column headerText when the user hovers over a cell. Adobe's example dataTipFunction:
private function tipFunc(value:Object):String
{
if (value is AdvancedDataGridColumn)
return "Column Name";
// Use the 'name' property of the data provider element.
return "Name: " + value["name"];
}
But in this case the value is only an AdvancedDataGrid column if the user hovers over a column header? I want the dataTip to always show the headerText for that column. So if i have to use this function, then how do i get the column headerText for a cell?
And as i understand the dataTipField i can't really use it to statically equal column.headerText (dataTipField = headerText).
Anyone have any pointers on how i can achieve this? It seems like a really easy task, still i can't seem to find out how :)
You can use a different function per column, which could be anonymous:
<AdvancedDataGridColumn dataTipFunction="{function(value:Object):String{return 'Data Tip'}}" ... />

Select statement selection through URL parameters

I'm attempting to alter the contents of certain parts of a HTML form through usage of the URL. For a text field, I'm aware that this will suffice,
http://<domain>?fieldname=ping&anotherfield=pong
On the form there are multiple select braces (drop down boxes); Is it possible to pick an int or string value through the url for this?
There seems to be little documentation on this (or even people trying to do the same)...
You haven't specified how you want to do this, but I'll assume that you want to use JavaScript:
To get a value from QueryString:
getQueryStringArgument = function(key) {
var hu = window.location.search.substring(1);
var gy = hu.split("&");
for (i = 0; i < gy.length; i++) {
var ft = gy[i].split("=");
if (ft[0] == key)
return ft[1];
}
}
To set the selected value of the select list:
document.getElementById("sel").value = getQueryStringArgument("id");
For a text field, I'm aware that this will suffice
No, it won't (at least, not in a generic way).
For a text field, the default value is specified by the value attribute. There might be a server side script that populates it based on query string data, but there doesn't have to be.
On the form there are multiple select braces (drop down boxes); Is it possible to pick an int or string value through the url for this?
Again, this requires an attribute to be set (selected on <option>), and that could (again) be set by a server side script based on the query string data.