Closing all open command prompt leads to error in forked process in testNG - selenium

I am using testNG for mobile automation and I want to close multiple command prompts(appium servers) which i launched. For this, I am using the below code
#AfterSuite
public void closeCommandPrompts() throws IOException, InterruptedException{
System.out.println("After suite");
Thread.sleep(8000);
Runtime.getRuntime().exec("taskkill /F /IM node.exe");
System.out.println("closed node.exe");
Runtime.getRuntime().exec("taskkill /F /IM cmd.exe");
The last line if I commnet it out works fine, however if they are not commented it gives an error in the forked process.
I guess testNG is internally using command prompt which i am trying to close when iam using taskkilll in #aftersuite.
Please help me in getting some work around.

You can use following code :
CommandLine command = new CommandLine("cmd");
command.addArgument("/c");
command.addArgument("taskkill");
command.addArgument("/F");
command.addArgument("/IM");
command.addArgument("node.exe");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(command, resultHandler);
System.out.println("Stopped appium node ! ");
} catch (IOException e) {
System.out.println("FAIL => Unable to stop appium server "+e.getMessage());
e.printStackTrace();
}

Related

Appium Unknown server-side error occurred - did not start application

I am trying to test a mobile app in appium but its throwing the following error
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command.
Original error: Cannot start the 'com.example.abc' application.
Original error: 'com.example.abc.ui.splash.SplashActivity' or 'com.example.abc.ui.splash.SplashActivity' never started.
Visit https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/android/activity-startup.md for troubleshooting
(WARNING: The server did not provide any stacktrace information)
Follwing are my capabilities setup
#Before
public void setUp() {
File f = new File( "src" );
//App Name
File fs = new File( f, "app-sandbox-debug.apk" );
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability( "deviceName", "Samsung SM-A305F/DS Android 10, API 29" );
capabilities.setCapability( "platformName", "Android" );
capabilities.setCapability( CapabilityType.BROWSER_NAME, "Android" );
capabilities.setCapability("normalizeTagNames","true");
capabilities.setCapability( MobileCapabilityType.APP, fs.getAbsolutePath() );
try {
driver = new AndroidDriver<MobileElement>( new URL( "http://127.0.0.1:4723/wd/hub" ), capabilities );
driver.manage().timeouts().implicitlyWait( 1000, TimeUnit.SECONDS );
System.out.println("Application running");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
I am unable to find the error cause nor unable to find what's missing at my end.
You can use adb shell dumpsys window windows command to see the launch activity
Launch the app on the device
connect the device to PC/Laptop with adb enabled
In terminal enter the following command adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"
Use the correct activity which can be launched properly

Commands endlessly executing in eclipse using JSCH

I am executing below code to establish a ssh connection to linux vm and execute commands using jsch from eclipse. code is like below:
try{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession("prkotagi", "slc10gst.us.oracle.com", 22);
session.setPassword("P#rs#1234nth");
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
But the issue here i am facing is when i execute the command in linux vm using eclispe. In eclipse the programm is running indefinitely like 30 mins. But actually the command should take 1 to 2 mins and it is showing log in my console. but the execution is getting strucked.
How to quit the command execution as soon as we got the o/p and the new line with bash $ displayed. But eclipse is not ending the execution.

Winium remote automation?

I am using Winium to automate a desktop application. The process is I download a file from the web and execute it. which opens the remote application. Till here everything is working fine, but I am unable to access any control of that newly launched remote Citrix application. Any help would be appreciated.
public void SetupEnv() throws InterruptedException
{
DesktopOptions options = new DesktopOptions();
options.setApplicationPath("C:\\Users\\ajinkya\\Downloads\\launch.ica");
try
{
driver = new WiniumDriver(new URL("http://localhost:9999"), options);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
Thread.sleep(5000);
driver.findElementByClassName("Transparent Windows Client").click();
Thread.sleep(5000);
}
Maybe you should try something like this:
WebElement window = driver.findElementByName("abcd.exe");
WebElement menuItem = window.findElement(By.name ("Find Item"));
menuItem.click();

How to kill many geckodriver.exe processes?

I am using Windows 10 pro x64, Firefox 50.x, Java 8, Selenium 3.0.1
public RemoteWebDriver remoteWebDriver;
//...
System.setProperty("webdriver.gecko.driver", browserWebDriverFilePath);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
remoteWebDriver = new FirefoxDriver(capabilities);
remoteWebDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
remoteWebDriver.manage().window().maximize();
//...
remoteWebDriver.quit();
Run this from Command line:
taskkill /F /IM geckodriver.exe
Or better of, put it in a batch file and run the file every time you want to clean up.
You can also do it from your code if you want, before you start running:
boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
try {
if (isDebug)
Runtime.getRuntime().exec("taskkill /F /IM geckodriver.exe");
} catch (IOException e) {
e.printStackTrace();
}
Only in debug to avoid killing instances on slave if you're running in parallel.
I recently encountered this problem with webdriver which was leaving many zombie geckodriver processes.
Placing the driver.quit() in finally block resolves this problem.
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
WebDriver driver = new FirefoxDriver(options);
try {
....
} finally {
driver.quit();
}
if driver.Close() and the execute driver.Quit(), this way not succes but overide driver.Close() with driver.Quit()
Not exec with terminal because will leave process in the server.

Browsers are not launching from command prompt. Same testng File is working while running through eclipse

I am trying to run my testng test cases from command prompt. I can see browser's instance are created in task manager but it is not launching with given URL. I debugged the code and found it is failing on new ChromeDriver() below line but there is no exception.
On command line, I can see below message :
Starting ChromeDriver 2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a) on port 7108
Only local connections are allowed.
Browser instance will create in task manager but it will not launch and will not go to next line.
This issue is coming from all browsers on same line.
same code worked well in Eclipse.
Using Selenium 2.53.0 and testng 6.9.13 versions
Try this solution:
ChromeDriverService service = null;
try
{
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(chromeDriver))
.usingAnyFreePort()
.build();
service.start();
}catch(IOException io){}
if(service.isRunning())
{
DesiredCapabilities cap = DesiredCapabilities.chrome();
try{
driver = new RemoteWebDriver(service.getUrl(), cap);
}catch (SessionNotCreatedException e)
{
}
}