Run Tests on multiple browsers in parallel - c#

I found the way to run tests on multiple browser one by one, but I can't find a way to use selenium grid in order to run my tests on multiple browsers in parallel(with C#).
That's what I'm using currently:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System.Threading;
namespace SeleniumTests
{
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new()
{
private IWebDriver driver;
[SetUp]
public void CreateDriver () {
this.driver = new TWebDriver();
}
[Test]
public void GoogleTest() {
driver.Navigate().GoToUrl("http://www.google.com/");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Bread" + Keys.Enter);
Thread.Sleep(2000);
Assert.AreEqual("bread - Google Search", driver.Title);
driver.Quit();
}
}
}
Run Selenium tests in multiple browsers one after another from C# NUnit

In the test explorer in Visual Studio there should be a button that has three lines with circles on the left side of the lines and arrows on the right sides (if you hover over it it says 'Run Tests in Parallel'). Make sure this is checked (its background will be a different colour to the test explorer panels when selected).
For each test you want to be parallel add the attribute Parallelizable. So the thing above your method that looks like [Test] will become [Test, Parallelizable].
Extra - This doesn't apply to you but will apply to a few people so I'll add it. If your Driver is a static instance (like a Singleton) you need to annotate it with [ThreadStatic] or else the different tests will all be called on the same driver.

Related

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.

Selenium WebDriver C# test case is inadvertently running twice

I'm using VS 2015 community. My Selenium C# test case always runs twice. The Test Case Explorer window shows 1 test case was run but the pass result shows two of the same test cases were executed.
What is wrong with my test or framework?
I have created a Test File with testcase (NunitDemo.cs) under my project.
I attached a screenshot to my Solutions Explorer window as well.
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace DemoNunit
{
[TestFixture]
public class NunitDemo
{
private IWebDriver driver;
[Test]
public void tc_newAccount()
{
//open browser and navigate to aut
driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.trainingrite.net");
//click on signup button
driver.FindElement(By.CssSelector("input.submitbtn")).Click();
//enter firstname, lastname, email, password
driver.FindElement(By.Id("ctl00_MainContent_txtFirstName")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtFirstName")).SendKeys("Ren");
driver.FindElement(By.Id("ctl00_MainContent_txtLastName")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtLastName")).SendKeys("G");
driver.FindElement(By.Id("ctl00_MainContent_txtEmail")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtEmail")).SendKeys("rmng3#yahoo.com");
driver.FindElement(By.Id("ctl00_MainContent_txtPassword")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtPassword")).SendKeys("12345");
driver.FindElement(By.Id("ctl00_MainContent_txtVerifyPassword")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtVerifyPassword")).SendKeys("12345");
driver.FindElement(By.Id("ctl00_MainContent_txtHomePhone")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtHomePhone")).SendKeys("951-265-1234");
driver.FindElement(By.Id("ctl00_MainContent_txtCellPhone")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtCellPhone")).SendKeys("760-855-1234");
driver.FindElement(By.Id("ctl00_MainContent_txtInstructions")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtInstructions")).SendKeys("Running first selenium automation scripts in C#!");
//click on submit button
driver.FindElement(By.Id("ctl00_MainContent_btnSubmit")).Click();
//verify new customer is added successfully
Assert.AreEqual("Customer information added successfully", driver.FindElement(By.Id("ctl00_MainContent_lblTransactionResult")).Text);
}
}
}
Do you have both the Nunit 2.x and 3.x Test Adapter installed in VS? If so try removing one of them and running the test.
As an addition to Dmitry's answer, if you have the NUnitTestAdapter installed via the Extensions as well as via the NuGet package, the tests will run twice. This is a known issue.

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

Visual Studio Test Explorer with playlists

This may be related to question: Dynamic playlists of unit tests in visual studio.
I want to be able to have one or more playlists of tests and not have not add every single new test to a certain playlist.
I currently have one playlist containing all my unit tests, but in the future I want to have a playlist consisting of automated integration tests, which should be run before committing to TFS but not every time the application builds.
Is there a way to do this?
Im not aware of the types of settings you can use in TFS, since Im not using TFS, but I know it is possible using Categories in both, NUnit and MSTest.
Solution With NUnit
With NUnit, you can mark single tests or even the whole fixtures with a Category-Attribute:
namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
[Category("IntegrationTest")]
public class IntegrationTests
{
// ...
}
}
or
namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
public class IntegrationTests
{
[Test]
[Category("IntegrationTest")]
public void AnotherIntegrationTest()
{
// ...
}
}
}
and the only run those using nunit-console.exe:
nunit-console.exe myTests.dll /include:IntegrationTest
Solution with MSTest
The Solution for MSTest is very similar:
namespace MSTest.Tests
{
[TestClass]
public class IntegrationTests
{
[TestMethod]
[TestCategory("IntegrationTests")
public void AnotherIntegrationTest()
{
}
}
}
But here you have to mark all Tests with that attribute, it cannot be used to decorate the whole class.
Then, like with NUnit, only execute those tests in the IntegrationTests-category:
Using VSTest.Console.exe
Vstest.console.exe myTests.dll /TestCaseFilter:TestCategory=IntegrationTests
Using MSTest.exe
mstest /testcontainer:myTests.dll /category:"IntegrationTests"
EDIT
You can also execute certain Test-Categories using the TestExplorer of VS.
(source: s-msft.com)
As seen in the above image, you can select a category in the top left corner of the TestExplorer. Select Trait and execute only the catgegory that you want to.
See MSDN for more Information.

Categories