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

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.

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

How to assert in selenium webdriver in 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!

How to open a browser and keep it open between coded UI tests in Visual Studio 2017 using C#

I am trying to make some coded UI tests to help automate some of the manual testing at the company I work at. I am pretty new to the CUIT part of visual studio, but I feel like I am figuring it out. However I am having an issue with the testing thread being closed before the other tests run.
So I want to make this testing fully automated, as in, all the developer will need to do is to click "Run all" and they will all run automatically. The problem that I am having is that the very first test needs to launch Internet Explorer, go to a website, and log into the website. The rest of the tests are based off of being logged into the system. However, after the first test, the browser closes and gets killed along with the first test method.
Any advice on this would be great, I have searched online for some answers but a lot are for very old versions of visual studio, and the ones I have tried don't work.
Thank you.
Edit: So inside each class lets say "CodedUITest1.cs", I can use the same browser in each of the [Test Method]s that I have in that class (as someone suggested below). The issue I have is that if I want a different test class to test different functionality, "CodedUITest2.cs", the browser will close when the first class finishes its tests.
If I'm understanding your question correctly, then This code segment should work for you:
BrowserWindow window;
[TestMethod]
public void Method1()
{
window = BrowserWindow.Launch(new Uri("http://www.bing.com"));
window.CloseOnPlaybackCleanup = false;
}
[TestMethod]
public void Method2()
{
window = BrowserWindow.Locate("Bing");
window.CloseOnPlaybackCleanup = false;
}
[TestMethod]
public void Method3()
{
window = BrowserWindow.Locate("Bing");
}
After reading the new info of this question, I have tested the code a bit. If you want to keep the browser open between CodeUITes1.cs and CodedUITest2.cs, then the following code segment may help you. It is adopted from the following link: https://blogs.msdn.microsoft.com/devops/2012/11/08/coded-ui-test-why-does-application-close-after-each-test-in-visual-studio-2012/
File: CodedUITest1.cs
public class CodedUITest1
{
static BrowserWindow browserWindowInstance = null;
public void LoadLocalHost()
{
if (browserWindowInstance == null)
{
browserWindowInstance = BrowserWindow.Launch(new System.Uri("YourWebSiteAddress"));
browserWindowInstance.CloseOnPlaybackCleanup = false;
browserWindowInstance.Maximized = !browserWindowInstance.Maximized;
}
else
{
browserWindowInstance.Maximized = !browserWindowInstance.Maximized;
}
}
[TestMethod]
public void CodedUITestMethod1()
{
LoadLocalHost();
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
this.UIMap.ClickNewsAndEvents();
}
CodedUITest2.cs file:
[TestMethod]
public void CodedUITestMethod2()
{
CodedUITest1 obj1 = new CodedUITest1();
obj1.LoadLocalHost();
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
this.UIMap.ClickNewsPage();
}
You can add more CodedUITest classes. Just create a new object like obj1 in the code sample of CodedUITest2 class, and use LoadLocalHost() method that resides in CodedUITest1.class from any subsequent classes. Hoping this will resolve your problem.

Cannot launch Repl() (Xamarin)

I'm writing UITests on Xamarin. I'm trying to launch the Repl window, but it doesn't launch.
My code:
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.Queries;
namespace MurakamiKiev.UITests
{
[TestFixture]
public class Tests
{
AndroidApp app;
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.StartApp();
}
[Test]
public void ClickingButtonTwiceShouldChangeItsLabel ()
{
app.Repl();
}
}
}
What's wrong with my code? Thanks for the help.
I had the same issue, the problem was that app.Repl(); was already running in the background, from the previous session.(When you are debugging if you terminate debugger it wont close automatically)
The error I got was:
System.Exception: 'Error while performing Repl()
Inner exception
IOException: The process cannot access the file 'C:\Users\daniel\AppData\Local\Temp\uitest\repl\ICSharpCode.NRefactory.CSharp.dll because it is being used by another process.
So you just need to close app.Repl(); command prompt that is running in the background
Nothing is wrong with that code. I have seen this happen before but now can't recall the exact resolution. Might have been using NUnit version >= 3. Can you see if you still get the same issue using NUnit version 2.6.3?

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

Categories