Extensibility- Extend Common Services
Learn how to extend BELLATRIX common services.
Example
[TestFixture]
public class ExtendExistingCommonServicesTests : WebTest
{
[Test]
public void PurchaseRocket()
{
App.Navigation.NavigateViaJavaScript("http://demos.bellatrix.solutions/");
Select sortDropDown = App.Components.CreateByNameEndingWith<Select>("orderby");
Anchor protonMReadMoreButton =
App.Components.CreateByInnerTextContaining<Anchor>("Read more");
Anchor addToCartFalcon9 =
App.Components.CreateByAttributesContaining<Anchor>("data-product_id", "28").ToBeClickable();
Anchor viewCartButton =
App.Components.CreateByClassContaining<Anchor>("added_to_cart wc-forward").ToBeClickable();
TextField couponCodeTextField = App.Components.CreateById<TextField>("coupon_code");
Button applyCouponButton = App.Components.CreateByValueContaining<Button>("Apply coupon");
Number quantityBox = App.Components.CreateByClassContaining<Number>("input-text qty text");
Div messageAlert = App.Components.CreateByClassContaining<Div>("woocommerce-message");
Button updateCart = App.Components.CreateByValueContaining<Button>("Update cart").ToBeClickable();
Button proceedToCheckout =
App.Components.CreateByClassContaining<Button>("checkout-button button alt wc-forward");
Heading billingDetailsHeading =
App.Components.CreateByInnerTextContaining<Heading>("Billing details");
Span totalSpan = App.Components.CreateByXpath<Span>("//*[@class='order-total']//span");
sortDropDown.SelectByText("Sort by price: low to high");
protonMReadMoreButton.Hover();
addToCartFalcon9.Focus();
addToCartFalcon9.Click();
viewCartButton.Click();
couponCodeTextField.SetText("happybirthday");
applyCouponButton.Click();
messageAlert.ToHasContent().ToBeVisible().WaitToBe();
messageAlert.ValidateInnerTextIs("Coupon code applied successfully.");
quantityBox.SetNumber(0);
quantityBox.SetNumber(2);
updateCart.Click();
totalSpan.ValidateInnerTextIs("95.00€", 15000);
proceedToCheckout.SubmitButtonWithEnter();
billingDetailsHeading.ToBeVisible().WaitToBe();
}
}
Explanations
public static class NavigationServiceExtensions
{
public static void NavigateViaJavaScript(this NavigationService navigationService, string url)
{
var javaScriptService = new JavaScriptService();
if (!navigationService.IsUrlValid(url))
{
throw new ArgumentException($"The specified URL- {url} is not in a valid format!");
}
javaScriptService.Execute($"window.location.href = '{url}';");
}
public static bool IsUrlValid(this NavigationService navigationService, string url)
{
bool result = Uri.TryCreate(url, UriKind.Absolute, out var uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
return result;
}
}
One way to extend the BELLATRIX common services is to create an extension method for the additional action.
- Place it in a static class like this one.
- Create a static method for the action.
- Pass the common service as a parameter with the keyword ‘this’.
- Access the native driver via WrappedDriver.
Later to use the method in your tests, add a using statement containing this class’ namespace.
using Bellatrix.Web.GettingStarted.Advanced.Elements.Extension.Methods;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bellatrix.Web.GettingStarted
To use the additional method you created, add a using statement to the extension methods’ namespace.
App.Navigation.NavigateViaJavaScript("http://demos.bellatrix.solutions/");
Use newly added navigation though JavaScript which is not part of the original implementation of the common service.