Suppose if I want to press Delete button 9 times in my CODED UI code, is there any shortcut or do I need to give keyboard control 9 times in code? - coded-ui-tests

Suppose if I want to press Delete button 9 times in my CODEDUI code, is there any shortcut or I need to give keyboard control 9 times in code?

You can use a for loop to loop through clicking the control 9 times:
for(var i = 0;i < 9;i++)
{
Mouse.Click(UiControl.DeleteButton);
}
You can place this in a method in your .UiMap file, or in your testmethod.
Some unasked advice: If you want to click delete 9 times because you have 9 items you want to delete 9 items from a table or another element you could try to get the count of the children:
var deleteNum = UiControl.Table.GetChildren().Count;
for(var i = 0; i < deleteNum; i++ )
{
Mouse.Click(UiControl.DeleteButton);
}
This will help as you won't use the control more than it should, and will make sure your testmethods are more robust against changes in the application.

Here's a click example using WinAPI - since it's faster than control identification.
public static class AppManager
{
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
[DllImport("user32.dll",
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(long dwFlags,
long dx,
long dy,
long cButtons,
long dwExtraInfo);
public static void RapidClick(Point pt)
{
Cursor.Position = pt;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, pt.X, pt.Y, 0, 0);
}
}
Then to use RapidClick, I do this:
var point = new Point(control.Left+1, control.Top + 1);
for (int pos = 0; pos < totalClicks; pos++)
{
AppManager.RapidClick(point);
}
The benefits of this is that the performance is mindblowingly fast. This is because you do a lookup to the control once to get the initial co-ordinates, after that you can rapid click it reliably.
This has worked for me locally, and also no issues with RDC or in labs environment too.

Related

Non-blocking read of /dev/input/mice in java

I am trying to poll a mouse and update its position in Java using "/dev/input/mice". However right now I cannot figure out a way to determine if there is new data. How do I read an input file in a non-blocking way?
public class Mouse {
String MOUSE_STREAM = "/dev/input/mice";
BufferedInputStream bis;
private Mouse(){
bis = new BufferedInputStream(new FileInputStream(new File (MOUSE_STREAM)));
}
public Vect getDelta() {
int sumX = 0;
int sumY = 0;
// This is what I want, but available always returns 0;
// while (bis.available() > 3) {
// Otherwise, If I don't check then .read() will block until the next event
int buttons = bis.read();
int x = bis.read();
int y = bis.read();
sumX += x;
sumY += y;
}
return new Vect(sumX, sumY);
}
}
PS I need to be using a very low level interface because I'm operating in a Real-Time version of linux without much support for higher level libraries.

Count the number of frequency for different characters in a string

i am currently tried to create a small program were the user enter a string in a text area, clicks on a button and the program counts the frequency of different characters in the string and shows the result on another text area.
E.g. Step 1:- User enter:- aaabbbbbbcccdd
Step 2:- User click the button
Step 3:- a 3
b 6
c 3
d 1
This is what I've done so far....
public partial class Form1 : Form
{
Dictionary<string, int> dic = new Dictionary<string, int>();
string s = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
s = textBox1.Text;
int count = 0;
for (int i = 0; i < s.Length; i++ )
{
textBox2.Text = Convert.ToString(s[i]);
if (dic.Equals(s[i]))
{
count++;
}
else
{
dic.Add(Convert.ToString(s[i]), count++);
}
}
}
}
}
Any ideas or help how can I countinue because till now the program is giving a run time error when there are same charachter!!
Thank You
var lettersAndCounts = s.GroupBy(c=>c).Select(group => new {
Letter= group.Key,
Count = group.Count()
});
Instead of dic.Equals use dic.ContainsKey. However, i would use this little linq query:
Dictionary<string, int> dict = textBox1.Text
.GroupBy(c => c)
.ToDictionary(g => g.Key.ToString(), g => g.Count());
You are attempting to compare the entire dictionary to a string, that doesn't tell you if there is a key in the dictionary that corresponds to the string. As the dictionary never is equal to the string, your code will always think that it should add a new item even if one already exists, and that is the cause of the runtime error.
Use the ContainsKey method to check if the string exists as a key in the dictionary.
Instead of using a variable count, you would want to increase the numbers in the dictionary, and initialise new items with a count of one:
string key = s[i].ToString();
textBox2.Text = key;
if (dic.ContainsKey(key)) {
dic[key]++;
} else {
dic.Add(key, 1);
}
I'm going to suggest a different and somewhat simpler approach for doing this. Assuming you are using English strings, you can create an array with capacity = 26. Then depending on the character you encounter you would increment the appropriate index in the array. For example, if the character is 'a' increment count at index 0, if the character is 'b' increment the count at index 1, etc...
Your implementation will look something like this:
int count[] = new int [26] {0};
for(int i = 0; i < s.length; i++)
{
count[Char.ToLower(s[i]) - int('a')]++;
}
When this finishes you will have the number of 'a's in count[0] and the number of 'z's in count[25].

SpriteBatch.Draw in rectangle

