How to press BACKSPACE or ENTER multiple times in protractor - angularjs

I understand that we can initiate keypress events like Backspace or ENTER in protractor with the help of protractor.Key.BACK_SPACE or ENTER. But , how do i do this action multiple times? Is it just adding another line of it or any good approach towards it?

We have written a common functions to do the same.
exports.commonfunc = {
pressKey: function(key) {
switch (key) {
case 'Enter':
browser.actions().sendKeys(protractor.Key.ENTER).perform();
break;
case 'Backspace':
browser.actions().sendKeys(protractor.Key.BACK_SPACE).perform();
break;
}
},
pressKeyNtimes: function(key, n) {
for (i = 1; i <= n; i++) {
this.pressKey(key);
}
}
}
Now we using commonfunc in another class as below :
exports.Login = {
commonfunc: Object.create(require('../common/commonfunctions.js').commonfunc),
iClickEnterTwice: function(){
this.commonfunc.pressKeyNtimes('Enter',2);
}
}
Hope this helps!

Related

Compare values in an array using if conditions in a for loop

so I'm utilizing the Ocrad.js library in an ionic project to be able to read text from images. In the app, the user can add 'items' (as in words) to an array and then I would like to check whether or not these items (words) are present in the text from the image. For instance: if the image has a sentence: 'I like football' and the user added the word 'football' to the list, on the press of a button, the app would check whether or not 'football' exists in 'I like football' and would say so to the user. In an else occasion, it would also say so.
So far I came up with this situation:
for(let item of this.itemList) {
if(text.indexOf(item.name)>=0){
alert('word found');
} else {
alert('word not found');
}
}
The idea was to loop through the list of items(words) that the user had added to the array and give them the appropriate response. While it is working if I add just one word, like the football example I mentioned above, if I add more words to the list, the loop will obviously give me the 2 alerts. So if add the word 'soccer' to the list and therefore have an array with 'football' and 'soccer', I would get the 2 alerts, which makes sense because I wasn't stopping the loop. So I tried a switch case, which did not work for some reason (I don't really know why, but it kept giving me the two alerts). This is what my switch looked like:
for(let item of this.itemList){
switch(true){
case (text.indexOf(item.name)>=0): {
alert('word found');
break;
}
case (text.indexOf(item.name) <= 0):{
alert('word not found');
break;
}
default: {
alert('please add a word to the list');
break;
}
}
}
So after playing around and researching, I could not really find something that helped me very well. The idea again is if the image text says 'I like football' and I add 3 items to the array: 'soccer', basketball', 'football', the answer would be 'word found' only, whereas if the word is not there, I would get 'word not found' only once! I believe I might be doing something really stupid that I can't see with my noobs' eyes haha. I hope my question made sense. Can anyone help me with this? Cheers guys!
Edit - This is the function I am actually using with the camera feature:
onAnalyze() {
let matchFound = false;
let loader = this.loadingCtrl.create({
content: 'Please wait...'
});
loader.present();
(<any>window).OCRAD(document.getElementById('image'), text => {
loader.dismissAll();
alert(text);
console.log(text);
for(const item of this.itemList)
{
if(text.indexOf(item) >=0){
matchFound = true;
break;
}
}
if(found){
alert('word found!');
} else{
alert('word not found!');
}
}
);
}
In the first example you provided, the for-loop continues to evaluate the input string, even though you've already found a match (which I believe is what you want, to see only one alert for any number of successful matches). The below example introduces a boolean matchFound, which is set to true on the first match. If a match is found, we break out of the for-loop, terminating its execution (and preventing the N number of alerts):
const itemList = ['football', 'nachos', 'sports'];
const textFromImage = 'I like football and nachos';
isTextPresent(itemList, textFromImage);
function isTextPresent(itemList: string[], textFromImage: string) {
let matchFound = false;
for (const item of this.itemList) {
if (textFromImage.indexOf(item) >= 0) {
matchFound = true;
break;
}
}
if (matchFound) {
alert('word found');
} else {
alert('word not found');
}
}
FWIW, Array.prototype.some() could also be used here as well. On the first match from the textFromImage, it terminates (much like the for-loop that breaks in the first example):
const itemList = ['football', 'nachos', 'sports'];
const textFromImage = 'I like football and nachos';
isTextPresent(itemList, textFromImage);
function isTextPresent(itemList: string[], textFromImage: string) {
const matchFound = itemList.some((item: string) => {
return textFromImage.indexOf(item) >= 0;
});
if (matchFound) {
alert('word found');
} else {
alert('word not found');
}
}
You should use an nested for loop.
let words = ['soccer', basketball', 'football']
let foundedWords = []
for(let item of this.itemList) {
for(let word in words){
if(text.indexOf(word)>=0){
foundedWords.push(word)
alert('word found');
} else {
alert('word not found');
}
}
}
And finally you will get a list of founded words.
I think this should be fine for you.

angularjs stop incrementing on uncheck of checkbox

I have a checkbox that adds +2 if the user checks it. I need it to only do this one time. Right now the user can constantly add plus 2 which is not what I need. I need it to add it only once. (factors.Delivery is the ng-model)
I also need it to remove the --2 and stop if the checkmark is removed, but right now, it keeps subtracting.
Any help would be appreciated
Even disabling the check after clicking would be good, but it wont disable after clicking only before.
$scope.factors.Delivery = 0;
$scope.increment = function() {
if ($scope.factors.Delivery >= 0) {
$scope.factors.Delivery+=2;
} else {
$scope.factors.Delivery-=2;
}
};
2 is greater than or equal to 0. It sounds like the only acceptable values are 2 and 0 based on your post. I would suggest:
$scope.factors.Delivery = 0;
$scope.increment = function() {
if ($scope.factors.Delivery == 0) {
$scope.factors.Delivery=2;
} else {
$scope.factors.Delivery=0;
}
};`
or using your code, you can disable the checkbox with something like this:
ng-disabled="clickedOnce" on the checkbox and the modifications below.
$scope.factors.Delivery = 0;
$scope.clickedOnce = false;
$scope.increment = function() {
$scope.clickedOnce = true;
if ($scope.factors.Delivery >= 0) {
$scope.factors.Delivery+=2;
} else {
$scope.factors.Delivery-=2;
}
};

$scope.$watch callback executes after a delay

I have the following:
$scope.$watch('duration.dayPreference',function(value){
console.log(value);
if(value=='every')
{
that.duration.days = 1;
}
else if(value=='selected')
{
//alert('test');
that.duration.days=[];
}
else if(value=='everyday')
{
that.duration.days='everyday';
}
});
this.selectDay = function (day) {
$scope.duration.dayPreference = 'selected';
//$scope.$apply();
/*if(typeof(this.duration.days)!='object')
{
this.duration.days=[];
}*/
var index = this.duration.days.indexOf(day);
if (index == -1) {
//alert('test2');
this.duration.days.push(day);
}
else {
this.duration.days.splice(index, 1);
}
}
In this, when I do $scope.duration.dayPreference = 'selected'; I expect the line below it to have the this.duration.days set to a blank array. But it doesn't. Upon a closer inspection, I found that the callback in the $watch runs after the line below the assignment.
It may be very probable that, $watch may be using some kinda timers internally. What should be the way to do it then.
The watch won't be triggered until the digest is run. This will be after your entire function is compete.
If you consider that AngularJS is itself written in JavaScript, there would be no way for it to react to your setting of a property at the time. You are using the thread yourself. It can only wait for you to finish and then react.
As for what to do instead...
Perhaps you could call that watch function manually?
Or maybe the code which expects the empty array should belong inside the watch?
Watch will trigger on the $digest, which will occur after current cycle/code finishes running. You need to figure out a way of rearranging your code that handles things asynchronously. One possible quick solution might be:
var selectedDays = [];
$scope.$watch('duration.dayPreference',function(value){
console.log(value);
if(value=='every')
{
that.duration.days = 1;
}
else if(value=='selected')
{
//alert('test');
that.duration.days = selectedDays;
}
else if(value=='everyday')
{
that.duration.days='everyday';
}
});
this.selectDay = function (day) {
$scope.duration.dayPreference = 'selected';
var index = selectedDays.indexOf(day);
if (index == -1) {
//alert('test2');
selectedDays.push(day);
}
else {
selectedDays.splice(index, 1);
}
}

AS3 SQLite DELETE statement don't give result

When I delete row from DB, it deletes row, but I don't get event, that it deleted row (I need to refresh list after user delete something). Here are methods for handle results from DB:
private final function onReady(e:EventWithMessage):void
{
switch (action)
{
case "get files":
dispatchEvent(new EventWithMessage(EventWithMessage.FILES, {files: null}));
break;
case "save note":
dispatchEvent(new EventWithMessage(EventWithMessage.SAVED, { ID:e.message.ID } ));
break;
case "delete note":
// Saving and getting files above work! And this is not working
dispatchEvent(new EventWithMessage(EventWithMessage.DELETED, { } ));
break;
}
action = null;
}
private final function onData(e:EventWithMessage):void
{
DATA = e.message.data;
switch (action)
{
case "get files":
// some code was here
dispatchEvent(new EventWithMessage(EventWithMessage.FILES, {files: fileArray}));
break;
case "load note":
dispatchEvent(new EventWithMessage(EventWithMessage.FILE, { text:DATA[0].text } ));
break;
case "delete note":
// And even here it isn't working
dispatchEvent(new EventWithMessage(EventWithMessage.DELETED, { } ));
break;
}
action = null;
}
Method for DELETE:
public final function deleteNote(ID:int):void
{
SQLiteManager.SQLM.Operations(String("DELETE FROM Notes WHERE id = " + ID));
action = "delete note";
}
And methods in SQLiteManager (my class):
public final function Operations(command:String, parameters:Array = null)
{
statement = new SQLStatement ();
statement.sqlConnection = connection;
statement.text = command;
if (parameters && parameters.length > 0)
{
for (i = 0; i < parameters.length; i++)
{
statement.parameters[i] = parameters[i];
}
}
statement.execute (-1,responder);
}
Responder has two methods :
private final function handleSuccess (result:SQLResult):void
{
if (result.data)
{
dispatchEvent(new EventWithMessage(EventWithMessage.SQLDATA, { data:result.data} ));
}
else
{
dispatchEvent(new EventWithMessage(EventWithMessage.READY, { ID:result.lastInsertRowID } ));
}
}
private final function handleError (e:SQLError):void
{
dispatchEvent (new EventWithMessage(EventWithMessage.ERROR,{error:e.message}));
}
Is there any mistake or it just don't give any result after DELETE? Help please.
EDIT: While I wait for answer - I use timer (1 second after DELETE it refresh file list). Maybe it's an good alternative to event?
EDIT 2 action variable is null, when it comes to dispatch event O_o Why it is null? Oh my god.
RESOLVED!!! There was an event, I made some "trace" and found out that problem was in action variable, it was null, when event had been dispatched. I deleted lines action = null; and now everything works well. Hooray :)

Unity unityscript Error

I have an array learnnum that looks like [0,1,1,0,1,1,1,1,0].
I need to basically ask the user for an input Left Mouse Button or Right Mouse Button. If Left, then the values of learnnum of [i] is flipped, else nothing happens. I only do this for i=1,3,5,7. I have written the below code, but it does not work properly, instead of going for all the 4 conditions... it directly goes to 4. It seems that it is not waiting for the input conditions... Is there any way I can correct this?
function changeNumba(i)
{ //check1=true;
print ("PRINTT "+check1);
while(!Input.GetButtonDown("Fire1") && !Input.GetButtonDown("Fire2"))
{
if(Input.GetButtonDown("Fire1"))
{
check1++;
}
if(Input.GetButtonDown("Fire2"))
{
learnednum[i]=0 ? 1 : 0;
check1++;
}
}
}
function changelearn()
{
//FIRST STEP
//if(check1)
if(move1==9 && check1==0)
{changeNumba(1);
}
//SECOND STEP
if(move1==9 && check1==1)
{changeNumba(3);
}
if(move1==9 && check1==2)
{changeNumba(5);
}
if(move1==9 && check1==3)
{changeNumba(7);
}
}
var check1=0;
//1,3,5,7
function Update () {
if(move1==9)//this is just a game condition. Do not bother about it.
{
changelearn();
}
}
from looking at the unity script api:
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html
you should not have a while() loop inside of your Update() method.
change changeNumba() as follows:
function changeNumba(i)
{
if(Input.GetButtonDown("Fire1")){
check1++;
}
if(Input.GetButtonDown("Fire2")){
learnednum[i] = learnednum[i]==0 ? 1 : 0;
check1++;
}
}

Resources