How to Test PageMode.IsExperienceEditorEditing in Sitecore


If you are passionate about testing automation in Sitecore, sooner or later you will want to mock Context.PageMode.IsExperienceEditorEditing (or Context.PageMode.IsPageEditorEditing in pre-Sitecore 8) properties. This post will give you step-by-step instructions on how to do that using Sitecore.FakeDb.

First of all, you need to define a “shell” site in the app.config file.

<site name="shell" domain="sitecore"></site>

SiteContextFactory.Sites.Add() method has no use here. Sitecore uses the lower level API SiteManager.GetSite(“shell”), so the “shell” site has to be set up in app.config or you need to use a custom provider in SiteManager.

After finishing with the “shell” site, you need to take care of the current context site. It can be set up using standard Sitecore.FakeDb SiteContextSwitcher helper. There are only two essential attributes to set up – enableWebEdit and masterDatabase.

var fakeSiteContext = new FakeSiteContext(
new StringDictionary
{"enableWebEdit", "true"},
{"masterDatabase", "master"}
var siteContextSwitcher = new SiteContextSwitcher(fakeSiteContext);

Now you are ready for the last step – switch site mode. You need to call Context.Site.SetDisplayMode method for that:

Context.Site.SetDisplayMode(DisplayMode.Edit, DisplayModeDuration.Remember);

Now you have everything you need and can assert that. Below is a full test listing using NUnit and FluentAssertions:

public void TestIsPageEditorEditing()
var fakeSiteContext = new Sitecore.FakeDb.Sites.FakeSiteContext(
new Sitecore.Collections.StringDictionary
{"enableWebEdit", "true"},
{"masterDatabase", "master"},

using (new Sitecore.Sites.SiteContextSwitcher(fakeSiteContext))
Sitecore.Context.Site.SetDisplayMode(DisplayMode.Edit, DisplayModeDuration.Remember);


//Available only in Sitecore 8+

You also can find full code for the solution in GitHub. Happy testing!

About the Author

As a Sitecore MVP, Brian spends most of his time consulting and architecting software solutions for enterprise-level Sitecore projects.

More from this Author

Thoughts on “How to Test PageMode.IsExperienceEditorEditing in Sitecore”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up