How to execute a .bat or exe file from CodedUI script using Process.Start()? - coded-ui-tests

I am new to Coded UI. I have written a simple code to execute a .bat file from a CodedUITestMethod1() as below:
thisProcess.StartInfo.CreateNoWindow = true;
thisProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
thisProcess.StartInfo.FileName = #"C:\BVTBatch\PlayBack.bat";
thisProcess.StartInfo.UseShellExecute = false;
thisProcess.StartInfo.RedirectStandardOutput = true;
thisProcess.Start();
thisProcess.WaitForExit();
strException = thisProcess.StandardOutput.ReadToEnd();
Problem statement: When I debug the script, it gets executed but the batch file does not run. I tried executing iexplorer.exe, and observed same issue. The script gets executed with pass, but IE browser does not start.
However if I execute the same code from other console application or Unit Test project method, it gets executed successfully.
Can someone suggest what is the reason behind this? and how can we fix this in CodedUI?
Thanks in advance.

This would seem legit:
thisProcess.StartInfo.FileName = ("C:\BVTBatch\PlayBack.bat");

Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = new ProcessStartInfo ("C:\BVTBatch\PlayBack.bat");
p.Start();

Related

Open a text file in C# programmatically

I want to open a text file programmatically using C#. I have used :
System.Diagnostics.Process.Start(test.txt);
but this code is causing OS command injection problem when scanning for threats.
Is there any way that i can open a text file programmatically?? or way to bypass that OS command injection?
Thank you
You should call a program, say notepad:
Process.Start("notepad.exe", fileName);
the argument is the file name:
Process.Start("notepad.exe", "Test.txt");
See the problem with your code in the comments of this post:
Open a file with Notepad in C#
Try:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
string _path = "c:/filepath";
startInfo.Arguments = string.Format("/C start {0}", _path);
process.StartInfo = startInfo;
process.Start();

Running SoapUI test cases using testRunner

