How to start WinAppDriver programmatically in the remote machine - automation

My test setup consist of 2 windows machines, first one test runner which will have the my test code in c# and the second one test agent machine where winappdriver is installed along with application under test.
I would like to start the winappdriver in the test agent through the C# code and the code would run on the test runner. Also, I would like to close the winappdriver once test execution is over.
How this could be done? Appreciate any lead on this.

For Java specific projects: You can Do it in following ways-
Assumin WinAppDriver is installed in default location-i.e. C:/Program Files (x86)/Windows Application Driver
1st:
String command = "C:/Program Files (x86)/Windows Application Driver/WinAppDriver.exe";
Runtime.getRuntime().exec(command);
2nd:
String command = "C:/Program Files (x86)/Windows Application Driver/WinAppDriver.exe";
ProcessBuilder builder = new ProcessBuilder(command).inheritIO();
Process process = builder.start();
Dont forget to dispose it by-
winDriver.close();
winDriver.quit();
or for 2nd approach-
process.destroy();
Convert them as part of you init or BeforeTest Methods as needed.

You can use [BeforeTestRun] attribute in your Test initialize class. Assuming the winappdriver is installed in the same path in the test machines. I use it in my azure agents
[BeforeTestRun]
public static void TestSetup()
{
Process.Start(#"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe");
Process.Start(#"Your application Path");
}
After the test run, if you want to close the Winappdriver, you can use AfterTestRun Attribute. Basepage has the static instance of the WinappDriver
[AfterTestRun]
public static void TearDownReport()
{
BasePage.WindowsDriver.Close();
BasePage.WindowsDriver.Dispose();
}

Related

Validate that NUnit's ITestEventListener within Jenkins Pipeline for a netcoreapp3.1 assembly is called

Locally, I have successfully implemented the interface ITestEventListener within a C# netcoreapp3.1 csproj. However, when the tests are an within a Jenkins Pipeline, things appear to not be working(?).
I am using version 3.12.0 of NUnit.Engine.
By locally I am referring to using 1) Visual Studio Version 16.9.2 to run the tests and 2) command line dotnet test -c devint --test-adapter-path:. --logger:nunit to run the tests. I am getting successful test runs.
Success is my [Extension]public class ReportTestListener : ITestEventListener {...} generates an html file. I am to see the html file is created locally whereas from the Jenkins Pipeline the html file is not generated.
Within the Jenkins Pipeline, I am using the command sh "dotnet test -c ${env.TARGET_ENV} --test-adapter-path:. --logger:nunit" where env.TARGET_ENV resolves to devint. I know tests successfully run within the Jenkins Pipeline since the NUnit test results file is generated/published.
What I am not sure of is how to test/validate that the ReportTestListener is being called within the Jenkins Pipeline. I know that testing frameworks such as NUnit uses refection to identify test classes and methods. I am presuming that also happens with my implementation of [Extension]public class ReportTestListener : ITestEventListener {...}. Ideas/Suggestions on how to validate that ITestEventListener's method void OnTestEvent(string report) is being called besides writing out to disk?
Changed file writing text path to use / instead of \.

How to save NUnit test results to xml?

I am trying to learn Selenium in C# and everything is going smooth so far. But I wanted to export reports to xml.
Looked over some answers but did not find any exact answer. Seems like it is done with NUnit console, but what command is not specified anywhere. Would appreciate even for useful links.
Using:
VS 2017
NUnit 3.11.0
Selenium 3.14.0
Have only 1 Test
[Test]
public void Open()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://google.com");
Assert.AreEqual("Google", driver.Title);
driver.Close();
driver.Quit();
}
So after some research I found the answer. The commands is:
nunit3-console "file.dll"
DLL file can be found in bin\debug\projectname.dll after building the solution (CTRL+SHIFT+B).
Utilizing Test Explorer and the NUnit or NUnit 3 connector, there is no real way to spare the test results as a XML record. You would, as you say, need to introduce the NUnit reassure sprinter and run tests with it. You can utilize the NuGet NUnit.Runners bundle to get a duplicate of NUnit3-support sprinter into your undertaking.
See this answer
NUnit testing can be run with the nunit-console.exe application which is installed with nunit under {Project_root}/lib/nunit/nunit-console.exe. It downloads with NuGet when NUnit installs.
It can be passed a list of testing binaries, or testing project files, or an nunit project (listing multiple if needed).
{PathToProject}\lib\nunit\nunit-console.exe{PathToTestDll}\Project1.Tests.dll{PathToTestDll}\Project2.Tests.dll /xml=nunit-result.xml
or create an NUnit Project with the NUnit Project Editor if you want to group all your test projects into a single config file.
{PathToProject}\lib\nunit\nunit-console.exe{PathToNUnitProject}\Project.Tests.nunit/xml=nunit-result.xml
For Nunit 2.6.4, the command is as follows, see below. Using the option /result you can select location of where the results in XML will be saved:
nunit-console.exe "D:\X-Test\TestFramework\Aumentum.Tests\bin\Debug\Aumentum.Tests.dll" /out:"d:\NUnitOutput.txt" /result:"d:\TestResult.xml" /trace=Verbose /labels /noshadow /framework=net-4.6 /nothread

