There is a focus on the field , in the browser , or notepad . When I plan to offer NFC, I want to force to insert text into the focused field . I know how to read NFC tag and add it to the clipboard . But how to make paste this text into an external application for the selected field ? Unfortunately I have not found how to do it using the clipboard manager. Tell me in what direction I move? Maybe using the keyboard ?
I'd suggest emulating keypresses.
new Thread(new Runnable() {
#Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
for ( int i = 0; i < 10; ++i ) {
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
Thread.sleep(2000);
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
Thread.sleep(2000);
}
}
catch(InterruptedException e){
}
}
}).start();
Android simulate key press
There's probably a way for you to emulate ctrl+v, which, despite not being on the default keyboards, does work on most devices.
Related
Hello I want to learn how to switch in in to a new windows without using thread sleep. I was trying to use awaitility artifact but I was not able to done it correctly. I was trying to automate print window. When I click on print icons on my web page I navigate to print window I want to wait while navigating to print window and once print window displayed I want to click on cancel button. Can someone help me for that
Print_icon.click();
await().atMost(10,TimeUnit.SECOND).pollInterval(1,TimeUnit.SECONDS);
Cancel_button.click();
You can try this :
Print_icon.click();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
boolean elmnt = false;
boolean timeOut = false;
int second = 1;
do {
try {
if(second>30) {
timeOut = true;
}
Cancel_button.click();
elmnt=true;
} catch (Exception e) {
TimeUnit.SECONDS.sleep(1);
second++;
}
}while(elmnt==false && timeOut==false);
You can't handle windows dialogs so I suggest you review your page code and find the name of the method that opens the print window and override it. In your test case for example you can override print method to do nothing, like this:
((JavascriptExecutor)driver).executeScript("window.print=function(){};");
I have to create a transfer. When I run the script the 'Transfer' button for which 'enabled' is false therefore script not able to tap 'Transfer' button & fails. I have attached a screen shot of uiautomator viewer dump.
The workaround I found is to manually click on amount edit box and then this enables the on-screen android keyboard and by entering value manually for 'amount' field & then 'Transfer' button got enabled & can be clicked. But I am not sure how to enter value in edit box from on-screen android keyboard and then get rid of this keyboard to enter date & press 'Transfer' button.
Your help is much appreciated. Thanks.
First set following capabilities:
capabilities.setCapability("unicodeKeyboard", true);
capabilities.setCapability("resetKeyboard", true);
second try to hide keyboard on following way:
driver.hideKeyboard(); // doesn't work on newer versions of appium
or try this:
driver.pressKeyCode(AndroidKeyCode.BACK); //this also doesn't work on all devices but give it a try
For date control I'm not quite sure which control You're using date-picker or some custom date control, but on each action try to hide keyboard.
Hi you can hide the soft keyboard by using following code snippet. It works for me.
public void someMethod(){
driver.getKeyboard();
try {
if (checkSoftKeyboard())
driver.hideKeyboard();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean checkSoftKeyboard() throws IOException {
boolean isKeyboardPresent = false;
Process p = Runtime.getRuntime().exec("adb shell dumpsys input_method | grep mInputShown");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String outputText = "";
while ((outputText = in.readLine()) != null) {
if(!outputText.trim().equals("")){
String keyboardProperties[]=outputText.split(" ");
String keyValue[]=keyboardProperties[keyboardProperties.length-1].split("=");
String softkeyboardpresenseValue=keyValue[keyValue.length-1];
if(softkeyboardpresenseValue.equalsIgnoreCase("false")){
isKeyboardPresent=false;
}else{
isKeyboardPresent=true;
}
}
}
in.close();
return isKeyboardPresent;
}
The checkSoftKeyboard method will check if the soft keyboard is already there or not? If it is there it will simply hide the soft keyboard. And then you will be able to see the Transfer button.
hope this works for you. Thanks!
I am able to solve this problem. My approach is to first click on 'Amount' field and then do sendkeys the value of amount. For details see below code:-
//locating the amount field using xpath
MobileElement amount = driver.findElement(By.xpath("//android.widget.EditText[#resource-id='com.abc.rbanking:id/workflow_step_amount_value']"));
amount.click();
amount.sendKeys("1.25");
//clicking and sendkeys would enable the disabled 'Transfer' button
//locating the 'Date' field and click it. Clicking it would get rid of soft android keyboard
driver.findElement(By.xpath("//*[#text = 'Date']")).click();
Thread.sleep(3000);
driver.findElement(By.id("com.abc.rbanking:id/back_button")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.Button[#resource-id='com.abc.rbanking:id/PrimaryButton' and #text='Transfer']")).click();
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 am writing my own Netbeans plugin to edit opened files. I have managed to get some information about currently active file using
TopComponent activeTC = TopComponent.getRegistry().getActivated();
FileObject fo = activeTC.getLookup().lookup(FileObject.class);
io.getOut().println(fo.getNameExt());
io.getOut().println(fo.canWrite());
io.getOut().println(fo.asText());
But I have no idea how to modify this file. Can someone help me with this?
And second question, how to get text selection ranges? I want to run my command only on selected text.
For modifying the file you could use the NetBeans org.openide.filesystems.FileUtil.toFile() and then the regular Java stuff to read and write files and for getting the selected text of the current editor window you would have to do something like:
Node[] arr = activeTC.getActivatedNodes();
for (int j = 0; j < arr.length; j++) {
EditorCookie ec = (EditorCookie) arr[j].getCookie(EditorCookie.class);
if (ec != null) {
JEditorPane[] panes = ec.getOpenedPanes();
if (panes != null) {
// USE panes
}
}
}
For more code examples see also here
After several hours of research I found out that:
The code I posted in Question can be used to obtain basic information about active file.
To get caret position or get selection range you can do:
JTextComponent editor = EditorRegistry.lastFocusedComponent();
io.getOut().println("Caret pos: "+ editor.getCaretPosition());
io.getOut().println("Selection start: "+ editor.getSelectionStart());
io.getOut().println("Selection end: "+ editor.getSelectionEnd());
To modify content of active file (in a way that the modification can be undo by Ctrl+z) you may use this code:
final StyledDocument doc = context.openDocument();
NbDocument.runAtomicAsUser(doc, new Runnable() {
public void run() {
try {
doc.insertString(ofset, "New text.", SimpleAttributeSet.EMPTY);
} catch (Exception e) {
}
}
});
I made a code to delete the adobe directories from an user profile, I use it remotely conecting to remote computers. In this code when a file it's deleted an textArea must show the rute of the deleted file. In a System.out.println the rute it runs but it doesn't change the textArea until the recursive function ends.
I have this code. (Sorry for the rudimentary translate to English)
private void RecursiveDel(String rute) {
File tdel = new File(rute);
if (tdel.isDirectory()) {
for (File del : tdel.listFiles()) {
RecursiveDel(del.getAbsolutePath());
}
}
txtInform += "Removing: " + ruta + "\r\n";
ActRes();
tdel.delete();
System.out.println(rute);
if (tdel.exists()) {
txtInforme += "File isn't deleted: \r\n" + ruta + "\r\n";
ActRes();
Correct = false;
}
}
private void ActRes(){
Thread act = new Thread(new Runnable() {
#Override
public void run() {
txtResult.setText(txtInforme);
}
});
act.start();
}
How I can do show the deleted Files into the TextArea meanwile the recursive function works?
it runs but it doesn't change the textArea until the recursive function ends.
Correct, because your code is looping through all the directories and building a string rather than trying to update the text area for each directory.
Instead you should be using a SwingWorker and "publishing" the directory as you find it. Then every time you publish a value the text area can be updated.
Read the section from the Swing tutorial on Tasks That Have Interim Results for an example of this approach.