How to assert in selenium webdriver in C#? - c#

I am working with Selenium WebDriver in C# and I have to create a service for an applicant. I have done this already but after I confirm the service goes to a List ( Services that need to be confirmed from another user ) which is increased by 1 in read mode. Is there any way how to assert these values that are increased by 1 every time a new service is added?

You need to use a test framework to do this -- selenium itself cannot assert for you.
If you are using C#, I recommend installing NUnit. You can find this under NuGet Package manager, and you'll also want to install NUnitTestAdapter if you are using Visual Studio.
Once you have installed a test framework on your project, you can use [Test] flags to designate entry point methods for test cases, and use Assert statements which are part of the NUnit namespace.
You can find documentation here: https://github.com/nunit/docs/wiki/NUnit-Documentation

Selenium's built-in assert functionality only exists in SeleniumIDE, the point-and-click browser add-on available for Chrome and Firefox.
If you are going to write your tests in C#, as Christine said, you need to use a unit testing framework. For example, I'm using Xunit, and a simple test looks like this:
using Xunit; // Testing framework. NuGet package
using OpenQA.Selenium.Firefox; // Driver for Firefox
using Xunit.Priority; // NuGet add-on to Xunit that allows you to order the tests
using OpenQA.Selenium; // NuGet package
using System.Diagnostics; // Can Debug.Print when running tests in debug mode
namespace Test_MyWebPage
{
[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)] // Set up ordering
public class Test_BasicLogin : IDisposable
{
public static IWebDriver driver = new FirefoxDriver(#"path\to\geckodriver");
// Here be the tests...
[Fact, Priority(0)]
public void Test_LaunchWebsite()
{
// Arrange
var url = "https://yourserver.yourdomain/yourvirtualdir";
// Act
// Sets browser to maximized, allows 1 minute for the page to
// intially load, and an implicit time out of 1 minute for elements
// on the page to render.
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().PageLoad = new TimeSpan(0, 1, 0);
driver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 1, 0);
driver.url = url; // Launches the browser and opens the page
/* Assuming your page has a login prompt
/* we'll try to locate this element
/* and perform an assertion to test that the page comes up
/* and displays a login prompt */
var UserNamePrompt = driver.FindElement(By.Id("userLogin_txtUserName"));
// Assert
Assert.NotNull(UserNamePrompt); // Bombs if the prompt wasn't found.
Debug.Print("Found User Name Prompt successfully.");
}
public void Dispose()
{
// Properly close the browser when the tests are done
try
{
driver.Quit();
}
catch (Exception ex)
{
Debug.WriteLine($"Error disposing driver: {ex.Message}");
}
}
}
}
As you can see, there is considerably more work in setting up tests for the Selenium WebDriver than in setting up simple smoke tests with the SeleniumIDE. I haven't addressed how you should store your configs properly (hardcoding as in the example is bad) and you will have to tailor your driver.find() statements to fit your precise situation. I am using the Xunit.Priority package so I can make sure the tests don't all run in parallel; I need to test one thing at a time in a progression. Your needs might be met by putting all of the steps into a single Test_* method. Each method appears as a separate test in the Visual Studio Test Explorer window. Right-clicking a test in the Test Explorer and selecting 'Debug selected tests' will allow you to set breakpoints and also enable your Debug.Print (or Debug.Write/Writeline) methods to display in the Tests section of the VS output window.
Another gotcha is in setting up the IWebDriver: don't put the complete path including the executable in the path, just the path to the folder that contains it.
Good luck and happy testing!

Related

Can't run a simple Unit Test #Cs #VS19 #MSTest

I'm new in unit test and I'm trying to run a simple test but after push the "run test" button it's load then nothing
What's wrong ????!!
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestNinja.Fundamentals;
namespace TestNinga.UnitTests
{
[TestClass]
public class ReservationTests
{
[TestMethod]
public void CanBeCancelledBy_UserIsAdmin_ReturnTrue()
{
var reservation = new Reservation();
var result = reservation.CanBeCancelledBy(new User { IsAdmin = true });
Assert.IsTrue(result);
}
}
}
It looks like nothing is wrong. Sometimes this happens also to me and sometimes I fix it by closing the VS, deleting the bin & obj folders, opening the VS, and cleaning the solution. One time I also noted out that the process runs in the background and stuck and as a result, the tests didn't run, so I killed the process, and then it started working.
If it won't help you always can see what is the error you get and share more information.
To see the error you should open the Output pane and in the Show output from: box, you should choose the tests option.
I find the isssue , I just update/add this package in my test solution :
Microsoft.NET.Test.Sdk
MSTest.TestAdapter
MSTest.TestFramework

OneTimeTearDown is not working using selenium WebDriver

