Telerik Forums
Testing Framework Forum
6 answers
3.3K+ views

Hello!

Is it possible to retrieve the instance of the DataContext of a UserControl within a WPF Application?

I have tried it with FrameworkElement.DataContextProperty, but FrameworkElement.GetProperty(FrameworkElement.DataContextProperty) just get's the FullName of the DataContext as a string.

 

I am working with VS2015, .NET Framework 4.5.2, MSTest (switching to MSTest V2 once no longer a prerelease).

 

Here's my example code of a MVVM WPF-Application:

 

Default App.xaml and App.xaml.cs, with StartupUri="MainWindow.xaml"

Command and ViewModel, see http://docs.telerik.com/data-access/quick-start-scenarios/wpf/quickstart-wpf-viewmodelbase-and-relaycommand#implementing-viewmodelbase-and-relaycommand.

 

MainWindow.xaml: (default Code-Behind)

<Window x:Class="WpfApplication.MainWindow"
        xmlns:local="clr-namespace:WpfApplication"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:MyUserControl
            AutomationProperties.AutomationId="UserControlId" />
    </Grid>
</Window>

 

MyUserControl.xaml: (default Code-Behind)

<UserControl x:Class="WpfApplication.MyUserControl"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WpfApplication"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <local:MyDataContext />
    </UserControl.DataContext>
    <Grid>
        <StackPanel>
            <Button
                AutomationProperties.AutomationId="ButtonId"
                Content="Click"
                Command="{Binding ClickCommand}"
                CommandParameter="{Binding ClickCommandParameter}" />
        </StackPanel>
    </Grid>
</UserControl>

 

MyDataContext:

using System.Windows.Input;
 
namespace WpfApplication
{
    public class MyDataContext : ViewModel
    {
        public ICommand ClickCommand { get; }
        public string ClickCommandParameter { get; } = "clicked";
 
        public int Count { get; private set; }
 
        internal MyDataContext()
        {
            ClickCommand = new Command(OnClick);
        }
 
        private void OnClick(object obj)
        {
            Count++;
        }
    }
}

 

Unit Test (MSTest):

using System.IO;
using ArtOfTest.WebAii.Core;
using ArtOfTest.WebAii.Silverlight;
using ArtOfTest.WebAii.TestTemplates;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WpfApplication;
 
namespace UnitTests
{
    [TestClass]
    public class MyUnitTests : BaseWpfTest
    {
        public TestContext TestContext { get; set; }
 
        [TestInitialize]
        public void MyTestInitialize()
        {
            Initialize();
 
            string currentWorkingDirectory = Directory.GetCurrentDirectory();
            string path = Path.Combine(currentWorkingDirectory, "..", "..", "..", "WpfApplication", "bin", "Debug", "WpfApplication.exe");
 
            Manager.LaunchNewApplication(path);
        }
 
        [TestCleanup]
        public void MyTestCleanup()
        {
            CleanUp();
        }
 
        [TestMethod]
        public void MyTestMethod()
        {
            FrameworkElement userControl = ActiveApplication.MainWindow.Find.ByAutomationId("UserControlId");
 
            FrameworkElement button = userControl.Find.ByAutomationId("ButtonId");
            Assert.AreEqual("Click", button.GetProperty(new AutomationProperty("Content", typeof(string))));
            Assert.AreEqual("WpfApplication.Command", button.GetProperty(new AutomationProperty("Command", typeof(string))));
            Assert.AreEqual("clicked", button.GetProperty(new AutomationProperty("CommandParameter", typeof(string))));
 
            button.User.Click();
 
            object dataContext = userControl.GetProperty(FrameworkElement.DataContextProperty);
            Assert.AreEqual("WpfApplication.MyDataContext", dataContext); //pass
            Assert.IsInstanceOfType(dataContext, typeof(MyDataContext), "''" + dataContext + "''"); //fail
        }
    }
}

 

 

And are there any other ways to get and/or set a Property of a ViewModel alternatively?

From the code sample above, I would like to get the current value of MyDataContext.Count.

In my Unit Test, I want to Assert the incremented MyDataContext.Count property after each Button-Click.

 

Best wishes!

Elena
Telerik team
 answered on 09 Jan 2017
6 answers
283 views

Hello!

Is there a way to verify that e.g. a Mouse-Click via FrameworkElement.User.Click() throws a particular exception within a WPF Application?

Check within a Unit Test via e.g. MSTest's ExpectedExceptionAttribute or NUnit's Assert.Throws?

Best wishes!

Elena
Telerik team
 answered on 09 Jan 2017
1 answer
150 views

Hi,

I am trying to create a multilevel test for entering 100 records for a Client profile table through 3 different pages(each having some 30-40 fields) in a sequence.

Scenario (created a driver script for this) -

1. Login and initialize application

2. Call Test A (parent test - data bound to 100 records in excel)

3. Logout

 

Steps in Test A

1. Enter data in Page1 and continue to Pg2(Test B1)

2. Enter data in Page2 and continue to Pg3(Test B2)

3. Enter data in Page3 and continue to Pg4(Test B3)

4. Enter data in Page4 and continue to Pg4(Test B4)

5. Enter data in Page5 and save record(Test B5)

6. Navigate back to Page 1 for next iteration

 

My issue is:

1. Data for Test B1, B2 .. B5 are in 5 different sheets in excel. And I can bind the parent TestA to only one sheet at a time. So I consolidated all of them into one sheet. Is there any other solution to this?