I am working on a SoapUI project where I need to run my test suite using test runner. I am using external groovy scripting for environment variable. The problem I am facing here is whenever I am running test case from test runner its return Workspace as null, which is used in External groovy. So in external groovy I am getting workspace as null causing error [getProjectByname() cannot be invoked on null]. Below is the
constructor of global script where workspace is used
AvengerAPITestManager(String TestProject, String TestSuite, String TestCase,String TestStep)
{
TestName = "AvengerAPITests";
testProject = SoapUI.getWorkspace().getProjectByName(TestProject);
tSuite = testProject.getTestSuiteByName(TestSuite);
tCase = testProject.getTestSuiteByName(TestSuite).getTestCaseByName(TestCase);
tStepName = TestStep.toString();
tStep=testProject.getTestSuiteByName(TestSuite).getTestCaseByName(TestCase).getTestStepByName (TestStep);
}
Above we have user SoapUI.getWorkspace() which is working fine when trying to run from soapUI but whever I m trying to run from testrunner SoapUI.getWorkspace comes out to be null. I even tried passing workspace like I am passing testproject name still it didnt worked.
I tried something like this also
AvengerAPITestManager(Object workspace,String TestProject, String TestSuite, String TestCase, String TestStep)
{
TestName = "AvengerAPITests";
testProject = workspace.getProjectByName(TestProject);
tSuite = testProject.getTestSuiteByName(TestSuite);
tCase = testProject.getTestSuiteByName(TestSuite).getTestCaseByName(TestCase);
tStepName = TestStep.toString();
tStep = testProject.getTestSuiteByName(TestSuite).getTestCaseByName(TestCase).getTestStepByName(TestStep);
}
In the above code I tries passing Workspace object from the test case as I passed Testcase name and all but still I m getting null for workspace. Please tell me how do I deal with the problem.
Here is usefull working example https://github.com/stokito/soapui-junit
You should place your sample-soapui-project.xml to /src/test/resources folder that will expose it to classpath
If you want to use soap ui in external code, try to directly create new test runner with specific project file:
SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
runner.setProjectFile( "src/dist/sample-soapui-project.xml" );
runner.run();
Or if you want to define test execution more precisely, you can use something like this:
WsdlProject project = new WsdlProject( "src/dist/sample-soapui-project.xml" );
TestSuite testSuite = project.getTestSuiteByName( "Test Suite" );
TestCase testCase = testSuite.getTestCaseByName( "Test Conversions" );
// create empty properties and run synchronously
TestRunner runner = testCase.run( new PropertiesMap(), false );
PS: don't forget to import soap ui classes, that you use in your code and put them to classpath.
PPS: If you need just run test cases outside the soap ui and/or automate this process, why not just use testrunner.sh/.bat for the same thing? (here is description of this way: http://www.soapui.org/Test-Automation/functional-tests.html)
I am not sure if this is going to help anyone out there but here is what I did to fix the problem I was having with workspace as null causing error[getProjectByname() cannot be invoked on null] When i run from cmd
try this:
import com.eviware.soapui.model.project.ProjectFactoryRegistry
import com.eviware.soapui.impl.wsdl.WsdlProjectFactory
import com.eviware.soapui.impl.wsdl.WsdlProject
//get the Util project
def project = null
def workspace = testRunner.testCase.testSuite.project.getWorkspace();
//if running Soapui
if (workspace != null) {
project = workspace.getProjectByName("Your Project")
}
//if running in Jenkins/Hudson
else{
project = new WsdlProject("C:\\...\\....\\....\\-soapui-project.xml");
}
if (project.open && project.name == "Your Project") {
def properties = new com.eviware.soapui.support.types.StringToObjectMap()
def testCase = project.getTestSuiteByName("TestSuite 1").getTestCaseByName("TestCase");
if(testCase == null)
{
throw new RuntimeException("Could not locate testcase 'TestCase'! ");
} else {
// This will run everything in the selected project
runner = testCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
}
}
else {
throw new RuntimeException("Could not find project ' Order Id....' !")
}
The above code will run everything in the selected project.

Mono restart console application

I have written a console application in mono for linux.
i have to start it with
"sudo mono app.exe"
Is there some posibility to restart the app when something happens.
For examlpe I run this app on the raspberry Pi, and when the app
detects some voltage on a special IO pin the app should restart automatically.
Please help me.
You can launch a new application instance using methods in the Process class and later exit.
http://www.dotnetperls.com/process-start
On Windows you can use cmd.exe to execute the script. In this blog post the author asks cmd to wait for a while, and then delete the executable,
http://blog.pedroliska.com/2010/05/20/c-self-destruct-windows-app/
You can use the same trick to restart the executable if on Windows.
var startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
// The delay is just making sure the exe to delete is done
// running.
var delayPings = 2;
var exeName = AppDomain.CurrentDomain.FriendlyName;
process.StandardInput.WriteLine("(ping -n " + delayPings + " 127.0.0.1) && (CALL " + exeName + ")");
Now for Linux, you just need to use bash (or another Shell) to replace cmd and also modify the command passed to it.

C# Process.Start is messing with URI's inside a batch file

This is just a quick question that I am sure someone will be able to answer quickly as I am most likely just missing something.
Lets say I have the following directory layout
Folder1
-> CurrentlyRunning.EXE
-> Folder2
ProcessToStart.Bat
ApplicationToStartFromBat.exe
This is the code inside the applications.
CurrentlyRunning.EXE:
var proc = new Process
{
StartInfo =
{
FileName = "Folder2/ProcessToStart.Bat",
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}
};
proc.Start();
ProcessToStart.Bat:
START ApplicationToStartFromBat.exe
Now, if I run ProcessToStart.Bat by double clicking on it, it will open ApplicationToStartFromBat.exe with no problems (which is good). If I run CurrentlyRunning.EXE (which will execute the code I posted above) the BAT file fails saying it can't find my EXE (which is really weird).
If I change the BAT file to:
START Folder2/ApplicationToStartFromBat.exe
and then run CurrentlyRunning.EXE, the bat will then properly open ApplicationToStartFromBat.exe. My problem is I can not change the code inside the bat for one reason or another.
Why is proc.Start() causing the bat file search root directory to change, and how do I stop this from happening?
Thanks
I think it is to do with where the working directory is for your exe file.
Try using ProcessStartInfo.WorkingDirectory to set the correct directory for your batch file.
var proc = new Process
{
StartInfo =
{
FileName = "Folder2/ProcessToStart.Bat",
WorkingDirectory = "DirectoryPath";
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}
};
proc.Start();

Write to file via jenkins post-groovy script on slave

I'd like to do something very simple: Create/write to a file located in the remote workspace of a slave via the jenkins groovy post-build script plug-in
def props_file = new File(manager.build.workspace.getRemote() + "/temp/module.properties")
def build_num = manager.build.buildVariables.get("MODULE_BUILD_NUMBER").toInteger()
def build_props = new Properties()
build_props["build.number"] = build_num
props_file.withOutputStream { p ->
build_props.store(p, null)
}
The last line fails, as the file doesn't exist. I'm thinking it has something to do with the output stream pointing to the master executor, rather than the remote workspace, but I'm not sure:
Groovy script failed:
java.io.FileNotFoundException: /views/build_view/temp/module.properties (No such file or directory)
Am I not writing to the file correctly?
While writing onto slave you need to check the channel first and then you can successfully create a file handle and start reading or writing to that file:
if(manager.build.workspace.isRemote())
{
channel = manager.build.workspace.channel;
}
fp = new hudson.FilePath(channel, manager.build.workspace.toString() + "\\test.properties")
if(fp != null)
{
String str = "test";
fp.write(str, null); //writing to file
versionString = fp.readToString(); //reading from file
}
hope this helps!
Search for words The post build plugin runs on the manager and doing it as you say will fail if you are working with slaves! on the plugin page (the link to which you've provided) and see if the workaround there helps.
Does the folder /views/build_view/temp exist?
If not, you will need to do new File( "${manager.build.workspace.remote}/temp" ).mkdirs()

Resources