This code should takes a screenshot when test fail:
[TestClass]
public class UnitTest1
{
[OneTimeTearDown]
public void TestFail()
{
IWebDriver driver = new ChromeDriver();
if (NUnit.Framework.TestContext.CurrentContext.Result.Outcome != ResultState.Success)
{
string screensLocation = #"D:\";
string testName = NUnit.Framework.TestContext.CurrentContext.Test.Name;
var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile(screensLocation + testName + ".png");
}
}
[TestMethod]
public void TestMethod1()
{
// my code, here test is failed
}
}
But it is not working. I don't have any screen in location D:\
Otherwise is there a way to debug code under OneTimeTearDown Attribute? Because when the test is fail, debugging ends and I don't know what's going on in the method TestFail().
Thanks for your help.
OneTimeTearDownAttribute is a feature of NUnit.
Although your tag says "nunit", your code is not actually using it. TestClassAttribute and TestMethodAttribute are features of MS Test. If you tried to run this test with NUnit, it would not recognize the tests at all.
Obviously, your test assembly does reference the NUnit framework, since it would not otherwise compile.
So... bottom line, your test code references two different frameworks in such a way that it cannot be run successfully by either runner!!! You have to choose which of the two you want to use, remove the other reference and use a runner for the framework you choose to keep.

Slow execution of tests using Teststack.White + Nunit3-console

Hello i have small problem with nunit and teststack white.
Here i have small test (Window is localized with option WithCache):
[Test] public void ShouldLogIn()
{
var loginPanel = Window.Get<Panel>("txbLogin");
var loginTextBox = loginPanel.Get<TextBox>("mobTextBox1");
loginTextBox.BulkText = "user1";
}
Now i run this test from VS, and debugging each step. I can notice, that Get takes around 50ms, Get 30ms. Next i run the same test using nunit3-console.exe and now, after attaching to process i can notice around 3 time longer execution of each step. Its not problem with attaching, becouse i checked it in longer test and always steps takes longer.
Another long process is getting new window. My application show new windows, which covers actual one, so i need to get new window when new one appears. Command Application.GetWindows().Last() takes around 200-300ms from VS, but from nunit console in worst case can take about 3 seconds.
Maybe someone of you have similar problem and have any solution for me.
HERE I HAVE EXAMPLE:
You need to download app from here:
https://www.codeproject.com/Articles/607868/Social-Club-Sample-application-using-WinForms-Csha
Build it, run application, login into it with credentials:
login: demo
password: demo123
Then create new unit test prkject, add nuget pacakges for nunit and teststack.white. Here is code for this test:
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Castle.Core.Logging;
using NUnit.Framework;
using TestStack.White;
using TestStack.White.Configuration;
using TestStack.White.Factory;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Finders;
using TestStack.White.UIItems.WindowItems;
namespace StackTestProject
{
[TestFixture]
public class Notepadtests
{
private Application Application;
private Window Window;
[OneTimeSetUp]
public void OneTimeInitialize()
{
CoreAppXmlConfiguration.Instance.FindWindowTimeout = 30000;
CoreAppXmlConfiguration.Instance.LoggerFactory = new WhiteDefaultLoggerFactory(LoggerLevel.Debug);
}
[SetUp]
public void TestInitialize()
{
Thread.Sleep(5000);
ProcessStartInfo psi = new ProcessStartInfo("John.SocialClub.Desktop.exe");
psi.WorkingDirectory = #"C:\Users\[USER NAME]\Downloads\John.SocialClub\John.SocialClub\John.SocialClub.Desktop\bin\Debug";
Application = TestStack.White.Application.AttachOrLaunch(psi);
//before this u need to have opened and logged in aplication with login:demo and password: demo123
Window = Application.GetWindow("Social Club - Membership Manager", InitializeOption.WithCache); //250ms
}
[Test]
public void NotepadTest()
{
var toolbar = Window.Get<Panel>(SearchCriteria.ByClassName("tabRegistration")); //33ms
}
[TearDown]
public void TestCleanup()
{
Application.Kill();
}
}
}
I hit breakpoint on line where i assign value to Window, and now i check time of getting window with name "Social Club - Membership Manager":
a) when i debug test from visual studio - its around 450ms for first assign, when i move cursor back to this line and assign again its faster and takes about 250ms
b) when i run from nunit3-console with command :
nunit3-console.exe C:\Users\kamil.chodola\source\repos\StackTestProject\StackTestProject\bin\Debug\StacktestProject.dll
and attach to proces nunit-agent.exe program stops on breakpoint and now assign of Window take around 1second, second assign takes around 700ms which is still longer than running same test from visual studio adapter.
I must notice that its not problem with attaching to proces, becouse without attaching i can see as well, that this test takes longer than from visual studio.
Really interesting thing is that i reproduce this issue only on winforms applications. When i automatize app like notepad which is base on Win32 framework times are normal or even faster. Same thing on web application, and automatizing with selenium webdriver.

Visual Studio detecting Microsoft Edge as Windows Store App incorrectly when making Coded UI Test

