Coded UI Test without Element Detection - visual-studio-2010

Is there any way to create a coded UI test for a WPF application that only detects the parent window element? We are using a component suite that does not support Coded UI tests, but I would still like to be able to automate the UI for testing purposes. Ideally, such a solution would detect the parent window element, then use pixel offsets for automating any button presses, etc.
Thanks.

You can use the Coded UI Test Builder to discover the properties you need, but the basic method for getting a window is pretty simple.
Here's a really simple class representing a window:
using System;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.WpfControls;
public class MyWpfWindow : WpfWindow
{
public MyWpfWindow() : base()
{
this.SearchProperties[WpfWindow.PropertyNames.Name] = "My Wpf Window Name";
}
}
And here's a test
[CodedUITest]
public class MyWpfWindowUITests
{
[TestMethod]
public void MyWpfWindowExistsTest()
{
Assert.IsTrue(MyWpfWindow.Exists(5000), "The window was not found within five seconds");
}
}

While technically it is possible (Mouse.Click has overloaded version with x,y coordinates), it is not a good idea. Any change in control layout will break your test.

http://social.msdn.microsoft.com/Forums/en-US/a2c585ee-8db0-48c0-8825-fb1865631977/how-do-i-click-checkbox-inside-drawhighlight-method
This link has the code to click on the coordinates within a defined highlighted area

Related

How do I make a ProgressBar with rounded corners using Xamarin Forms

So my goal is to create a ProgressBar with rounded corners. This seems to be way harder than what it should be. I've tried looking at examples on StackOverflow but most of them are outdated or is referring to Android Studio which is NOT what I am working with.
As far as I know, I need to create a whats called "Custom Renderer" which is essentially a class that inherits from ProgressBarRenderer. And then change most of the properties there, which seems kinda silly since the UI should be done using XAML in my opinion.
I keep running into multiple issues when trying to create the custom renderer.
The first one being this
I've already installed Xamarin.Forms and it still keeps throwing that error.
And then there is a issue regarding the constructor. The constructor that doesn't take any parameters is obsolete and doesn't work anymore and it requires me to add a Context which I can't either because it throws the same error as above.
How do I properly create a progressbar with rounded corners and text in the middle indicating how much % it's at?
You should add proper reference to use the ProgressBarRenderer:
using App498.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
The custom renderer should look like this:
[assembly: ExportRenderer(typeof(Xamarin.Forms.ProgressBar), typeof(myProgressBarRenderer))]
namespace App498.Droid
{
public class myProgressBarRenderer : ProgressBarRenderer {
public myProgressBarRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
{
base.OnElementChanged(e);
}
}
}
If you want to add round corner to progress bar, check this thread.
I would refer you to SkiaSharp, graphics library, that enables you to create... well everything.
See the documentation here:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/
You'll need to play around for a while to get used to work with it, but it's quite intuitive and easy to work with.
To achieve round corners, you'll need to work with paths, described in here:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/curves/clipping
By a chance I also needed a progress bar in my app. I've drawn a picture with transparent line within for the progress to be shown, merged it with green rectangle (the progress itself) and voila, you've got the progress bar. When you need to update the progress, you just redraw the progress bar.
There are surely plenty of other solutions, but this one I reccomend as I know it will work fine.
Hapyy coding ;)

Where should a game logic be within a JavaFx application?

