Automation Testing with Selenium C# and MSTest - SQA Interview
Preparation
1. What is Automation Testing?
Automation Testing is the process of using specialized tools or scripts to execute test cases
automatically, without manual intervention. It increases efficiency, reduces human errors,
and ensures faster feedback during regression testing.
2. What is Selenium?
Selenium is an open-source framework used for automating web applications. It supports
multiple languages like C#, Java, Python, and JavaScript, and works across all major
browsers.
Selenium Components:
• Selenium WebDriver – Used for browser automation.
• Selenium IDE – A record and playback tool.
• Selenium Grid – Used for parallel execution across different machines or browsers.
3. Why C# with Selenium
C# is a popular choice for Selenium because it integrates well with Microsoft technologies
like Visual Studio and MSTest. It provides robust OOP support, better error handling, and
structured testing frameworks.
4. What is MSTest?
MSTest is a testing framework provided by Microsoft, mainly used for unit and UI testing. It
helps organize and run test methods with annotations like [TestClass], [TestMethod],
[TestInitialize], and [TestCleanup].
5. Selenium C# Test Structure (MSTest Example)
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace SeleniumAutomationDemo
{
[TestClass]
public class LoginTests
{
IWebDriver driver;
[TestInitialize]
public void Setup()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://example.com/login");
}
[TestMethod]
public void VerifyLoginFunctionality()
{
// Locate elements and perform actions
driver.FindElement(By.Id("username")).SendKeys("testuser");
driver.FindElement(By.Id("password")).SendKeys("password123");
driver.FindElement(By.Id("loginButton")).Click();
// Assertion
string expectedTitle = "Dashboard";
Assert.AreEqual(expectedTitle, driver.Title, "Login failed or redirected to wrong
page.");
}
[TestCleanup]
public void TearDown()
{
driver.Quit();
}
}
}
6. Explanation of Test Structure
• [TestClass] – Marks a class that contains test methods.
• [TestInitialize] – Runs before every test. Used to set up browser and navigate to URL.
• [TestMethod] – Contains the actual test steps and assertions.
• [TestCleanup] – Runs after each test to close the browser or clean resources.
7. Locators in Selenium
Locators are used to find web elements on a page. Selenium provides several locating
strategies:
• By.Id
• By.Name
• By.ClassName
• By.TagName
• By.LinkText
• By.PartialLinkText
• By.CssSelector
• By.XPath
8. Example of Locating Elements
// Using different locators
driver.FindElement(By.Id("username")).SendKeys("Ammar");
driver.FindElement(By.Name("password")).SendKeys("12345");
driver.FindElement(By.CssSelector("button.login")).Click();
driver.FindElement(By.XPath("//a[text()='Forgot Password?']")).Click();
9. Waits in Selenium
Waits are used to synchronize your test execution with web page behavior.
• Implicit Wait – Applies to all elements.
• Explicit Wait – Waits for a specific condition.
• Fluent Wait – Waits with polling frequency.
// Example of Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element =
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("dashb
oard")));
10. Assertions in MSTest
Assertions are used to verify test results. Common assertion methods:
• Assert.AreEqual(expected, actual)
• Assert.IsTrue(condition)
• Assert.IsFalse(condition)
• Assert.IsNotNull(object)
11. Example Challenge Faced During Selenium Automation
During a login automation test, I faced an issue where Selenium could not click a button due
to a dynamic overlay loading after page navigation. The test was failing intermittently.
To fix it, I implemented an Explicit Wait to ensure that the button becomes clickable before
performing the click. This stabilized the test execution and eliminated random failures.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement loginButton =
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("l
oginButton")));
loginButton.Click();
12. Common Selenium C# Interview Questions and Answers
Q1: What is Selenium WebDriver?
It’s a tool used to automate web browsers by simulating user actions like click, input, and
navigation.
Q2: What are the different types of waits in Selenium?
Implicit Wait, Explicit Wait, and Fluent Wait.
Q3: What is the difference between FindElement and FindElements?
FindElement returns a single element or throws an exception if not found, while
FindElements returns a list and never throws an exception.
Q4: What is the use of Assertions in MSTest?
Assertions are used to verify whether the actual result matches the expected result.
Q5: How do you handle dynamic elements?
By using XPath with contains(), starts-with(), or by applying Explicit Waits until the element
is visible or clickable.
Q6: How do you manage test data in Selenium?
Using configuration files, Excel, JSON, or environment variables for dynamic input.
Q7: How do you handle pop-ups or alerts?
By switching to alert using driver.SwitchTo().Alert().
Q8: What are the advantages of using MSTest?
Simple integration with Visual Studio, built-in reporting, and easy test management.
Q9: How do you capture screenshots in Selenium?
Using ITakesScreenshot interface and SaveAsFile method.
Q10: What is the Page Object Model (POM)?
A design pattern that separates page elements and actions into different classes to make
tests reusable and maintainable.
13. Final Tips for SQA Interview (Selenium C#)
• Be confident in explaining how Selenium interacts with browsers.
• Know how to locate elements using different strategies.
• Understand synchronization (waits) clearly.
• Mention handling of dynamic elements, assertions, and error handling.
• Give one real-life example where you faced and solved an automation issue.