Commands endlessly executing in eclipse using JSCH - 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.

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

Connection refused thrown when instantiating a new chrome driver

I have a selenium project with cucumber on my local Jenkins server.
DesiredCapabilities capability = DesiredCapabilities.chrome();
try {
baseUtil.driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
} catch (MalformedURLException e) {
e.printStackTrace();
}
This is how i instantiate my driver. I have the chrome plugin on jenkins as well.
When i try to build, i get this error:
org.openqa.selenium.remote.UnreachableBrowserException: Could not
start a new session. Possible causes are invalid address of the remote
server or browser start-up failure.
....
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to
localhost:4444 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1]
failed: Connection refused
Any idea what am i doing wrong?
If you are running locally try switching to chromedriver
DesiredCapabilities capability = DesiredCapabilities.chrome();
try {
baseUtil.driver = new ChromeDriver(capability));
} catch (MalformedURLException e) { e.printStackTrace(); }
the URL in your previous call was set to point to the hub which you aren't using.
you might need to point it at the local driver location such as:
System.setProperty("webdriver.chrome.driver", "<pathtoyourchromedriver>/chromedriver.exe");

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)
{
}
}

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

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();
}

selenium grid, creating node programmatically

I have to create a java application that will start a node and connect it to the hub. So far I have been able to do so when the hub and node are on the same computer, but as soon as I try to connect on another machine hub, the registering process hang forever.
I tried different approach. To just call my bat file function in code.
String command = "java -jar selenium-server-standalone-2.26.0.jar -role node -hub http://192.168.0.11:4444/grid/register -port 4449 -Dwebdriver.chrome.driver=data\\driver\\chromedriver.exe -Dwebdriver.ie.driver=data\\driver\\IEDriverServer.exe -nodeConfig data\\configurations.json";
try
{
pr = Runtime.getRuntime().exec(command);
}
catch(Exception e)
{
e.printStackTrace();
}
The command work when called from a bat file, but in code it only works if the node and hub are on the same computer.
I also tried to use the RegistrationRequest.
RegistrationRequest req = new RegistrationRequest();
req.setRole(GridRole.NODE);
Map<String, Object> nodeConfiguration = new HashMap<String,
Object>();
nodeConfiguration.put(RegistrationRequest.AUTO_REGISTER, true);
nodeConfiguration.put(RegistrationRequest.HUB_HOST, "192.168.100.66");
nodeConfiguration.put(RegistrationRequest.HUB_PORT, 4444);
nodeConfiguration.put(RegistrationRequest.PORT, 5555);
URL remoteURL = new URL("http://" + "192.168.100.66" + ":" + 5555);
nodeConfiguration.put(RegistrationRequest.PROXY_CLASS, "org.openqa.grid.selenium.proxy.DefaultRemoteProxy");
nodeConfiguration.put(RegistrationRequest.MAX_SESSION, 1);
nodeConfiguration.put(RegistrationRequest.CLEAN_UP_CYCLE, 2000);
nodeConfiguration.put(RegistrationRequest.REMOTE_HOST, remoteURL);
nodeConfiguration.put(RegistrationRequest.MAX_INSTANCES, 1);
req.setConfiguration(nodeConfiguration);
remote = new SelfRegisteringRemote(req);
remote.startRemoteServer();
remote.startRegistrationProcess();
Same result, when I try to run on another computer hub, it hand at the registering process.
INFO - Registering the node to hub :http://192.168.100.66:4444/grid/register
any idea why? or how to do it.
I figured out my problem, which is really simple to fix. In my code I had
URL remoteURL = new URL("http://" + "192.168.100.66" + ":" + 5555);
I just needed to replace the ip address by my local ip address, not the hub ip address, and it worked. Which is weird cause I am pretty sure I took this code from somewhere online and he had a variable for the ip, and it was the same for remoteURL and the HUB_HOST
well. Not sure whether is suitable for you but I'd like to share approach I use on my project.
I've got remote machine with 192.168.4.52 IP and selenium stanadlone server running on it.
All selenium test suites I got locally.
So to run my selenium test suite on remote machine I simply use these settings in BaseSeleniumTest.java on my local machine:
....
#BeforeClass
public static void firefoxSetUp() throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), capability);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1920, 1080));
}
#Before
public void homePageRefresh() throws IOException {
driver.manage().deleteAllCookies();
driver.get(propertyKeysLoader("login.base.url"));
}
#AfterClass
public static void closeFirefox(){
driver.quit();
}
where string
driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), capability);
indicates IP of machine which I want to run my selenium test siute on.
I'm starting server on remote machine with this command:
java -jar selenium-server-standalone-2.26.0.jar in cmd before I run my test suite.
Hope it be helpful for you.