Being new to Javafx and java, I'm having trouble picturing the design for a chess game.
So I have the start method like following in my JavaFX Application-extended class:
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(createContent());
primaryStage.setTitle("ChessGame");
primaryStage.getIcons().add(new Image("file:images/icon.png"));
primaryStage.setScene(scene);
primaryStage.show();
}
protected Parent createContent(){
Pane root = new Pane();
root.setPrefSize(WIDTH*TILE_SIZE, HEIGHT*TILE_SIZE);
boardInitialize("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -");
root.getChildren().add(tileGroup);
root.getChildren().add(pieceGroup);
return root;
}
The problem is, I don't know where to put the game's logic inside my application. The game logic will handle player's turn, check for checks and checkmates, generate possible moves, etc (and later a very crude AI if possible). I have tried to jam it into the start method, but it doesn't work because the start method only runs once. Pygame with their gameloop makes much more sense than this and I can see how I would go with it. So my question is: where do I put the game logic in my application?
You may use the Model-View-Controller (+ Network if needed) architecture to design your system. JavaFX is really useful when working with the MVC.
Rules of Thumb:
1- Do not put your game logic inside the Model, Network and View(FXML) classes.
2- Use FXML as part of the design (it will bootstrap your work).
3- Try to achieve " Low coupling & High Coherence "
4- Some example from a fully-working MVC JavaFX project.
> /* WelcomeScreenController class (interacts with the FXML file)*/
> #FXML
> void doSignup(ActionEvent event) {
>
> user = username.getText();
> pass = password.getText();
>
> if(user != null && !user.isEmpty() && pass != null && !pass.isEmpty())
> if (checkBox.isSelected())
> GameEngine.game().getAccessManager().callSignupService(user,pass);
> else
> showNotification("You need to accept terms.");
> }
5- Try to handle button logic from the Controller class of the particular FXML file. http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
6- Create a controller class named "GameEngine" to do all your computations about the game logic. Call GameEngine's methods from the button's handle action event method.
7- Try to use Object-Oriented Programming methodologies. (Polymorphism, Inheritance, Design Patterns, etc.)
I would stand upon the shoulders of others and use a library that already has some structure in it.
With that you can follow the model that build upon the experience of others and use their best practices and avoid the pitfalls they have experienced.
For JavaFX I am aware of the FXGL library, which comes with a nice list of examples of how to build a game in JavaFX.
On YouTube you can find several examples and tutorials for this library.
First of all, I would like to let you know that I have never written a Javafx application and I could be wrong.
The start method, as you correctly pointed out is the main entry point of a Javafx application, as you can see from the anatomy of a Javafx application described here. You can initialize your game there, but naturally, the moves will occur later than running the start. You will need to use event handling to handle the case when a move is being attempted by a player.
Once you are able to handle the move events, you will also be able to check whether a move is correct or not, or, to check the possible valid move options of a given player. I would like to suggest that you should have your own classes for the business logic and to avoid mixing business logic with event handling, even though the event handler will invoke business logic methods.

deal with Unity 5 UI in augmented reality app

I'm trying to make an augmented reality application with vuforia and unity.
whenever it recognize the image target, it must tell a story by showing text , and it should enable the user to press next and back to go on reading the different parts of this story, I'm totally new to unity and don't know how to handle with UI throughout scripting, I need some help on how to accomplish the part of "going forward and backward on showing the story by hitting Next and Back buttons", and all these parts of story should be related to the same image target in the same scene.
I appreciate it if you help me with an example code.
You should create some script that attach on trackable object, maybe something like this.
public class DataBook {
string[] dataBook;
string idText;
bool isActive;
}
Then you must create another script to set that trackable object is active or not, this link can help you to get that.
https://developer.vuforia.com/forum/faq/unity-how-do-i-get-list-active-trackables
Then after you get the active trackable object, you can set the dialog from the book by create another controller script for button, example
public void Next() {
DataBook[] books = FindObjectsOfType<DataBook>(); // if the object more than one, it will be more easy if it only the one
foreach (var book in books)
{
if (book.isActive) {
book.idText += 1;
textUI.text = book.dataBook[idText]; //textUI assign to object text on canvas
}
}
}
you can learn about unity UI Button on this :
https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
Good luck

Unity GUI Text won't work