2. InheritParentDatasource set to True for Tests B1-B5. Even after binding all the variable manually in the child tests, they are not entering the values from the parent table when the test is run

eg- Enter text in "ClientProfilePersonNameBirthNameTest" - DataDriven : [$(BirthName)] is shown when I bind the step in Test B1 to the BirthName variable coming from TestA but it doesn't actually enter that value during runtime. Iteration 1 in runtime log shows the value in this variable so its available to Test B1

Can you tell what the issue might be?

Thanks

 

 

Boyan Boev
Telerik team
 answered on 02 Jan 2017
5 answers
359 views
On the page when you click on the link - opens in a new tab

Can you please tell how to get the URL of a new tab
?
Elena
Telerik team
 answered on 22 Dec 2016
6 answers
383 views
Hello!

I would like to provide a self contained wrapper (licensed as non-viral open source) around the Testing Framework to serve as an adapter for an open source web control library. Since the wrapper library would be hosted in an open sourced repository, I would like for users of the library to be able to use the library without
a) running into version conflicts with the Testing Framework
b) not get Exceptions due to missing references because the Testing Framework is not installed.

I believe these issues could be solved if the Testing Framework where to be available as a nuget Package. Is there a way to accomplish this via nuget or an alternative means?

Best regards, Michael
Elena
Telerik team
 answered on 22 Dec 2016
3 answers
150 views

Hello,

I am trying to do something seemingly simple and that is to get the background color of a particular element in order to validate whether or not it is correct based on the business rules. The element in questions follows this configuration:

<input id="someID" class="there is some information here" tabindex="-32767" title="title" style="background-color: rgb(255, 50, 50); border-width: 2px; border-style: inset; border-image: none;" type="text">

 

I am not having any trouble accessing the element and its various attributes. However, regarding the style 'background-color', I get into trouble:

HtmlInputText el = <the element referenced above>;
var bgColor = el.GetStyle("background-color");    //  <---- Throws an exception (Invalid Operation Exception).

The problem is the style, "background-color" very clearly exists as you can see in bold above. However, I cannot figure out a reliable and elegant way to pull it out and get its value. Perhaps my approach to this problem is going down the wrong track?

Thanks for your help,

tsjax

Boyan Boev
Telerik team
 answered on 21 Dec 2016
1 answer
124 views

Hi,
Is there a resource for best practice when creating various types of automated test ?
Thank you !

Nikolay Petrov
Telerik team
 answered on 20 Dec 2016
1 answer
117 views

Hello Telerik, 

I am attempting to automate an HtmlInputNumber control. Can you please provide a working example of how this control can be automated?

My company appears to be using the HtmlInputNumer control in this fashion: 

1. 'Step up' (clicking up arrow in textbox UI) to a certain number

2. This 'step up' action once clicked, enables another button on the page. 

However, when I create the control using Telerik and attempt to use the HtmlInputNumberControl.StepUp(5) method - it only inputs the number, it doesn't enable any controls. So it appears that the step up control is not behaving as if it would truly behave in the UI. 

Thoughts? 

Lauren

Elena
Telerik team
 answered on 15 Dec 2016
1 answer
151 views

Trying to connect Samsung S5 device to Test Studio.  In my settings lie the Message Server Ip which is configured to "localhost" and the Message server port which is configured to "8084".

 

I click "Connect Device" to connect the device and the system reads my phone information via USB with the Port set to 8082, but I'm getting "device failed to connect on port 8082!"  I've tried every possible port, same result.  Out of ideas.  Helpppp

Elena
Telerik team
 answered on 12 Dec 2016
1 answer
161 views

Hi,

I wan to use Telerik Test Framework for WPF UI automation.

To find the controls I am using:

 return Get<ArtOfTest.WebAii.Controls.Xaml.Wpf.TextBlock>(
                            "XamlTag=contentpresenter",
                            "name=SelectedTabContent",
                            "|",
                      @"XamlPath=/grid[name=ContentPanel]/ribbonscrollviewer[name=TabItemsScrollViewer]/grid[0]/contentpresenter[name=Content]/itemspresenter[0]/ribbongroupspanel[0]/radribbongroup[0]/grid[0]/groupchrome[name=ExpandedChrome]/grid[0]/grid[name=Tray]/contentcontrol[0]/contentpresenter[0]/radribbongroup[automationid=GroupName]/grid[0]/groupchrome[name=ExpandedChrome]/grid[0]/grid[name=Tray]/contentcontrol[0]/contentpresenter[0]/textblock[0]");

Could you please let me know from where I can generate the XamlPath used above, or for that we need to use the paid functionality of DOM explorer?

Thanks,

Kanwar

Boyan Boev
Telerik team
 answered on 09 Dec 2016
Narrow your results
Selected tags
Tags
+? more
Top users last month
Miljana
Top achievements
Rank 2
Iron
Iron
Joel
Top achievements
Rank 3
Bronze
Bronze
Bronze
Cynthia
Top achievements
Rank 1
John
Top achievements
Rank 1
Iron
Mozart
Top achievements
Rank 1
Iron
Veteran
Want to show your ninja superpower to fellow developers?
Top users last month
Miljana
Top achievements
Rank 2
Iron
Iron
Joel
Top achievements
Rank 3
Bronze
Bronze
Bronze
Cynthia
Top achievements
Rank 1
John
Top achievements
Rank 1
Iron
Mozart
Top achievements
Rank 1
Iron
Veteran
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?