I want to make something like Terraria item sidebar thing. (the Left-top rectangles one). And here is my code.
Variables are
public Rectangle InventorySlots;
public Item[] Quickbar = new Item[9];
public Item mouseItem = null;
public Item[] Backpack = new Item[49];
public int selectedBar = 0;
Here is the initialization
inventory[0] = Content.Load<Texture2D>("Contents/Overlays/InventoryBG");
inventory[1] = Content.Load<Texture2D>("Contents/Overlays/InventoryBG2");
update method
int a = viewport.Width / 22;
for (int b = 0; b <= Quickbar.Length; ++b)
{
InventorySlots = new Rectangle(((a/10)*b)+(b),0,a,a);
}
draw method
spriteBatch.Begin();
for (int num = 0; num <= Quickbar.Length; ++num )
spriteBatch.Draw(inventory[0], InventorySlots, Color.White);
spriteBatch.Draw(inventory[1], InventorySlots, Color.White);
spriteBatch.End();
Yes it is not done, but when i try to run it, the texture didn't show up.
I am unable to find out what is wrong in my code.
is it in with SpriteBatch? In the draw method? or In the Update?
Resolved
The problem isnt at the code Itself. the Problem is in this:
int a = viewport.Width / 22;
The thing is, i trought that viewport in here (I've used a Starter Kit) is the Game Window!
You are assigning InventorySlots overwriting its content...
also it seems that you want to draw two sprites... but you are drawing only one inside the loop... and your looping over Quickbar when seems that its not related with your drawing calls.
And it seems that your slot layout calculations have few sense...
You should use an array or a list:
public List<Rectangle> InventorySlots = new List<Rectangle>();
// You put this code in update... but it's not going to change..
// maybe initialize is best suited
// Initialize
int a = viewport.Width / 22;
InventorySlots.Clear();
for (int b = 0; b < Quickbar.Length; ++b)
{ // Generate slots in a line, with a pixel gap among them
InventorySlots.Add( new Rectangle( 1 + (a+2) * b ,0,a,a) );
}
//Draw
spriteBatch.Begin();
for (int num = 0; num < InventorySlots.Count; ++num )
{
spriteBatch.Draw(inventory[0], InventorySlots[num], Color.White);
spriteBatch.Draw(inventory[1], InventorySlots[num], Color.White);
}
spriteBatch.End();

How to set the selection items for CComboBox to be the CString array all at once?

I am look for the combobox to display 4 rows where the first row shows "a", 2nd row shows "b"..."c"..."d"
cb1 = new CComboBox;
cb1->Create( WS_VSCROLL | CBS_DROPDOWN | WS_VISIBLE | WS_BORDER, CRect(20,200,200, 300), this, 30 );
CString itemSet[] = {"a","b","c","d"};
//I am to set the array all at once with out doing each itme ??
cb1.AddString(itemSet); //fails
There's no function to do so in one go. You could do as Jeeva suggest, a simple loop traversing your array:
CString itemSet[] = {"a","b","c","d"};
for (int i = 0; i < _countof(itemSet); ++i)
{
cb1.AddString(itemSet[i]);
}
However, if you are going to use it often, you could create your own CCombobox derived class and add a function that does it.
class CMyCombo : public CCombobox
{
public:
CMyCombo();
void AddStrings(const CString* strings, int num);
// ...
}
void CMyCombo::AddStrings(const CString* strings, int num)
{
for (int i = 0; i < num; ++i)
{
cb1.AddString(strings[i]);
}
}
Actually, I would probably use a container, such as std::vector or CStringArray, but you get the idea.
By the way, if you are using strings that could be localized, you should not rely on strings only. A better approach can be found here.
One last thing: there's usually no need to create controls on the fly. It's usually easier to create member variables for them.
Do something like this
CString arr[2] = {_T("A"),_T("B")};
for(int i =0 ;i <2; i++)
{
m_ctrlCombo.AddString(arr[i]);
}

how do you disable a button based on a tag?

i need assistance with this code. not sure how to write a code to disable/enable the button based on a tag.
i tried to use "[levelMenu setIsEnabled:false];" but all the buttons are disabled.
//in my init method
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"texture.plist"];
CCSpriteBatchNode *colorSprites = [CCSpriteBatchNode batchNodeWithFile:#"texture.png"];
[self addChild:colorSprites];
CCMenu *myMenu = [CCMenu menuWithItems:nil];
int rows = 2;
int cols = 4;
static int padding=20;
for(int count=0; count < (rows*cols); count++) {
int index1 = count +1;
NSString *spritefiles1 = [NSString stringWithFormat:#"sprite%d.png", index1];
random1 = [CCSprite spriteWithSpriteFrameName:spritefiles1];
CCMenuItemSprite *levelMenu = [CCMenuItemSprite itemFromNormalSprite:random1 selectedSprite:[CCSprite spriteWithFile:#"Iconselected.png"] disabledSprite:[CCSprite spriteWithFile:#"Iconlocked.png"] target:self selector:#selector(levelSelected:)];
int yOffset = padding+(int)(random1.contentSize.width/2-((random1.contentSize.width+padding)*(count/rows))); // or /cols to fill cols first
int xOffset = padding+(int)(random1.contentSize.width/2+((random1.contentSize.width+padding)*(count%rows))); // or %cols to fill cols first
levelMenu.position = ccp(xOffset, yOffset);
levelMenu.tag = count+1;
myMenu.position =ccp(0,300);
[myMenu addChild:levelMenu];
//testing to disable the button
// [levelMenu setIsEnabled:false];
}
-(void) levelSelected:(CCMenuItemSprite*) sender {
int level = sender.tag;
NSLog(#"test button number %d",level);
}
You may want to read this post
In Cocos2d, with buttons on multiple layers, how can I control which button to react to user's touch?
I tried, it worked for me
[_fireButton setIsEnabled:NO];

Resources