I have the following problem, and I'd appreciate greatly if someone could help.
I've been trying create a roll-a-ball game in Unity, similar as the one presented in one of the tutorials on their official website, but with one difference: I'd like to add GUI text that would show the score. Every time the ball hits (and destroys) one of the rotating cubes, the score should increase by 1, and the text should show the current score.
I've watched the following tutorial:
http://unity3d.com/learn/tutorials/projects/space-shooter/counting-points
I have created the UI text, called ScoreText, and, on the other hand, I have written the script that makes an instance of GUIText class, called guitext, show the current score. However, I cannot connect my particular GUI text (ScoreText) with the script, i.e. I cannot tell the script to use that particular GUI text to show score. In the video, at 16:35, they just drag the GUI text to the instance of the GUIText class from the script (in my case, guitext), in order to tell the script it's that particular text that should show the score. But when I try to drag it, I simply can't do it. I have no idea why.
I don't know if this could cause the problem: under "Create" in the free version of Unity, I could not find anything called "GUI text" but instead I created a UI text. I know it's the same thing, but... In the script, I define an instance of GUIText class - guitext - so maybe, when I try to add my UI text to it, it won't work because my text really doesn't belong to the GUIText class?
Since you are using Unity 4.6 which has a new GUI system and not the Legacy UI which was present when the tutorial which you mentioned was made. There are two options - either you create an empty gameobject and add add a GUIText component to it. You can add the GUIText to empty gameobject by selecting it in the Hierarchy which should display the objects properties in the Inspector panel.
In the Inspector, you should see an Add Component button, click on it and search for GUIText. Add it and that should solve your problem.
Oh yea, the second option is to migrate to Unity 4.6 new GUI, which should take time unless you are pretty good with unity.
If you want to learn Unity 4.6 GUI, you can refer these links
Unity Official
Unity 4.6 tutorials by TheGameContriver
I am only posting on this because many come to this one and get lost. Here is how to make this work in Unity5.3 at the time of this response.
This is in C#
Simply use the GameController game object to attach the script to. Second create a UI text. Toy only call it score or scoretext for your own referral otherwise the name itself does not matter.
using UnityEngine;
using UnityEngine.UI;///needed to reference the new UI setup in this script type
using System.Collections;/// <summary>
/// allows collections of related information.
/// </summary>
public class GameController : MonoBehaviour {
public GameObject targets;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public Text Text; /// <summary>
/// change this because you dont actually use the words UI or GUI. Thats just for the reference and not clarified by instructors.
/// </summary>
private int score;
void Start()
{
score = 0;
UpdateScore();
StartCoroutine(SpawnWaves());
}
IEnumerator SpawnWaves()///this depends on the above using System.Collections
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(targets, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
Text.text = "Score: " + score;
}
}
Confirmed. In the GameController.cs file, change the declaration of public GUIText scoreText to public Text scoreText, and then it will let you drag it on the Game Controller as the video shows.
Update your Unity tutorials, peoples! Just spent an hour trying to figure this out!
One more thing...check the Z position. My UI looked perfect head on in the scene view, but my text never showed in the game...then I rotated the scene view and found my text was way behind the camera. Seems when you add something to the UI, the z value is somewhat random.

SwitchTo method for Coded UI

I am working on a, hand coded, Coded UI test for an application.
The application has a certain function that operates in 2 windows. When a button is clicked, a new window appears and then the user makes a selection upon which that window closes, and then the user continues taking actions on the main window.
With Selenium, I would handle that by iterating through all the window handles and switching by using passing it the URL of the page I wanted using the "driver.SwitchTo().Window(handle)" method. However, with Coded UI, I have not found a similar solution. Using the Process class I can do something similar that could work:
Process[] myList = Process.GetProcessesByName("iexplore");
foreach (Process item in myList)
if (item.MainWindowTitle.Contains("Window Title"))
{
item.Kill();
}
The problem is that the application that I am testing is poorly designed and all windows throughout the application have the same name, thus it will not work.
Is there a method which can be used to switch to a different window on Coded UI? Or what would be the best approach?
Take a look at this question, it might help: Interacting with multiple instances of an application in Coded UI
You don't do "switching" in CUIT, every window and control is accessed via a UITestControl object. If you want to do stuff on an other window you create a new object for it. If you can't differentiate the two windows with search properties you can use the Instance property or FindMatchingControls method.
To catch a window creation event you can use a winhook. It gives you the window handle of every window created. Using that you can find out if the window created is the window you are waiting for and then use the UITestControlFactory.FromWindowHandle to create a UITestControl for CUIT to interact with. If you have a generated class for that window you can create an instance of that class and call its CopyFrom method to pass to it the control you created from the window handle.
I am using:
public static HtmlDocument WaitForPopup(string popupName, string text)
{
BrowserWindow browser = new BrowserWindow();
browser.SearchProperties.Add(BrowserWindow.PropertyNames.Name, popupName,
PropertyExpressionOperator.Contains);
browser.WindowTitles.Add(popupName);
browser.WaitForControlExist();
browser.WaitForControlReady();
browser.SetFocus();
HtmlDocument html = new HtmlDocument(browser);
html.SearchProperties.Add(HtmlDocument.PropertyNames.InnerText, text,
PropertyExpressionOperator.Contains);
html.WaitForControlExist();
html.WaitForControlReady();
Playback.Wait(1000);
return html;
}
Just call WaitForPopup("your window name", "some constant text in window") and continue with tests there.
Note that this method will return html of the new window and you can use it further on.

Resources