_csharp
Example
Our example page is from the-internet and can be seen here. There are three different variants of the page that are available, each with different heading text:
- Control:
A/B Test Control
- Variation 1:
A/B Test Variation 1
- Opt-out:
No A/B Test
Let's start things off by including our requisite classes for our test framework (e.g., NUnit.Framework
), driving the browser with Selenium (e.g., OpenQA.Selenium
, etc.), and start our class with some setup and teardown methods.
// filename: ABTestOptOut.cs
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
public class ABTestOptOut
{
IWebDriver Driver;
[SetUp]
public void SetUp()
{
Driver = new FirefoxDriver();
}
[TearDown]
public void TearDown()
{
Driver.Quit();
}
// ...
Now let's wire up our first test to step through visiting the page and verifying that the text changes as we forge an opt-out cookie.
// filename: ABTestOptOut.cs
// ...
[Test]
public void OptOutWithCookieAfterVisitingPage()
{
Driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/abtest");
string HeadingText = Driver.FindElement(By.TagName("h3")).Text;
Assert.That(HeadingText.StartsWith("A/B Test"));
Driver.Manage().Cookies.AddCookie(new Cookie("optimizelyOptOut", "true"));
Driver.Navigate().Refresh();
HeadingText = Driver.FindElement(By.TagName("h3")).Text;
Assert.That(HeadingText.StartsWith("No A/B Test"));
}
// ...
After navigating to the page we confirm that we are in one of the A/B test groups by grabbing the heading text and checking to see if it starts with the text "A/B Test"
. After that we add the opt-out cookie, refresh the page, and then confirm that we are no longer in the A/B test group by checking the heading text again.
We could also load the opt-out cookie before navigating to the page.
// filename: ABTestOptOut.cs
// ...
[Test]
public void OptOutWithCookieBeforeVisitingPage()
{
Driver.Navigate().GoToUrl("http://the-internet.herokuapp.com");
Driver.Manage().Cookies.AddCookie(new Cookie("optimizelyOptOut", "true"));
Driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/abtest");
string HeadingText = Driver.FindElement(By.TagName("h3")).Text;
Assert.That(HeadingText.StartsWith("No A/B Test"));
}
// ...
Here we are navigating to the main page of the site first (to establish the host) and then adding the opt-out cookie. If we didn't visit the site first, then adding the cookie wouldn't have worked. Once added we navigate to the example page and perform our checks just like before.
Alternatively, we can achieve the same thing without forging a cookie. Instead, we can append an opt-out query to the URL when visiting the page.
// filename: ABTestOptOut.cs
// ...
[Test]
public void OptOutWithURL()
{
Driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/abtest?optimizely_opt_out=true");
Driver.SwitchTo().Alert().Dismiss();
Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
string HeadingText = Driver.FindElement(By.TagName("h3")).Text;
Assert.That(HeadingText.StartsWith("No A/B Test"));
}
}
By appending ?optimizely_opt_out=true
to the URL we achieve the same outcome as with forging a cookie. But keep in mind that this approach triggers a JavaScript alert, so we have to switch to it and dismiss it (e.g., Driver.SwitchTo().Alert().Dismiss();
) before performing our assertion.
Expected Behavior
When you save this file and run it (e.g., nunit3-console.exe .\ABTestOptOut.sln
from the command-line) here is what will happen:
- Open the browser
- Opt-out of the split tests (either by cookie or appended URL)
- Visit the split testing page
- Grab the header text
- Confirm that the session is opted out of the split tests
- Close the browser
Summary
Hopefully this tip was helpful in guiding you in different methods of how to opt out of split tests.
Happy Testing!