C# ChromeDriver throws exception when executed dynamically by reflection - c#

I'm trying to execute test program which uses selenium web driver in custom test runner.
In the test runner, selenium web driver equiped test method is invoked by reflection.
When the test program is run by Visual Studio Test Explorer, it works fine.
Problem occurs when it is run dynamically by reflection.
The test program is as follows.
namespace TrialWebUnitTest
{
public class WebDriverTest01
{
private IWebDriver driver;
[TestMethod]
public void NavigateToSeleniumHQByChrome()
{
string TargetUrl = "https://www.seleniumhq.org/projects/webdriver/";
this.driver = new ChromeDriver();
driver.Manage().Window.Size = new System.Drawing.Size(1000, 800); // <- driver throws exception here.
this.driver.Navigate().GoToUrl(TargetUrl);
this.driver.Dispose();
}
}
}
The custom test runner's core test executing method is as follows.
namespace TrialWebUnitTestRunner
{
public partial class TestForm : Form
{
// test button click's event handler.
private void TestButton_Click(object sender, EventArgs e)
{
string retErrorMsg = string.Empty;
if (!ExecTestDynamically(ref retErrorMsg))
{
// show error information in UI textbox.
this.this.ResultMessage.Text = retErrorMsg;
}
}
internal bool ExecTestDynamically(ref string retErrorMsg)
{
var target = new TrialWebUnitTest.WebDriverTest01();
System.Type targetType = typeof(TrialWebUnitTest.WebDriverTest01);
var method = targetType.GetMethod("NavigateToSeleniumHQByChrome");
try
{
method.Invoke(target, null);
return true
}
catch (Exception exp)
{
retErrorMsg = exp.Message + Environment.NewLine + exp.StackTrance;
return false;
}
}
}
}
Exception information
System.InvalidOperationException:
disconnected: unable to connect to renderer
(Session info: chrome=65.0.3325.181)
(Driver info: chromedriver=2.31.488763
Sample program
I've written a sample program to reproduce the error.
Please download it from my dropbox url: https://db.tt/HqUTMOKWBl
You can click the link and download 'TrialWebTestForInspection.zip'.
Please extract it in any arbitrary folder and find TrialWebTestForInspection.sln.
The solution consists of two projects, "TrialWebUnitTest" and "TrialWebUnitTestRunner".
The first one is MS Unit Test, and you can run 3 test methods from Visual Studio Test Explorer.
The 3 test methods are very simple. They just launch the browser correspond to the webdriver, and navigate to Selenium HQ site.
The other project is a WindowsForm application which provide a very simple test runner.
It kicks the test methods in previous test project.
When you choose the test method using IE or FireFox driver, it works fine.
On the other hand, when you choose Chrome driver test, it thows exception which I mentioned above.
Things I'd like to know.
First I'd like to know, if it is a bug of current Chrome driver version, or it is a part of specification.
Then I'd like to know, if there is a way to avoid this problem or not.
What I'm afraid of is the possibility that IWebDriver specification originally does not support correct action whent it is run by reflection.
spec of sample program
.NET Framework version 4.6.1
nuget package
MSTest.TestFramework.1.2.0 MSTest.TestAdapter.1.2.0
Selenium.WebDriver.3.11.0 Selenium.WebDriver.ChromeDriver.2.37.0
Selenium.WebDriver.IEDriver.3.11.1 Selenium.Firefox.WebDriver.0.20.0
Chrome Browser version 65.0.3325.181(Official Build)(64 bit)

The problem was solved. Please refer to this.
The custom TestRunner application didn't refer to the latest chrome-driver version. I had to install selenium.webdrivers not only to the testclass, but also to the testrunner program.
For further information, please refer to the following issue in GitHUB.
https://github.com/SeleniumHQ/selenium/issues/5705

Related

How do I add Hyperlinking in NUnit Test Output?

I want to be able to add hyperlinks to relevant data in the output for a unit test.
I have the following test:
using NUnit.Framework;
namespace BioFire.PanelSoftware.Service.Tests
{
[TestFixture]
public class SimpleTest
{
[Test]
public void Test1()
{
Console.WriteLine("www.google.com"); //not hyperlink
Console.WriteLine(#"C:\Program Files"); //not hyperlink
throw new Exception("My output window will somehow give me a hyperlink to here.");
}
}
}
According to this question, it isn't possible in C#. But it is clearly working for nUnit somehow:
This is very specific to the terminal you are using and I don't believe anything in C# can achieve clickable text. You can technically use Process.Start() from the System.Diagnostics namespace to trigger the default browser to open the webpage you want, but this isn't a hyperlink and would rather be triggered by your specifications
If you are running from within an IDE, you would have to look into the underlying shell and try to swap it out for a different profile (ex. this should be easy on VS Code but I'm unsure if Visual Studio can support it). If you are running from cmd line, then try using the new Windows Terminal App as it supports this functionality

What is the simplest way to run nunit 3 tests from a button in a Windows Form?

I currently have an nunit project outputting a class library 'RegressionTests.dll' that opens the Selenium WebDriver and runs a few dozen UI tests. I have created a WinForm app with a button 'Run Tests'. When clicking this button, I want to execute a series of n-unit tests from RegressionTests.dll.
I had gotten this to work on my local machine using Process.Start("nunit3-console.exe, nunit-console RegressionTests.dll"), but realized that it would only work on my local if I had installed nunit3-console as a standalone app. After realizing this, I dug more into the n-unit documentation and discovered the n-unit engine. I have tried leveraging the n-unit3 Engine in order to run it internally but have faced issues with implementation of the ITestEventListener in the WinForm project. I've attached the code to my button here:
Form1.cs
private void btnRun_Click(object sender, EventArgs e)
{
TestRunner.Run();
}
Inside TestRunner.cs, we have this code:
[Extension(Description = "Test Reporter Extension", EngineVersion = "3.11")]
public class TestRunner : ITestEventListener
{
public static void Run()
{
ITestEngine engine = TestEngineActivator.CreateInstance();
TestPackage package = new TestPackage("RegressionTests.dll");
ITestEventListener testListener = new TestRunner();
using (ITestRunner runner = engine.GetRunner(package))
{
XmlNode result = runner.Run(testListener, TestFilter.Empty);
}
}
public void OnTestEvent(string report)
{
throw new NotImplementedException();
}
}
Currently, the solution layout is as follows.
Solution
Regression (project)
RegressionTests.dll
TestRunner.cs (file that contains my code linked above)
SeleniumFormApp
Form1.cs (contains button that, upon click, should run Selenium test cases)
How can I leverage n-units Nuget packages to accomplish what I want to here? Is n-unit engine the proper one? If so, how should the ITestEventListener be implemented to accomplish this?
Thank you - please let me know if this is unclear.

Why do tests disappear from Microsoft Visual Studio when adding code to existing tests that already work?

I have hit an issue which I cannot seem to figure out.
My project is on Microsoft Visual Studio (2017), using C#/Selenium/NUnit.
I have tests in my project that are currently working. I went back to these tests to add code so they can run on multiple browsers (mainly Chrome and Firefox).
Work started on this about a month ago, and I was able to add the code to existing tests and those tests run on multiple browsers.
However, when I went back to the remainder of the tests to add this code, I noticed that the test disappeared from the test explorer after cleaning/rebuilding the project.
What could be causing this? I did not make any major changes to the project.
The code in question is below. Any help is greatly appreciated. Thank you!
With the code below, the test disappears. If I remove this code, the test is back after a rebuild/build.
Here is the code I am adding:
namespace NewProject
{
//adding this for NUnit
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(ChromeDriver))]
//adding code after the class
class ProjectAMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new()
{
//adding the private declaration below
private IWebDriver driver;
TPTMethods tptDo = new TPTMethods();
URLComparison urlComp = new URLComparison();
TakeAScreenShot tas = new TakeAScreenShot();
TakeAScreenShotFullName tasf = new TakeAScreenShotFullName();
[Test, Order(1)]
public void ProjectALoginMult()
{
//adding the driver object below
driver = new TWebDriver();
try
{
.....
This is how I discovered the test filter icons across the top of the VS Test Explorer. I inadvertently unknowingly clicked the "not run tests" icon; why did all my tests disappear????!!! I was about to embarrass myself going to my supervisor when I inadvertently clicked the "passed tests" icon; they came back! But then new tests didn't show. Lightbulb lit up - I realized it's because I had it filtered for passed tests, not tests that didn't run yet. There's a "total tests" icon. Now I know.

Run data driven coded ui from console app

I have a requirement to run Coded UI test that is data driven from console application. If I run the Coded UI test as a standalone, then [DataSource] can access the values from the CSV file. Whereas if I call Coded UI from the console app, I get the unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object. - since TestContext.DataRow is null.
Here’s snippet from the code
Program.cs (console app):
public class Program
{
static void Main(string[] args)
{
Playback.Initialize();
CodedUITestWarmup test = new CodedUITestWarmup();
test.WarmUp();
Playback.Cleanup();
}
}
CodedUITestWarmup.cs (coded ui test):
public class CodedUITestWarmup
{
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", #"|DataDirectory|\DataFiles\warmup.csv", "warmup#csv", DataAccessMethod.Sequential)]
public void WarmUp()
{
InitializeVendorTest();
...
}
private void InitializeVendorTest()
{
caseV = new CaseVariables(TestContext);
...
}
}
class CaseVariables
{
public string lastNameID;
...
public CaseVariables(TestContext TestContext)
{
lastNameID = TestContext.DataRow["lastNameID"].ToString();
...
}
}
Could you please provide some inputs what can be done in this regard?
You cannot run codded ui test without vstestconsole.exe or from visual studio.
You can try run vstestconsole.exe with parameter (path to your test dll)
eg.
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstestconsole.exe
Dll file will be created during each build the codedui test project
Why do you need the test to run from a console application?
I believe you can use either vstestconsole.exe or mstest.exe
vstestconsole is command line tool that replaces MStest. But in this case I thing any of them can do wat you want!
A Coded UI Test or Unit Test will allwais need to have the TestContext initialized, and the test engine is responsible to do that, that is why you get an exception.
In my PC the mstest executable is in "c:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\"
So I can start the command prompt, navigate to the mstest.exe folder (the one abouve) and use a command like the following one:
mstest.exe /testcontainer:"c:/TestFolder/testassembly.dll" /test:"TestNamespace.MyTestToExecute_TestMethod1"

nunit TestContext throws NullReferenceException

I have the following code:
[TestFixture]
public class LexicalTests
{
[Test]
public void LexicalTest1()
{
TestContext.CurrentContext.TestDirectory;
}
}
CurrentContext throws an exception while attempting to get TestDirectory or WorkingDirectory property.
How can I solve this problem?
P.S.: On my home PC tests work perfectly (without strange exceptions).
It seems that some applications that offer the functionality to run NUnit unit tests have a problem with the TestContext class.
The test in class below should pass:
using NUnit.Framework;
namespace UnitTests
{
[TestFixture]
public class UnitTests
{
[Test]
public void CurrentContextTest()
{
Assert.IsNotNull(TestContext.CurrentContext);
Assert.IsNotNull(TestContext.CurrentContext.TestDirectory);
Assert.IsNotNull(TestContext.CurrentContext.WorkDirectory);
}
}
}
If the test doesn't pass then, as Dmitry wrote in his comment above, change the NUnit version in the ReSharper menu. From within Visual Studio, go to ReSharper -> Options -> Tools -> NUnit. Click the Specified NUnit installation radio button and ensure that a folder with nunit.core.dll, nunit.core.interfaces.dll and nunit.util.dll is specified. An error will be displayed if the listed files cannot be found.
Once the NUnit version has been changed, re-run the test and it should pass.

Categories

Resources