I am currently attempting to set up Coded UI Testing for a Web App. I created a "Coded UI Test Project" using Visual Studio Enterprise 2015.
I have installed the Microsoft WebDriver and added the 4 NuGet Packages needed to the Coded UI Test Project.
When using the "Coded UI Test Builder", I start on a fresh, new tab on Microsoft Edge, then hit record on the UI Test Builder. No matter what action I take, the builder spits out:
To test Windows Store apps, use the Coded UI Test project template for Windows Store apps under the Windows Store node.
I have not installed the Chrome WebDriver myself, but when I click on Chrome and do various things it does work correctly and creates the code for the recorded section. Is there a step I may have forgotten to get this working correctly for Microsoft Edge?
I was under the pretense when I made this question that I would be able to use the Coded UI Tests with Microsoft Edge. However, it turns out that it just isn't compatible at this point in time.
If you wish to do UI Tests with Microsoft Edge you will need to create a Unit Test Project (Not Coded UI Test Project). You will also not be able to use the UI Test Builder with it.
Here is an example UI Test with a Unit Test Project used with Microsoft Edge
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Support.UI;
namespace ExampleUITest
{
[TestClass]
public class ExampleUITest
{
private IWebDriver driver;
private string serverPath = "Microsoft Web Driver";
private string baseUrl = "http://example.com";
[TestInitialize]
public void TestInitialize()
{
if (Environment.Is64BitOperatingSystem)
{
serverPath = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), serverPath);
}
else
{
serverPath = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles%"), serverPath);
}
var options = new EdgeOptions
{
PageLoadStrategy = EdgePageLoadStrategy.Eager
};
driver = new EdgeDriver(serverPath, options);
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5));
}
[TestCleanup]
public void TestFinalize()
{
// Automatically closes window after test is run
driver?.Close();
}
[TestMethod]
public void LoadHomePage()
{
driver.Url = baseUrl;
var element = driver.FindElement(By.LinkText("Example Link Text"));
element.Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(w => w.Url == $"{baseUrl}/ExampleLink");
Assert.AreEqual(driver.Url, $"{baseUrl}/ExampleLink");
}
}
}
More Information can be found in this blog post

Why is UnitTestOutcome set to Unknown when running tests on TeamCity?

I am checking TestContext.CurrentTestOutcome in my TestCleanup method in order to perform an action if the test did not pass (in this case, the tests are using Selenium to exercise a website and I am saving a screenshot if the test does not pass).
private static TestContext _testContext;
private static IWebDriver _driver;
[ClassInitialize]
public static void SetupTests(TestContext testContext)
{
_testContext = testContext;
_driver = new FirefoxDriver();
}
[TestCleanup]
public void TeardownTest()
{
if (_testContext.CurrentTestOutcome != UnitTestOutcome.Passed)
{
var fileName = Path.Combine(
Environment.CurrentDirectory,
string.Format("{0}.{1}.gif", _testContext.FullyQualifiedTestClassName, _testContext.TestName));
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(fileName, ImageFormat.Gif);
Console.WriteLine("Test outcome was {0}, saved image of page to '{1}'", _testContext.CurrentTestOutcome, fileName);
}
}
This works well when run on a local development PC using ReSharper, but on our build server (which uses TeamCity) the UnitTestOutcome is always Unknown, although TeamCity reports them as passed.
The documentation on MSDN is not very helpful. What can cause this value to be set to Unknown?
According to http://confluence.jetbrains.com/display/TCD8/MSTest+Support TeamCity does not support on-the-fly reporting of individual test results, it parses the tests results file to provide the results to the build step.
That would explain how TeamCity is able to report the tests as passed even though UnitTestOutcome may be unknown at the time an individual test has completed.
The link above mentions "specifics of MSTest tool" as the reason for non-on-the-fly test result reporting so I can only theorize that the same specifics may mean that TestContext is unavailable when running from your build server.
Also, the MSDN documentation for TestContext.CurrentTestOutcome does mention that Full Trust for the immediate caller is required. TeamCity could be executing the tests in a manner that is only partially trusted and therefore causing the test outcome to be Unknown.
A quick way to check if MSTest is your problem would be to switch to NUnit using:
#if NUNIT
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using IgnoreAttribute = NUnit.Framework.IgnoreAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IgnoreAttribute = Microsoft.VisualStudio.TestTools.UnitTesting.IgnoreAttribute;
#endif
source http://www.anotherchris.net/tools/using-team-city-for-staging-and-test-builds-with-asp-net-and-selenium/
You would have to do something similar in your TeardownTest method to use the NUnit TestContext.CurrentContext.Result.Status though.
The fix for this issue is to use a public property for TestContext, rather than using the parameter passed to the [ClassInitialize] method.
i.e.
public TestContext TestContext { get; set; }
The test runner will automatically set the property.
(This is related to another question I posted on SO)

Categories