I know this question is asked many times.But i didnt get what i want.
I need to automate quick3270 which is used to connect to mainframe using java.
First let me tell you what i want.
I need my code to open quick3270.exe then open my saved session:---this is done.
Now, I have to send commands to the quick3270.Here comes the problem, I dont know how to send command to that software.
Third is I am using robot class.So that i can input:TAB,ENTER,F3 etc. inputs.
So, the whole thing is I want to send commands to quick3270. I need interval also.Like send one command then delay of 1 second then other and so on.
public static void main(String[] args) throws IOException, AWTException {
String exeloc = "C:\\Program Files\\Quick3270\\Quick3270.exe ";
// my saved session
String directory = "C:\\Users\\c111128\\Desktop\\Project\\xyz.ecf";
ProcessBuilder builder = new ProcessBuilder(new String[] { exeloc, directory });
// Starting the process
Process p = builder.start();
// For handling keyboard events
Robot robot = new Robot();
try {
robot.delay(2000);
// Passing enter key to top screen
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(4000);
// Here I want to write the command
//Command like:"teleview" which is used in mainframe
robot.delay(1000);
}
catch (Exception e) {
System.out.println("Second:" + e);
e.printStackTrace();
}
}
did you manage the Problem?
Via VBA you can send commands to Quick3270 this way:
Set Session = .ActiveSession
Set Screen = Session.Screen
Screen.SendKeys ("<Enter>")
Result = Screen.WaitForKbdUnlock
Screen.SendKeys ("<PF12>")
Screen.SendKeys ("<Enter>")
Result = Screen.WaitForKbdUnlock
Screen.SendKeys ("<PF12>")
Result = Screen.WaitForKbdUnlock
Result = Screen.WaitForCursor(4, 15)
QuickPutstring "1", 10, 2
Private Function QuickPutstring(ByVal PutstringText As String, Row As Long, Col As Long)
Screen.MoveTo Row, Col
Screen.Putstring PutstringText
End Function
Hope that helps...
Related
I am working on a Spring-MVC webapplication in which we are trying to get a screenshot of an URL. Currently I am using PhantomJS for that task, but it's too slow(>10seconds). Also, the URL's have to be with http/https and www for detecting that it's an URL. As this is a chat application, there can be simple URL's which users add like helloworld.com. Any help would be nice. Thank you.
Code:
String[] words = message.split(" ");
for( String item : words ){
boolean val = ResourceUtils.isUrl(item);
if(val){
urlIdentifier = calcUrl(item);
break;
}else {
System.out.println("Url false is "+item);
}
}
if(urlIdentifier!=null){
replies.setPreviewIdentifier(urlIdentifier);
input.put("preview_identifier",urlIdentifier);
}
Method to calculate screenshot :
private String calcUrl(String website){
try {
String identifier = String.valueOf(new BigInteger(130, random).toString(32));
String previewLocation = msg + "chatthumbs/" + identifier ;
Process proc = Runtime.getRuntime().exec("phantomjs --ssl-protocol=any " +
"/home/deploy/phantom/rasterizepdf.js " +" "+website+" " +previewLocation);
proc.waitFor();
BufferedImage image = ImageIO.read(new File("/home/akshay/testme.png"));
if(image!=null){
if (image.getWidth() > image.getHeight()) {
image = Scalr.resize(image, Scalr.Mode.FIT_TO_HEIGHT, 250, 250);
} else {
image = Scalr.resize(image, Scalr.Mode.FIT_TO_WIDTH, 250, 250);
}
image = Scalr.crop(image, 250, 250);
ImageIO.write(image, "png", new File(previewLocation));
}
return identifier;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
Any help would be nice. Thank you.
(a) I think the process of taking a screen shot is taking time. Is this code running on the same device as the chat screen? Why not use java.awt.Robot to take the screen shot ? or just save the text why do you need a screen shot?
(b) Is the system too busy/ Use on a SSD to see if faster?
(c) But curious as to the end application, is this part of a web app? How will your code run on the client systems? Or will you java agent be installed on all user systems that monitors the web page and takes screen shots? Then why use a web page, use a java app to chat, and parse the text as typed.
Parsing the text. What if user types/ pastes a long message? Are you parsing everything once or on change? Think of ways to improve that if it seems to be a problem. Ignore if not the immediate issue now.
Also if the msg is too long the parsing can take a lot of time. maybe process after every key press or change event (if paste) keep local js copy of previous text to get diff?
I have created an automation program using java selenium.I have used TestNG framework.
I want to record (video) of the screen those are getting executed during the script execution so it is better to track the failed/passed scenario and view the execution process.
Can any one help me with this, how to record the screen during running the automation suite execution.
Problems with solution mentioned before :-
All solutions answered to record video, records test execution from start to end. If automation suite run for hours then this won't be practical and optimal solution.
Main purpose of record video is to SEE what exactly happened when automation test case failed. So precisely testers need video recording of LAST 15 SECONDS BEFORE TEST CASE FAILED. They don't need any recording for PASSED test cases
Solution in theory :-
On Windows 10 onwards, Windows Xbox Game bar [Windows+G] has ability to capture LAST 15 seconds [customizable] of video. Keyboard shortcut Windows+Alt+G is use to capture last 15 seconds of video using XBox Game Bar and it would be stored in folder mentioned in settings.
Selenium automation can exploit this recording feature of Windows Xbox Game bar.
In your testNG automation project, in onTestFailure method of testNG listener just add code to keypress Windows+Alt+G to capture last 15 seconds video. This would capture video for ONLY failed test cases and never for PASS test cases. If you are using Java then you can use Robot library to send keypress programatically.
Screenshots showing Windows XBox game Bar and it's setting to capture last 15 seconds.
Solution in Code :-
I am calling below recordFailedTCVideo() method from testNG listner's
public void onTestFailure(ITestResult result) method.
This will just record last 15 seconds of video ONLY for failed test cases.[and not for PASS test cases]
Video explanation :- https://www.youtube.com/watch?v=p6tJ1fVaRxw
public void recordFailedTCVideo(ITestResult result) {
//private void pressKey() {
System.out.println("In recordFailedTCVideo::***In Try Block *** Video for test case failed " + result.getName());
commonUtility.logger.error("BaseTest::recordFailedTCVideo::***In Try Block *** Video for test case failed " + result.getName());
try {
// Useing Robot class to keypres Win+Alt+G which will capture last 15 seconds of video
Robot r = new Robot();
r.keyPress(KeyEvent.VK_WINDOWS );
Thread.sleep(1000);
r.keyPress(KeyEvent.VK_ALT );
Thread.sleep(1000);
r.keyPress(KeyEvent.VK_G );
Thread.sleep(5000);
r.keyRelease(KeyEvent.VK_WINDOWS);
Thread.sleep(1000);
r.keyRelease(KeyEvent.VK_ALT);
Thread.sleep(1000);
r.keyRelease(KeyEvent.VK_G);
Thread.sleep(5000);
/// Copy Video saved to desired location
File srcDir = new File(commonUtility.prop.getProperty("VIDEO_CAPTURE_DEFAULT_LOCATION"));
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd HHmmss");
LocalDateTime now = LocalDateTime.now();
String destination = ".\\ScreenshotsAndVideos\\" + dtf.format(now) ;
File destDir = new File(destination);
try {
System.out.println("In RecordFailedTCVideo::Source Folder is "+ srcDir +" Destination Folder = " + destDir);
commonUtility.logger.error("In RecordFailedTCVideo::Source Folder is "+ srcDir +" Destination Folder = " + destDir);
FileUtils.moveDirectory(srcDir, destDir);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("In recordFailedTCVideo::***In Catch Block ***\n" +e);
commonUtility.logger.error("BaseTest::recordFailedTCVideo::***In Catch Block *** \n"+e );
e.printStackTrace();
}
//}
}
Further Video explanation :-
https://www.youtube.com/watch?v=p6tJ1fVaRxw
Constraints :-
This solution is not for non-Windows platforms.
XBar Game utility would not record Windows Explorer , text files etc. Although it records browsers without problem.
See this API (Monte Library): http://www.seleniummonster.com/boost-up-your-selenium-tests-with-video-recording-capability/
and this link: http://unmesh.me/2012/01/13/recording-screencast-of-selenium-tests-in-java/
Example Code (from above links):
public void startRecording() throws Exception
{
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
this.screenRecorder = new ScreenRecorder(gc,
new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI),
new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,DepthKey, 24, FrameRateKey, Rational.valueOf(15),QualityKey, 1.0f,KeyFrameIntervalKey, 15 * 60),new Format(MediaTypeKey,MediaType.VIDEO, EncodingKey, "black",FrameRateKey, Rational.valueOf(30)),null);
this.screenRecorder.start();
}
public void stopRecording() throws Exception
{
this.screenRecorder.stop();
}
I've been bashing my head in on this problem for a few days now. I've done my full share of Googling, and I'm hoping I can find someone here that is more experienced than I (not hard to find haha) that can decipher my problem.
Scenario: I developed a Java Applet that implements a Swing GUI. Background work: The Applet gathers records from a large "phone book" excel file (.csv) and stores them in a Map data structure. The phone book contains about 106,000 records, and on the 34,586th record, I get an ArrayIndexOutOfBoundsException that I cannot make sense of. The exception only occurs when I'm running the applet on my personal website. The applet runs perfectly fine with no errors while testing in my IDE (NetBeans) and running the .html file (the one that contains the applet) on my local machine. The output and exception that is thrown while running on my website is as follows (I cut most of the records to save space):
Java Console
Kary,Webber,2826 East 12th Ave.,Memphis,TN,38168,901-749-1834
Erinn,Rocha,2132 East Main Ave.,Memphis,TN,38168,865-414-5105
Gina,Lane,71 South First St. Apt. 11,Memphis,TN,38168,731-485-1129
Patsy,Hart,661 East 11th St.
java.lang.ArrayIndexOutOfBoundsException: 3
at Implementation.PersonnelDatabase.addRecordFromFields(PersonnelDatabase.java:192)
at Implementation.PersonnelDatabase.initDBFromFile(PersonnelDatabase.java:215)
at Implementation.PersonnelDatabase.processData(PersonnelDatabase.java:239)
at Implementation.PersonnelDatabaseApplet$2.doInBackground(PersonnelDatabaseApplet.java:78)
at Implementation.PersonnelDatabaseApplet$2.doInBackground(PersonnelDatabaseApplet.java:69)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
As you can see, on the 34,586th record (beginning with Patsy,Hart), it outputs midway through her address. The full record is as follows: Patsy,Hart,661 East 11th St. Apt. 195,Memphis,TN,38168,555-555-5555 .
Below are the sections of code that are most logically influenced by the exception.
Object tag in HTML file
<object type="application/x-java-applet" height="400" width="300">
<param name="codebase" value="classes" />
<param name="code" value="Implementation/PersonnelDatabaseApplet.class" />
<param name="archive" value="PersonnelDatabase.jar" />
Applet failed to run. No Java plug-in was found.
</object>
PersonnelDatabase class (processes background data):
/*
* Create a new record using an appropriately ordered set of fields and add it to the data base
*/
public void addRecordFromFields(String[] fields)
{
// Read record attributes in, one at a time
Record thisRecord = new Record();
thisRecord.setFirstName(fields[0]);
thisRecord.setLastName(fields[1]);
thisRecord.setAddress(fields[2]);
thisRecord.setCity(fields[3]);
thisRecord.setState(fields[4]);
thisRecord.setZipCode(fields[5]);
thisRecord.setPhoneNo(fields[6]);
addRecord(thisRecord);
}
// O( n )
/**
* Destroy the current data base and load new data from a file.
* #param filename the file to use as a source
* #throws IOException: Either file not found or IO error
*/
public void initDBFromFile(URL url) throws IOException
{
// Open and read the file
InputStream in = url.openStream();
BufferedReader filein = new BufferedReader(new InputStreamReader(in));
// Read record file, parse lines, and add records to data base
String line = filein.readLine();
while(line != null) {
System.err.println(line);
String[] fields = line.split(",");
addRecordFromFields(fields);
line = filein.readLine();
}
filein.close();
}
/**
* Loads the default library and provides for interaction with the data
* via the JPanel GUI inputs.
* #param args
* #throws IOException
*/
public String processData(String input, int selection, URL url)
{
//Create the main library object
PersonnelDatabase dbiLib = new PersonnelDatabase();
System.err.println(url);
// Try to read the default library
try
{
dbiLib.initDBFromFile(url);
}
catch (IOException e)
{
System.err.println("File IO Error");
e.printStackTrace();
System.exit(1);
}
// Queries can be simulated by typing into the console in Eclipse, and using Ctrl-d (Ctrl-z in Windows) when finished.
// For example: "searchLastName,Smith" would print a list of all people with the last name of Smith.
Iterable<Record> result = null;
String[] fields = new String[2];
if (input.contains(",")) {
fields = input.split(",");
}
switch(selection) {
case 0: result = dbiLib.searchByFirstName(input); break;
case 1: result = dbiLib.searchByLastName(input); break;
case 2: result = dbiLib.searchByFullName(fields[0].trim(), fields[1].trim()); break;
case 3: result = dbiLib.searchByCity(input); break;
case 4: result = dbiLib.searchByState(input); break;
case 5: result = dbiLib.searchByCityState(fields[0].trim(), fields[1].trim()); break;
case 6: result = dbiLib.searchByZip(input); break;
case 7: result = dbiLib.searchByPhoneNumber(input); break;
case 8: String[] newFields = new String[fields.length-1];
System.arraycopy(fields, 1, newFields, 0, fields.length-1);
dbiLib.addRecordFromFields(newFields);
return "Record added successfully!\nEnter a query or add another record.";
default: return "Invalid query.\nEnter another query or add a record.";
}
PersonnelDatabaseApplet class (initializes GUI, gathers input, and displays output):
public void init() {
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
#Override
public void run() {
initComponents();
}
});
} catch (Exception e) {
System.err.println("Creation of GUI did not successfully complete.");
}
}
// Process inputs in the background.
SwingWorker worker = new SwingWorker<String, Void>() {
#Override
public String doInBackground() {
URL url = null;
try {
url = new URL(getCodeBase(), fileToRead);
}
catch(MalformedURLException e){}
personnelDatabase = new PersonnelDatabase();
final String output = personnelDatabase.processData(input, selection, url);
return output;
}
#Override
public void done() {
processingLabel.setVisible(true);
try {
textToDisplay = get(15, TimeUnit.SECONDS);
} catch (InterruptedException ignore) {
ignore.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (ExecutionException e) {
String why = null;
Throwable cause = e.getCause();
if(cause != null) {
why = cause.getMessage();
cause.printStackTrace();
} else {
why = e.getMessage();
e.printStackTrace();
}
System.err.println("Error retrieving request: " + why);
}
if(worker.isDone() && textToDisplay != null) {
processingLabel.setVisible(false);
outputTextArea.setText(textToDisplay);
}
}
};
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
selection = searchComboBox.getSelectedIndex();
input = valueTextField.getText();
processingLabel.setVisible(true);
worker.execute();
}
Link to the Applet on my personal website: http://www.ryan-taylor.me/Applied%20Maps/build/PersonnelDatabaseApplet.html
I'm fairly certain the error has nothing to do with the excel data itself as this program runs just fine in NetBeans and when running the html on my local machine. I'm guessing it has something to do with Swing (threads), but I'm not sure. I made changes to help with transferring data between Swing threads by making use of the SwingWorker, but I had no luck. I guess there's always the possibility I missed something when implementing it.
I also thought about signing the jar, but the file that I'm processing is stored online - not a local machine - so I didn't see a real need.
If anyone has any suggestions, I would greatly appreciate it!
Looks like the array index that's out of bounds is 3, because the input line contains only three fields and you're trying to access the fourth one (index 3) without checking to verify that it actually exists. The error is in
thisRecord.setCity(fields[3]);
because the array fields has only three elements. At
String[] fields = line.split(",");
addRecordFromFields(fields);
when you get to
Patsy,Hart,661 East 11th St.
array fields will be created with only 3 entries.
If the number of fields is expected to be constant then you should reject input lines that don't have the right number of fields. If the number of fields can vary, then you must check the actual number returned and extract only those elements that actually exist.
It looks like something is causing the file to be truncated when you run the applet in a browser. My guess is that you are fetching the file from a web server, and either the server or the browser is silently enforcing some download limit. (Or may be the file got truncated when you uploaded it ...)
I suspect a data race between your worker and another thread that is exposed by network latency. Two things may bear closer scrutiny:
Using invokeAndWait() to initialize the GUI is reasonable, as long as no further processing is done on the initial thread.
Your worker is unusual in not invoking publish(), as a way to process() records on the event dispatch thread, for example.
I have a file that works just fine if I use the command lp filename.
The file is an ESC/P file for a receipt impact printer. That has linux native CUPS drivers and all that works.
Im trying to use the javax.print API so that I can have a finer grained control over the printing and hopefully keep it cross-platform compatible, though Linux is the target platform.
I've tried every DocFlavor combination known to man and every type of data type (InputStream, byte[], Reader etc.)
It either just ignores the print() command all together or just flips out a blank sheet of paper. Running lp filename prints it perfect, so how do I get javax.print to just do the functional equivalent of lp filename?
I'm not set on using javax.print I can use other "things" and might start investigating cups4J but it seems it would limit me to Linux/*nix only, which is OK for now but would rather have a cross platform solution.
I could just issue the lp system command on the file but, I need finer grained control. These aren't receipts we're printing, they are tickets and the tickets range from $5.00 to to thousands of dollars. Currently if we detect a printing issue, we void the transaction and if anything printed, its invalid, we don't take reprints lightly and most of the time charge to print a new copy if the customer looses his copy. Oh the reason for doing this is we're changing the POS system from Windows to Linux and the printers from direct access over serial ports to CUPS managed over USB. Here's my code that doesn't work. Any help is appreciated.
try {
// Find the default service
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// Create the print job
DocPrintJob job = service.createPrintJob();
InputStream in = new FileInputStream("/home/bart/real.escp");
Doc docNew = new SimpleDoc(in,flavor,null);
// Monitor print job events; for the implementation of PrintJobWatcher,
// see Determining When a Print Job Has Finished
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it
job.print(docNew, null);
// Wait for the print job to be done
pjDone.waitForDone();
// It is now safe to close the input stream
in.close();
} catch (PrintException e) {
} catch (IOException e) {
}
I am fine with cups4j.
First get your printer.
try {
CupsClient client = new CupsClient("addressOfTheCupsServer", 631);
List<CupsPrinter> printers = client.getPrinters();
if (printers.size() == 0) {
throw new RuntimeException("Cant list Printer");
}
for (CupsPrinter cupsPrinter : printers) {
if (cupsPrinter.getName().equals("NameOfPrinter")) {
selectedPrinter = cupsPrinter;
}
}
}catch (Exception ignored){
ignored.printStackTrace();
}
}
Then create a printjob and send it to the printer
PrintJob printJob = new PrintJob.Builder(inputStream).jobName("Jobname").build();
PrintRequestResult result = selectedPrinter.print(printJob);
We are using the new Java printing API which uses PrinterJob.printDialog(attributes) to display the dialog to the user.
Wanting to save the user's settings for the next time, I wanted to do this:
PrintRequestAttributeSet attributes = loadAttributesFromPreferences();
if (printJob.printDialog(attributes)) {
// print, and then...
saveAttributesToPreferences(attributes);
}
However, what I found by doing this is that sometimes (I haven't figured out how, yet) the attributes get some bad data inside, and then when you print, you get a white page of nothing. Then the code saves the poisoned settings into the preferences, and all subsequent print runs get poisoned settings too. Additionally, the entire point of the exercise, making the settings for the new run the same as the user chose for the previous run, is defeated, because the new dialog does not appear to use the old settings.
So I would like to know if there is a proper way to do this. Surely Sun didn't intend that users have to select the printer, page size, orientation and margin settings every time the application starts up.
Edit to show the implementation of the storage methods:
private PrintRequestAttributeSet loadAttributesFromPreferences()
{
PrintRequestAttributeSet attributes = null;
byte[] marshaledAttributes = preferences.getByteArray(PRINT_REQUEST_ATTRIBUTES_KEY, null);
if (marshaledAttributes != null)
{
try
{
#SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"})
ObjectInput objectInput = new ObjectInputStream(new ByteArrayInputStream(marshaledAttributes));
attributes = (PrintRequestAttributeSet) objectInput.readObject();
}
catch (IOException e)
{
// Can occur due to invalid object data e.g. InvalidClassException, StreamCorruptedException
Logger.getLogger(getClass()).warn("Error trying to read print attributes from preferences", e);
}
catch (ClassNotFoundException e)
{
Logger.getLogger(getClass()).warn("Class not found trying to read print attributes from preferences", e);
}
}
if (attributes == null)
{
attributes = new HashPrintRequestAttributeSet();
}
return attributes;
}
private void saveAttributesToPreferences(PrintRequestAttributeSet attributes)
{
ByteArrayOutputStream storage = new ByteArrayOutputStream();
try
{
ObjectOutput objectOutput = new ObjectOutputStream(storage);
try
{
objectOutput.writeObject(attributes);
}
finally
{
objectOutput.close(); // side-effect of flushing the underlying stream
}
}
catch (IOException e)
{
throw new IllegalStateException("I/O error writing to a stream going to a byte array", e);
}
preferences.putByteArray(PRINT_REQUEST_ATTRIBUTES_KEY, storage.toByteArray());
}
Edit: Okay, it seems like the reason it isn't remembering the printer is that it isn't in the PrintRequestAttributeSet at all. Indeed, the margins and page sizes are remembered, at least until the settings get poisoned at random. But the printer chosen by the user is not here:
[0] = {java.util.HashMap$Entry#9494} class javax.print.attribute.standard.Media -> na-letter
[1] = {java.util.HashMap$Entry#9501} class javax.print.attribute.standard.Copies -> 1
[2] = {java.util.HashMap$Entry#9510} class javax.print.attribute.standard.MediaPrintableArea -> (10.0,10.0)->(195.9,259.4)mm
[3] = {java.util.HashMap$Entry#9519} class javax.print.attribute.standard.OrientationRequested -> portrait
It appears that what you're looking for is the PrintServiceAttributeSet, rather than the PrintRequestAttributeSet.
Take a look at the PrintServiceAttribute interface, and see if the elements you need have been implemented as classes. If not, you can implement your own PrintServiceAttribute class(es).