Dynamic Data Binding - Unable to perform databinding through code.

2 posts, 0 answers
  1. Veera
    Veera avatar
    25 posts
    Member since:
    Jul 2010

    Posted 14 Dec 2010 Link to this post

    We tried to use the below code from forum to bind data sheet dynamically, but not working.
    http://blogs.msdn.com/b/mathew_aniyan/archive/2009/04/16/more-on-data-driving-coded-ui-tests.aspx

    <DataSource("System.Data.OleDb", @"Provider=Microsoft.Jet.OLEDB.8.0;Data Source=C:\QTP_Scripts\hCue_Refresh_POC\hCue_POC_01\TestProject1\TestProject1\Data\hCue_Refresh.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", "Login$", DataAccessMethod.Sequential), TestMethod()> _

  2. Cody
    Admin
    Cody avatar
    3354 posts

    Posted 16 Dec 2010 Link to this post

    Hello Veeraraghavan,

    Here's a complete example:

    using System;
     
    using ArtOfTest.WebAii.Controls.HtmlControls;
    using ArtOfTest.WebAii.Core;
    using ArtOfTest.WebAii.TestTemplates;
     
    using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    namespace WebAiiCodedTests
    {
        /// <summary>
        /// Summary description for DataDriventest
        /// </summary>
        [TestClass]
        public class DataDrivenTest : BaseTest
        {
     
            #region [Setup / TearDown]
     
            private TestContext testContextInstance = null;
            /// <summary>
            ///Gets or sets the VS test context which provides
            ///information about and functionality for the
            ///current test run.
            ///</summary>
            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }
     
     
            //Use ClassInitialize to run code before running the first test in the class
            [ClassInitialize()]
            public static void MyClassInitialize(TestContext testContext)
            {
            }
     
     
            // Use TestInitialize to run code before running each test
            [TestInitialize()]
            public void MyTestInitialize()
            {
                #region WebAii Initialization
     
                // Initializes WebAii manager to be used by the test case.
                // If a WebAii configuration section exists, settings will be
                // loaded from it. Otherwise, will create a default settings
                // object with system defaults.
                //
                // Note: We are passing in a delegate to the VisualStudio
                // testContext.WriteLine() method in addition to the Visual Studio
                // TestLogs directory as our log location. This way any logging
                // done from WebAii (i.e. Manager.Log.WriteLine()) is
                // automatically logged to the VisualStudio test log and
                // the WebAii log file is placed in the same location as VS logs.
                //
                // If you do not care about unifying the log, then you can simply
                // initialize the test by calling Initialize() with no parameters;
                // that will cause the log location to be picked up from the config
                // file if it exists or will use the default system settings (C:\WebAiiLog\)
                // You can also use Initialize(LogLocation) to set a specific log
                // location for this test.
     
                // Pass in 'true' to recycle the browser between test methods
                Initialize(false, this.TestContext.TestLogsDir, new TestContextWriteLine(this.TestContext.WriteLine));
     
                // If you need to override any other settings coming from the
                // config section you can comment the 'Initialize' line above and instead
                // use the following:
     
                /*
     
                // This will get a new Settings object. If a configuration
                // section exists, then settings from that section will be
                // loaded
     
                Settings settings = GetSettings();
     
                // Override the settings you want. For example:
                settings.DefaultBrowser = BrowserType.FireFox;
     
                // Now call Initialize again with your updated settings object
                Initialize(settings, new TestContextWriteLine(this.TestContext.WriteLine));
     
                */
     
                // Set the current test method. This is needed for WebAii to discover
                // its custom TestAttributes set on methods and classes.
                // This method should always exist in [TestInitialize()] method.
                SetTestMethod(this, (string)TestContext.Properties["TestName"]);
     
                #endregion
     
                //
                // Place any additional initialization here
                //
     
            }
     
            // Use TestCleanup to run code after each test has run
            [TestCleanup()]
            public void MyTestCleanup()
            {
     
                //
                // Place any additional cleanup here
                //
     
                #region WebAii CleanUp
     
                // Shuts down WebAii manager and closes all browsers currently running
                // after each test. This call is ignored if recycleBrowser is set
                this.CleanUp();
     
                #endregion
            }
     
            //Use ClassCleanup to run code after all tests in a class have run
            [ClassCleanup()]
            public static void MyClassCleanup()
            {
                // This will shut down all browsers if
                // recycleBrowser is turned on. Else
                // will do nothing.
                ShutDown();
            }
     
            #endregion
     
     
            [DataSource("System.Data.Odbc", "Dsn=Excel Files;dbq=.\\Book1.xlsx;defaultdir=.;driverid=1046;maxbuffersize=2048;pagetimeout=5", "Sheet1$", DataAccessMethod.Sequential)]
            [DeploymentItem("..\\Book1.xlsx")]
            [TestMethod]
            public void DataDrivenWebAiiTest()
            {
                // Launch a browser instance
                Manager.LaunchNewBrowser(BrowserType.InternetExplorer);
     
                // The active browser
                ActiveBrowser.NavigateTo("http://www.google.com");
     
                // Find the google search box and set it to "Telerik";
                Find.ByName<HtmlInputText>("q").Text = TestContext.DataRow["Name"] as String;
     
                // Click the Search button
                Find.ByName<HtmlInputSubmit>("btnG").Click();
     
                // Validate the search contain the 'Telerik' text
                Assert.IsTrue(ActiveBrowser.ContainsText("Telerik"));
            }
     
        }
    }

    I placed the excel file "Book1.xlsx" into the Solution Items folder, which is one folder up from my test project folder. That's why the deployment item starts with "..\\"

    Also, if you're using Vs 2010, make sure that you enable Deployment Items as shown in the attached screenshot.


    All the best,
    Cody
    the Telerik team
    Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
Back to Top