Run the sample web app visual test
Step 1: Setup environment
Requirements
- .NET 9.0
- NUnit framework
- Appium client
- Access key
- You can generate an Access key using the following instruction – Access key
- Billing unit
- The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
More information can be found here.
- The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
Step 2: Run sample test
After setup up the environment, you can run your first web visual test
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System.Collections.Generic;
namespace SeleniumTests
{
public class WebSeleniumVisualCapsTest
{
// Constants
const string ProjectName = "<TEAM_CODE>";
const string AccessKey = "mobitru_ak_......";
const string SeleniumHost = "browserhub-us.mobitru.com";
const int DefaultImplicitTimeoutSec = 3;
const int DefaultWaitTimeoutSec = 10;
const string VisualSuiteName = "visual c# demo tests";
const string VisualTestName = "visual c# demo for selenium";
const string OpenUrl = "https://mobitru.com/";
private RemoteWebDriver driver;
[SetUp]
public void Setup()
{
var browserOptions = new ChromeOptions();
var mobitruOptions = new Dictionary<string, object>();
mobitruOptions.Add("enableVideo", false);
mobitruOptions.Add("sessionTimeout", "1m");
var visualBuildInfo = new Dictionary<string, object>
{
{ "name", "Build 1" },
{ "project", "C# Automation Demo for JavaScriptExecutor" },
{ "branch", "main" },
{ "failOnError", false }
};
mobitruOptions.Add("visualTesting", visualBuildInfo);
browserOptions.AddAdditionalOption("mobitru:options", mobitruOptions);
var seleniumHubUrl = $"""https://{ProjectName}:{AccessKey}@{SeleniumHost}/wd/hub""";
driver = new RemoteWebDriver(new Uri($"{seleniumHubUrl}"), browserOptions);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(DefaultImplicitTimeoutSec);
}
[Test]
public void TestSeleniumDemo()
{
// Navigate to the URL
driver.Navigate().GoToUrl(OpenUrl);
// Wait for the Mobitru main page to load
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(DefaultWaitTimeoutSec));
By demoButtonLocator = By.CssSelector("button[class*='demo']");
wait.Until(d => d.FindElement(demoButtonLocator).Displayed);
// Verify the current URL
Assert.That(driver.Url, Is.EqualTo(OpenUrl), "Current URL is incorrect.");
// Verify the page title
string expectedTitle = "Mobitru: Intelligent Real Device Cloud for Mobile and Cross-Browser Testing";
Assert.That(driver.Title, Is.EqualTo(expectedTitle), "Page title is incorrect.");
TakeAndSubmitScreenshot(driver, "after_login", VisualTestName, VisualSuiteName);
}
[TearDown]
public void TearDown()
{
driver?.Quit();
}
private void TakeAndSubmitScreenshot(RemoteWebDriver driver, String name, String testName, String suiteName)
{
var snapshotData = new Dictionary<string, object>
{
{ "name", name},
{ "testName", testName},
{ "suiteName",suiteName}
};
driver.ExecuteScript("mobitru:visualCheck", snapshotData);
}
}
}