The NUNIT automation scripts are NOT running from Task Scheduler

I am able to execute the NUNIT scripts using batch file. I am trying to run this batch file from Task scheduler to make it unattended and run regularly.
But bat file runs and provide output that window which is trying to perform automation is not opened.
Those are the parameters I'm using to launch the NUnit console:
cd C:\Program Files\NUnit.org\nunit-console
NUNIT3-CONSOLE D:\nunit\UnitTestProject1.dll --result="D:\nunit\TestResult.XML"
My code looks like this:
[Test]
public void TestMethod1()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("www.oford.com");
}
Is there any way to run the batch files in unattended mode ?
I've started my first setup with the same approach, but some user privileges and Windows settings may interrupt at any time.
Is there any way to run the batch files in unattended mode ?
Yes, you can use a CI server. It is really to setup, free and powerful once you get the hang out of it. I would recommend Jenkins, because of its great community and vast resources (tutorials,plugins etc.). Configuring a basic job takes no more than five minutes. The reporting is also great.

Jenkins + Selenium WebDriver + MSTest issue

I have created a test method in Visual Studio 2010 Ultimate that checks for the existance of two text boxes. I instantiate "InternetExplorerDriver" in the AssemblyInitialize() method and have setup Jenkins (on Windows 7) to run the MSTest method using the MSTestRunner plugin. The test seem to pass but I'm not seeing the DOS command window that I see when the "InternetExplorerDriver" instantiates and also I'm not seeing the Internet Explorer browser from loading the web page at all even though the test has passed. I also call the Quit() method on the webdriver in AssemblyCleanup. I'm running Jenkins service as my own NT account on my local box as running the service as "Local System Account" has issues. Here's my code snippet:
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
webDriver = new InternetExplorerDriver(ieDriverDirectory, ieOptions);
The ieDriverDirectory has the InternetExplorerServer.exe running in 32bit mode. When I run the same test method from Visual Studio 2010 IDE or from the mstest.exe /testcontainer:, I'm able to see the DOS command window loading with the port number, the browser loading, the test method passing and the browser closing at the end when Quit() is called.

Jenkins plugin - environment variables

I am using Jenkins with Testswarm and this plugin (forked sources).
I want to get a "job name" for Testswarm containing the Jenkins job name, build number and svn revision number.
Putting JOB_NAME in the configuration field does not help, the variable is not replaced by its value.
So I modified the plugin source code to get the Jenkins environment variables but all I get are "null"s.
Here is the culprit code. (in src/main/java/com/javaclimber/jenkins/testswarmplugin/TestSwarmBuilder.java from line 205)
I researched a lot concerning this functionnality and I did not find a working example for getting a variable.
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
...
EnvVars envVars = build.getEnvironment(listener);
...
envVars.get("JOB_NAME")
}
I am not at ease in Java and I am stuck at this point.
Any idea anyone, please ?
Update: java used version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~10.04.2)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
Replacing
EnvVars envVars = build.getEnvironment(listener);
By
EnvVars envVars = new EnvVars();
envVars = build.getEnvironment(listener);
did the trick...
What version of Java are you using?
According to this, to get an environment variable, you need to add the following:
String job_name = System.getenv("JOB_NAME");
Have you tried this instead?
Also, I'm not sure what the configuration field looks like, but did you try using $JOB_NAME instead of JOB_NAME ?
Jenkins pipelines appear to execute on the server, so the original post may be missing a variable defined for the agent (either via the server's agent definition or via the agent's OS).
I don't know the Jenkins pipeline way of obtaining the agent's environment.
I stumbled over this and searched a while for a solution, so for the next user the answer: If you need the environment variables, overwrite
public void perform(Run<?, ?> run, FilePath workspace, EnvVars env, Launcher launcher, TaskListener listener)
instead of
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)
and use the EnvVars from the parameters. (See also https://issues.jenkins.io/browse/JENKINS-29144)