Pretty much all the Sitecore development is happening in Agile. Hence, code is released more frequently to live servers than it used to be in waterfall models. This also means there is a need of testing after each release. A regression in QA environment and a smoke test in STAGE environment is required to ensure that the development done in sprint does not break any thing else.
This is the job of QA team. Why are we talking about it here?
If your team is testing Sitecore functionalities manually, then it is wise to automate at least some of your test cases. Testing some basic Sitecore functionalities like login, publishing etc. manually is something QA team can easily do but when one needs to automate it, QA developers needs to have some knowledge of Sitecore also and vice-versa. Hence, here is a simple blog on guiding how to develop some basic automated test cases in Sitecore.
Lets get started..
In this blog, I am taking example of a basic scenario which includes testing that your users should have the ability to log in to your Sitecore instance with correct credentials and shall be redirected back to login screen if incorrect credentials are provided.
Since we are going to log in to Sitecore using browsers, we will have to make use of any tool that supports browser automation and Selenium is one of them. So Selenium is our pick for this example.
We are using Asp.Net MVC in Sitecore. Hence, it makes sense to use NUnit as unit-test framework. You may use a different unit-test framework based on your project needs.
Step 1 -
This involves creating an NUnit project and then importing Selenium NuGets into it. Then, we also need to download Chrome webdrivers for Selenium to work with Chrome browsers. This is standard stuff and if you dont know about it, there are lots of online blogs available guiding you to do that. Here is one which you can make use of - https://testguild.com/selenium-webdriver-visual-studio/
Step 2 -
This is where our NUnit project is setup with Selenium and Chrome web drivers based on step 1. Now, we need to write code to make our test scenarios work. Create a class called Tests.cs in your NUnit project and add below code to it -
//Inside SeleniumTest.cs
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.ObjectModel;
using System.IO;
namespace SeleniumCsharp
{
public class Tests
{
IWebDriver driver;
[OneTimeSetUp]
public void Setup()
{
//Below code is to get the drivers folder path dynamically.
//You can also specify chromedriver.exe path dircly ex: C:/MyProject/Project/drivers
string path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
//Creates the ChomeDriver object, Executes tests on Google Chrome
ChromeOptions options = new ChromeOptions();
options.AddArguments("--start=maximized");
options.AddArguments("--lang=en");
driver = new ChromeDriver(path + @"\drivers\", options);
//If you want to Execute Tests on Firefox uncomment the below code
// Specify Correct location of geckodriver.exe folder path. Ex: C:/Project/drivers
//driver= new FirefoxDriver(path + @"\drivers\");
}
[Test]
public void verifyLogin()
{
driver.Navigate().GoToUrl("https://<sitecore instance hostname>/sitecore");
var uName = driver.FindElement(By.Id("Username"));
uName.SendKeys("admin");
var password = driver.FindElement(By.Id("Password"));
password.SendKeys("b");
var loginButton = driver.FindElement(By.Name("button")); loginButton.Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Assert.IsTrue(driver.FindElement(By.ClassName("sc-accountInformation"))!=null);//this ensure that after login, url is redirected to a page which has account information presents which means login was succussfull
}
[Test]
public void returnToLoginOnInvalidCreds()
{
driver.Navigate().GoToUrl("https://<sitecore instance hostname>/sitecore");
var uName = driver.FindElement(By.Id("Username"));
uName.SendKeys("admin");
var password = driver.FindElement(By.Id("Password"));
password.SendKeys("abcd");//incorrect credentials given intentionally for login to fail
var loginButton = driver.FindElement(By.Name("button"));
loginButton.Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Assert.IsTrue(driver.FindElement(By.Id("Username")) != null);//On login failure, user is redirected to login page in Sitecore which again asks user to input Username. Hence, checking for that element.
}
[OneTimeTearDown]
public void TearDown()
{
driver.Quit();
}
}
}
In the above class, there are two methods with evident names to test if a valid login succeeds in Sitecore or not and if an invalid login fails correctly in Sitecore or not.
After adding this code, build your project and open it using Test Explorer and Run the Tests.
You will see the magic happening. Selenium will open the browsers, perform the steps mentioned in the steps and then report if the login failed or succeeded in Sitecore.
This was a small introduction to a huge world of possibilities. Hope you liked it!
Comments
Post a Comment