ALM/BPT integration code samples (.NET SDK)
In this topic:
- Data drive an ALM test
- MSTest test for ALM OpenText Functional Testing for Developers test - includes data driving
- MSTest test for BPT test - includes data driving
Data drive an ALM test
This example uses ALM input parameters to data drive a test running from ALM.
The following component code shops on the AdvantageDEMO website.
[TestCaseSource("ALM")] public void TestShopping(string username, string password, string category, string product) { // Launch the browser IBrowser browser = BrowserFactory.Launch(BrowserType.InternetExplorer); browser.Navigate(@"http://www.advantageonlineshopping.com"); // Use Try/Catch to add a warning to the run report if the validation fails. try { #region Login var userPict = browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = @" My account My Orders Sign out " }); userPict.Click(); //Enter username and password in the modal dialog var usernameDiv = browser.Describe<IWebElement>(new WebElementDescription { TagName = @"DIV", InnerText = @"Username" }); var userNameField = usernameDiv.Describe<IEditField>(new EditFieldDescription { Type = @"text" }); var passwordDiv = browser.Describe<IWebElement>(new WebElementDescription { TagName = @"DIV", InnerText = @"Password" }); var passwordField = passwordDiv.Describe<IEditField>(new EditFieldDescription { Type = @"password", }); userNameField.SetValue(username); passwordField.SetSecure(password); var signInLink = browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = @"SIGN IN" }); signInLink.Click(); // Verify the page title. Verify.IsTrue(browser.Page.Title.Equals("Advantage Shopping"), "Verify Page Title", "Verify that the page title is as expected."); #endregion #region add product to cart var categoryElement = browser.Describe<IWebElement>(new WebElementDescription { ClassName = @"shop_now roboto-bold ng-binding", TagName = @"SPAN", InnerText = category }); categoryElement.Click(); var productElement = browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = product }); productElement.Click(); var addToCartButton = browser.Describe<IButton>(new ButtonDescription { ButtonType = @"submit", TagName = @"BUTTON", Name = @"ADD TO CART" }); addToCartButton.Click(); System.Threading.Thread.Sleep(1000); //Verify the correct products count in the cart var cartProductCountElement = browser.Describe<IWebElement>(new XPathDescription(@"//HEADER[1]/NAV[1]/UL[1]/LI[1]/A[1]/SPAN[1]")); var actualCartProductsCount = cartProductCountElement.InnerText; var expectedCartProductsCount = 1; var descr = string.Format("Expecting {0} products in the cart, actual count is {1}", expectedCartProductsCount, actualCartProductsCount); Verify.AreEqual(expectedCartProductsCount, actualCartProductsCount, "Verify products count in the cart", descr); #endregion } catch (AssertionException e) { // Use a ReportEvent step to add details to the run report if the validation fails. Reporter.ReportEvent("Login", "Validation failed.", Status.Failed, e); throw; } }
MSTest test for ALM OpenText Functional Testing for Developers test - includes data driving
The following is an example of a MSTest test that can be run in ALM.
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using HP.LFT.SDK; using HP.LFT.SDK.Web; using HP.LFT.UnitTesting; using HP.LFT.Verifications; namespace MSTestAlmExamples { [TestClass] public class AdvantageShopping : UnitTestClassBase<AdvantageShopping> { private const string SiteUrl = @"http://www.advantageonlineshopping.com"; readonly static BrowserType BrowserType = BrowserType.InternetExplorer; private static IBrowser _browser; [ClassInitialize] public static void ClassInitialize(TestContext context) { GlobalSetup(context); //Launch the browser. _browser = BrowserFactory.Launch(BrowserType); } [TestInitialize] public void TestInitialize() { } [TestMethod] [DataSource("ALM")] [Parameters("Username", "Password", "Category", "Product")] public void TestShopping() { var username = Convert.ToString(TestContext.DataRow["Username"]); var password = Convert.ToString(TestContext.DataRow["Password"]); var category = Convert.ToString(TestContext.DataRow["Category"]); var product = Convert.ToString(TestContext.DataRow["Product"]); OpenAdvantageShoppingSite(); Login(username, password); VerifyLoggedUser(username); SelectProduct(category, product); VerifyCartProductCount("1"); Checkout(); } private void OpenAdvantageShoppingSite() { //Navigate to the Advantage Shopping website. _browser.Navigate(SiteUrl); } private void Login(string username, string password) { var userPict = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = @" My account My Orders Sign out " }); userPict.Click(); //Enter username and password in the modal dialog var usernameDiv = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"DIV", InnerText = @"Username" }); var userNameField = usernameDiv.Describe<IEditField>(new EditFieldDescription { Type = @"text" }); var passwordDiv = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"DIV", InnerText = @"Password" }); var passwordField = passwordDiv.Describe<IEditField>(new EditFieldDescription { Type = @"password", }); userNameField.SetValue(username); passwordField.SetSecure(password); var signInLink = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = @"SIGN IN" }); signInLink.Click(); } private void VerifyLoggedUser(string username) { // Verify correct username appears in the header var loggedUserField = _browser.Describe<IWebElement>(new WebElementDescription { ClassName = @"hi-user containMiniTitle ng-binding", TagName = @"SPAN", }); var loggedUsername = loggedUserField.InnerText.Trim(); var descr = string.Format("Expected username '{0}', actual '{1}'", username, loggedUsername); if (!Verify.AreEqual(username.Trim(), loggedUsername, "Verify logged user name", descr)) { throw new AssertFailedException(descr); } } private void SelectProduct(string category, string product) { var categoryElement = _browser.Describe<IWebElement>(new WebElementDescription { ClassName = @"shop_now roboto-bold ng-binding", TagName = @"SPAN", InnerText = category }); categoryElement.Click(); var productElement = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = product }); productElement.Click(); var addToCartButton = _browser.Describe<IButton>(new ButtonDescription { ButtonType = @"submit", TagName = @"BUTTON", Name = @"ADD TO CART" }); addToCartButton.Click(); } private void VerifyCartProductCount(string expectedCartProductsCount) { System.Threading.Thread.Sleep(1000); //Verify the correct products count in the cart var cartProductCountElement = _browser.Describe<IWebElement>(new XPathDescription(@"//HEADER[1]/NAV[1]/UL[1]/LI[1]/A[1]/SPAN[1]")); var actualCartProductsCount = cartProductCountElement.InnerText; var descr = string.Format("Expecting {0} products in the cart, actual count is {1}", expectedCartProductsCount, actualCartProductsCount); Verify.AreEqual(expectedCartProductsCount, actualCartProductsCount, "Verify products count in the cart", descr); } private void Checkout() { var cartLink = _browser.Describe<ILink>(new LinkDescription { ClassName = @"img", TagName = @"A" }); cartLink.Click(); var checkoutButton = _browser.Describe<IButton>(new ButtonDescription { ButtonType = @"submit", TagName = @"BUTTON", Name = As.RegExp(@"^CHECKOUT.*"), Index = 1 }); checkoutButton.Click(); var totalValueSpan = _browser.Describe<IWebElement>(new WebElementDescription { ClassName = @"roboto-medium totalValue ng-binding", TagName = @"SPAN" }); totalValueSpan.Highlight(); var totalValue = string.Format("Total value for checkout is {0}", totalValueSpan.InnerText); Reporter.ReportEvent(totalValue, totalValue); } [TestCleanup] public void TestCleanup() { } [ClassCleanup] public static void ClassCleanup() { GlobalTearDown(); } } }
MSTest test for BPT test - includes data driving
The following is an example of a MSTest test with business components that can be run in ALM.
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using HP.LFT.SDK; using HP.LFT.SDK.Web; using HP.LFT.UnitTesting; using HP.LFT.Verifications; namespace MSTestAlmExamples { [TestClass]public class AdvantageShoppingBp : UnitTestClassBase<AdvantageShoppingBp> { private const string SiteUrl = @"http://www.advantageonlineshopping.com"; private static IBrowser _browser; [ClassInitialize]public static void ClassInitialize(TestContext context) { GlobalSetup(context); } [TestInitialize]public void TestInitialize() { } [TestCleanup]public void TestCleanup() { } [ClassCleanup]public static void ClassCleanup() { GlobalTearDown(); } //Output variables (should be static for MSTest tests)public static string CartProductCount; public static string CartTotalValue; // Component code to login to Advantage Shopping website and select product. [TestMethod] [DataSource("ALM")] [Parameters("BrowserType", "Username", "Password", "Category", "Product")] [OutputParameters("CartProductCount")] public void LoginAndSelectProduct() { var browserType = Convert.ToString(TestContext.DataRow["BrowserType"]); var username = Convert.ToString(TestContext.DataRow["Username"]); var password = Convert.ToString(TestContext.DataRow["Password"]); var category = Convert.ToString(TestContext.DataRow["Category"]); var product = Convert.ToString(TestContext.DataRow["Product"]); var text = string.Format("browserType='{0}', username='{1}', password='{2}', category='{3}', product='{4}'", browserType, username, password, category, product); Reporter.ReportEvent("Input Values: " + text, text); //Launch the browser. _browser = BrowserFactory.Launch(GetBrowserType(browserType)); //Navigate to the Advantage Shopping website. _browser.Navigate(SiteUrl); Login(username, password); VerifyLoggedUser(username); SelectProduct(category, product); CartProductCount = GetCartProductCount(); var totalValueText = string.Format("Products in cart: {0}", CartProductCount); Reporter.ReportEvent(totalValueText, totalValueText); } // Component code to login to Advantage Shopping website and select product. [TestMethod] [DataSource("ALM")] [Parameters("BrowserType", "Username", "Password", "ProductCount")] [OutputParameters("CartTotalValue")]public void LoginAndCheckout() { var browserType = Convert.ToString(TestContext.DataRow["BrowserType"]); var username = Convert.ToString(TestContext.DataRow["Username"]); var password = Convert.ToString(TestContext.DataRow["Password"]); var productCount = Convert.ToString(TestContext.DataRow["ProductCount"]); var text = string.Format("browserType='{0}', username='{1}', password='{2}', productCount='{3}'", browserType, username, password, productCount); Reporter.ReportEvent("Input Values: " + text, text); //Launch the browser. _browser = BrowserFactory.Launch(GetBrowserType(browserType)); //Navigate to the Advantage Shopping website. _browser.Navigate(SiteUrl); Login(username, password); VerifyLoggedUser(username); VerifyCartProductCount(productCount); CartTotalValue = Checkout(); var totalValueText = string.Format("Total value for checkout is {0}", CartTotalValue); Reporter.ReportEvent(totalValueText, totalValueText); } private void Login(string username, string password) { var userPict = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = As.RegExp(@".*My account.*") }); userPict.Click(); //Enter username and password in the modal dialog var usernameDiv = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"DIV", InnerText = @"Username" }); var userNameField = usernameDiv.Describe<IEditField>(new EditFieldDescription { Type = @"text" }); var passwordDiv = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"DIV", InnerText = @"Password" }); var passwordField = passwordDiv.Describe<IEditField>(new EditFieldDescription { Type = @"password", }); userNameField.SetValue(username); passwordField.SetSecure(password); var signInLink = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = @"SIGN IN" }); signInLink.Click(); } private void VerifyLoggedUser(string username) { // Verify correct username appears in the header var loggedUserField = _browser.Describe<IWebElement>(new WebElementDescription { ClassName = @"hi-user containMiniTitle ng-binding", TagName = @"SPAN", }); var loggedUsername = loggedUserField.InnerText.Trim(); var descr = string.Format("Expected username '{0}', actual '{1}'", username, loggedUsername); Verify.AreEqual(username.Trim(), loggedUsername, "Verify logged user name", descr); } private void SelectProduct(string category, string product) { var categoryElement = _browser.Describe<IWebElement>(new WebElementDescription { ClassName = @"shop_now roboto-bold ng-binding", TagName = @"SPAN", InnerText = category }); categoryElement.Click(); var productElement = _browser.Describe<IWebElement>(new WebElementDescription { TagName = @"A", InnerText = product }); productElement.Click(); var addToCartButton = _browser.Describe<IButton>(new ButtonDescription { ButtonType = @"submit", TagName = @"BUTTON", Name = @"ADD TO CART" }); addToCartButton.Click(); System.Threading.Thread.Sleep(2000); } private string GetCartProductCount() { //Verify the correct products count in the cart var cartProductCountElement = _browser.Describe<IWebElement>(new XPathDescription(@"//HEADER[1]/NAV[1]/UL[1]/LI[1]/A[1]/SPAN[1]")); var actualCartProductsCount = cartProductCountElement.InnerText; return actualCartProductsCount; } private void VerifyCartProductCount(string expectedCartProductsCount) { System.Threading.Thread.Sleep(1000); //Verify the correct products count in the cart var cartProductCountElement = _browser.Describe<IWebElement>(new XPathDescription(@"//HEADER[1]/NAV[1]/UL[1]/LI[1]/A[1]/SPAN[1]")); var actualCartProductsCount = cartProductCountElement.InnerText; var descr = string.Format("Expecting {0} products in the cart, actual count is {1}", expectedCartProductsCount, actualCartProductsCount); Verify.AreEqual(expectedCartProductsCount, actualCartProductsCount, "Verify products count in the cart", descr); } private string Checkout() { var cartLink = _browser.Describe<ILink>(new LinkDescription { ClassName = @"img", TagName = @"A" }); cartLink.Click(); var checkoutButton = _browser.Describe<IButton>(new ButtonDescription { ButtonType = @"submit", TagName = @"BUTTON", Name = As.RegExp(@"^CHECKOUT.*"), Index = 1 }); checkoutButton.Click(); var totalValueSpan = _browser.Describe<IWebElement>(new WebElementDescription { ClassName = @"roboto-medium totalValue ng-binding", TagName = @"SPAN" }); totalValueSpan.Highlight(); return totalValueSpan.InnerText; } private BrowserType GetBrowserType(string browserTypeParam) { if (browserTypeParam.Equals("Chrome", StringComparison.OrdinalIgnoreCase)) return BrowserType.Chrome; if (browserTypeParam.Equals("InternetExplorer", StringComparison.OrdinalIgnoreCase)) return BrowserType.InternetExplorer; if (browserTypeParam.Equals("IE", StringComparison.OrdinalIgnoreCase)) return BrowserType.InternetExplorer; if (browserTypeParam.Equals("Edge", StringComparison.OrdinalIgnoreCase)) return BrowserType.Edge; if (browserTypeParam.Equals("Firefox", StringComparison.OrdinalIgnoreCase)) return BrowserType.Firefox; if (browserTypeParam.Equals("Safari", StringComparison.OrdinalIgnoreCase)) return BrowserType.Safari; return BrowserType.Chrome; } } }