I have an issue with windowlicker on OS X ( everything works ok on Windows ).
The issue is that when I try to simulate user input to any text field the data is not inserted properly ( some letters are cut out ).
For instance:
JTextFieldDriver txField = new JTextFieldDriver(this,
JTextField.class,
named(fieldName));
txField.focusWithMouse();
txField.typeText(input);
Preceding code will result in that I will observe windowlicker inserting input to a text field named fieldName and the input will be incomplete ( Peter will be Peer or Fred will be Fre and so on ). Everything works properly on windows.
I am not sure if all that has anything to do with a warning. I get similar on windows. The warning is:
"WARNING: could not load keyboard layout Mac-, using fallback layout with reduced capabilities (JAR entry com/objogate/wl/keyboard/Mac- not found in /Users/odo/.m2/repository/com/googlecode/windowlicker/windowlicker-core/r268/windowlicker-core-r268.jar)"
Windowlicker seems not to be very popular tool. Nevertheless I managed to figure out the root cause. The warning stating that keyboard layout can't be set is displayed because I am not using english locale. It looks like windowlicker supports only Mac-GB keyboard layout.
The warning will go away if an appropriate system property is set.
For instance:
System.setProperty("com.objogate.wl.keyboard", "Mac-GB");
However this will not solve the main problem. After couple trials I figured out that only 'a' and 'd' characters are trimmed. This is because windowlicker inserts them as if user would hold the 'a' or 'd' key for a bit. Holding those keys results in a helper menu invocation which allows to select special characters. In order to fix that I played with the JTextComponentDriver and found a workaround. The solution is not to use driver's typeText to insert text. JTextComponentDriver's component() method can be used to retrieve the actual guy component and then having an instance setText() can be invoked to set the text.
Below I present my helper class which is using described solution:
public class TextTyper {
private final String inputText;
privte TextTyper(String inputText) {
this.inputText = inputText;
}
public static TextTyper typeText( final String inputText ){
return new TextTyper( inputText );
}
public void into( JTextComponentDriver<?> driver ) throws Exception{
driver.focusWithMouse();
driver.clearText();
Component cmp = driver.component().component();
if(cmp instanceof JPasswordField ){
JPasswordField pwField = (JPasswordField) cmp;
pwField.setText(this.inputText);
}
else if( cmp instanceof JTextField){
JTextField txField = (JTextField) cmp;
txField.setText(this.inputText);
}
else
throw new Exception("Component is not an instance of JTextField or JPasswordField");
}
}
Related
I need to create a program to store all words in an array list. Then check the user input from the textfield to see if it starts with anything other than numbers and punctuation. Otherwise it will need to display an error and prvent the string to be added to the arraylist and display an appropriate error.
https://pastebin.com/8UwDm4nE
Heres the ActionEvent listener that contins the code to check that. Im not really sure how to get it working.
#Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 1; i++) {
String str = tf.getText(); // MUST BE STORED ON AN ARRAY LIST
ta.append(str + "\n"); // Append the text on new line each
if(str.startsWith(String.valueOf(nums))) { // Check input for a number at the start
error.setText("Error: Word starts a number. Please try again!");
error.setForeground(Color.RED);
ta.append("");
} else if (str.startsWith(String.valueOf(punct))) { // Check if input contains a punctuation at the start
error.setText("Error: Word starts with an illegal character. Please try again!");
error.setForeground(Color.RED);
ta.append("");
}
}
}
I'm going to rephrase your problem a bit as clarification, please correct me if I'm misunderstanding.
You have a text field and a text area. You want a user to type a word into the text field and submit it. If that word starts with a number or punctuation, then indicate an error to the user. Otherwise, add it to the text area (on a new line) and the inner ArrayList.
To solve this problem, there are a couple things you'll need:
An ArrayList<String> that is a class member variable where you can store your words
An event handler that handles the button click.
The event handler should:
Parse the string from the text field (using getText(), as you already are).
Do the error checks you're already doing.
If neither of the error conditions are hit (so add an else clause for this), add the word to the text area (which you're already doing) and add it to the ArrayList.
Hopefully this helps you get a clearer idea of how to approach the problem. If not, please post a code sample of what you tried and what error you're specifically running into.
EDIT:
Here is some pseudocode for your if-else error-handling block of code, assuming you declare a new ArrayList to hold your words as a class member:
// as class member variable
List<String> wordList = new ArrayList<>();
// word handler code
if (str starts with a number) {
// handle error
} else if (str starts with punctuation) {
// handle error
} else {
ta.append(str + "\n");
wordList.add(str);
}
I'm using Java, Selenium, and Chrome for test automation. Our developers recently upgraded our UI from AngularJS to Angular2 (not sure if that matters). But since then, sendKeys is inputting incomplete characters in to the text field. Here's an example:
public void enterCustomerDetails()
{
txtFirstName.sendKeys("Joh201605130947AM");
txtSurname.sendKeys("Doe201605130947AM");
txtEmail.sendKeys("johndoe#gmail.com");
}
I also tried using executeScript. It didn't work. It can enter complete characters but the form thinks the field is null.
public void forceSendKeys(WebElement element, String text)
{
if (element != null)
((JavascriptExecutor) this.webDriver).executeScript("arguments[0].value=arguments[1]", element, text);
}
public void enterCustomerDetails()
{
forceSendKeys(txtFirstName, "Joh201605130947AM");
forceSendKeys(txtSurname, "Doe201605130947AM");
forceSendKeys(txtEmail, "johndoe#gmail.com");
}
I also tried using .click() before .sendKeys and adding in sleep time. They didn't work too.
I got an idea to enter the characters 1 by 1 from this post: How to enter characters one by one in to a text field in selenium webdriver?
It worked but that means I have to rewrite all my codes from sendKeys to the new function:
public void sendChar(WebElement element, String value)
{
element.clear();
for (int i = 0; i < value.length(); i++){
char c = value.charAt(i);
String s = new StringBuilder().append(c).toString();
element.sendKeys(s);
}
}
public void enterCustomerDetails()
{
sendChar(txtFirstName, "Joh201605130947AM");
sendChar(txtSurname, "Doe201605130947AM");
sendChar(txtEmail, "johndoe#gmail.com");
}
If you guys know a better way, please help! :)
I assume this is caused by this Angular2 issue https://github.com/angular/angular/issues/5808
Angular can't process input events when they arrive too fast.
As a workaround you would need to send single characters with a small delay between each.
I stumbled upon this error when doing integration tests with NightwatchJS (which uses selenium).
So I'm writing this for people coming here in the future.
I wrote this extension command for nightwatch:
exports.command = function (selector, value, using) {
var self = this;
self.elements(using || 'css selector', selector, function (elems) {
elems.value.forEach(function (element) {
for (var c of value.split('')) {
self.elementIdValue(element.ELEMENT, c);
}
});
});
return this;
};
Which can be used in this way:
var username = 'integration#test.com';
browser.setValueSlow('input[ngcontrol=username]', username); //Works with ng2!
This issue was also discussed on NightwatchJS's github here
By using Actions class, this issue solved for me
Tried many ways as mentioned above. Also tried to setvalue by js executescript
Finally, found this code and it worked well for grid component built on angular
actions.sendKeys(webElement,
modifiedValue).perform();
I was getting this error too in Java, Selenium. You might also be getting this error too while writing your codes - "sendKeys (CharSequence) from the type Webelement refers to the missing type charSequence"
I tried varying the wait time and even typing extra characters before the main characters, they did not work.
The simple trick I used was to change the Java Compiler version from JRE 9 to JRE 10.
This is due to a bug in Angular apps. Workaround is to put a sleep function.
public void setText(By element, String text) {
sleep(100); // Angular version < 2 apps require this sleep due to a bug
driver.findElement(element).clear();
driver.findElement(element).sendKeys(text);
}
i had the same problem, if you see it carefully selenium is changing the characters, some numbers perform a backspace or other symbols, i read it happens when using selenium with vncserver, i changed to firefox.... and it worked.
if that's not your problem, maybe sending the data in parts:
input1="Joh201605130947AM"
txtFirstName.sendKeys(input1[0:7])
txtFirstName.sendKeys(input1[8:end])
Using
Chromium 78.0.3904.70,
Vaadin Flow Framework 14.1.3,
Selenium 3.141.59
and OpenJDK 11.0.5
the behavior also occurs and is even worse:
I see that the character is typed in and suddenly after that it disappears.
A workaround is to be persistent and just try it again. And again. Until the character is finally typed in.
// Type in every single character
for (int i = 0; i < textToType.length(); i++) {
boolean typingCharacterWasSuccessful = false;
// If typing was not successful before, just type again
while (!typingCharacterWasSuccessful) {
// Type in the character
char singleCharacterToType = textToType.charAt(i);
htmlTextfeld.sendKeys(Character.toString(singleCharacterToType));
// Wait a little. Maybe alternatively/additionally wait.until(...)
Thread.sleep(200);
// Check typed in string.
String inputValueAfterTyping = htmlTextfeld.getAttribute("value");
if (inputValueAfterTyping.length() > i + 1) {
// Alternatively: delete everything and start all over
throw new Exception("Input value too long. Maybe character typed in twice!?");
}
// Typing was successful if the value in the input field is as expected (up to now)
typingCharacterWasSuccessful
= inputValueAfterTyping.equals(textToType.substring(0, i + 1));
}
}
I had a similar issue for big texts in Java.
I overcome the issue using the copy and paste of the text in the keyboard-related methods, as in the following method:
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
public static void clickAndSetMultilineText(WebElement element, String text) {
/** click on element **/
element.click();
/** clear older content of the text using keyboard functionality **/
element.sendKeys(Keys.CONTROL + "a"); // select all text
element.sendKeys(Keys.DELETE); // delete old text
StringSelection stringSelection= new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null); // copy text to the keyboard
element.sendKeys(Keys.CONTROL+"v"); // paste text to the field
}
Hope this is helpful.
try this code.. other way to set values using javascript
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");
driver.findElement(By.xpath("//input[#name='body']")).clear();
driver.findElement(By.xpath("//input[#name='body']")).sendKeys("Ripon: body text");
I am new to java and android development and I am working on an application that takes input from the user through an EditText. However, sometimes the input data has some characters with an encoding that is different from what the app expects.
I have created a hashmap with all the possible mapping (about 460 elements) and whenever the user types in (or paste) something I use the afterTextChanged method to go through the text and swap the ones that matches the incorrect encoding with the correct ones that displays correctly.
My code works fine for small sized entry, however the longer it is the slower it gets because afterTextChanged will be called recursively after every swap I do. And when pasting a text that has about 30+ characters with incorrect encoding the application will crash.
here is my code:
public void afterTextChanged(Editable editable) {
substituteText(editable);
}
public CharSequence substituteText(Editable txt) {
if (txt.length() == 0) {
return txt;
}
for (int i = 0; i < txt.length(); i++) {
char ch = txt.charAt(i);
CharSequence ret = myHashMap.get(ch);
if (ret != null) {
txt.replace(i, i + 1, ret);
}
}
return txt;
}
I thought about cloning "editable" to some, change it and then copy it back to the edittext editable variable so it will only see it as one change but I failed to do it since I couldn't find an easy way to clone it.
Is there a more efficient way to do this?
If you use onTextChanged instead of afterTextChanged, you'll get passed the new character(s) the user typed and where they are, and you'd only need to check those.
I am currently creating this java GUI that will ask the user to input 10 entries, then use the values to execte the next action.
I want only numbers or decimal point to be inputted inside such that it can only be a float value.
If it is not number or decimal point, it should prompt the user to input that specific entry again before the next action is executed.
How should I do it?
Wong,
not sure whether you are using Swing or not...
Ages ago I had the same problem and I solved it with creating a class RestrictedTextField extending JTextField. In the constructor I added a key listener (addKeyListener(new RestrictedKeyAdapter());)
private class RestrictedKeyAdapter extends KeyAdapter {
#Override
public void keyReleased(KeyEvent e) {
if (getText().equals("")) {
oldString = "";
return;
} else {
// if you cannot parse the string as an int, or float,
// then change the text to the text before (means: ignore
// the user input)
try {
if (type.equals("int")) {
int i = Integer.parseInt(getText());
oldString = getText();
} else if (type.equals("float")) {
float f = Float.parseFloat(getText());
oldString = getText();
} else {
// do nothing
}
} catch (NumberFormatException el) {
setText(oldString);
}
// if the text is identical to the initial text of this
// textfield paint it yellow. If the text was changed
// paint it red.
if (initialString.equals(getText())) {
setForeground(Color.YELLOW);
} else {
setForeground(Color.RED);
}
}
}
}
The idea is, that every time the user presses a key in the textfield (and releases it then), the text in the textfield is parsed. If the component should accept only floats for example then the component tries to parse it as an float (Float.parseFloat(..)). If this parsing is successful everything is fine. If the parsing fails (an NumberFormatException is thrown) then the old text is written back into the textfield (literally ignoring the user input).
I think you can add the KeyAdapter directly to the JTextField without creating a dedicated class for that, but with this solution you can remember the initial string and the old string.
you can play around with the code.. you can change the colour of the textfield if the input is valid or not (or like in my code snippet if the text is identical to the initial string).
one additional comment: I set the 'type' of the textfield in a variable with the name 'type', which is simply a String with the values "int", "float", etc.... a better solution would be here for example an enum of course...
I hope this is helpful...
timo
There are various options for what you would like to do. You can check here for one example of doing so. Another example could be to use Formatted TextFields, as shown here.
On the other hand, upon submission, you can try to parse the value to a float or double. If you get any exceptions, then, the value is not a number.
Lastly, you can use Regular Expressions. An expression such as ^\\d+(\\.\\d+)?$ should match any integer or floating point number.
I am facing a problem in implementing Input method for Virtual Keyboard. Currently I am using robot class for sending input to any application from virtual keyboard. But for that I need to create mapping of key-code and unicode, which is not consistent on different keyboard layout, can I directly pass the UNICODE to any application using Input method without worry about mapping between keycode and unicode.
Any useful link or sample code will be useful.
It is a simple Java program which is always on top of any application and work as onscreen keyboard. Using a mouse while you press any button (key) of the keyboard, the corresponding character will be typed in the application running below. This is working perfectly for English Alphabets. I am facing problem while I am doing for unicode.
find the code snippet below
public static void simulateKeyEvent(char key){
try{
AWTKeyStroke awtKS = AWTKeyStroke.getAWTKeyStroke(key);
int key_code = awtKS.getKeyCode();
System.out.println("key = "+key+" keyCode = "+key_code);
robot.keyPress(key_code);
robot.keyRelease(key_code);
}catch(Exception e){
e.printStackTrace();
}
}
How I sovled it:
//on startup: override the SystemEventQueue
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
final OwnEventQueue newEventQueue = new OwnEventQueue();
eventQueue.push(newEventQueue);
//because dispatchEvent is protected
public class OwnEventQueue {
private final static OwnEventQueue instance;
static{
instance = new OwnEventQueue();
}
#Override
public void dispatchEvent(AWTEvent event) {
super.dispatchEvent(event);
}
public static OwnEventQueue getInstance() {
return instance;
}
}
//then onpress of keyboard button
Character character = getCharacter();
int[] events = {KeyEvent.KEY_PRESSED, KeyEvent.KEY_RELEASED, KeyEvent.KEY_TYPED};
for (int i = 0; i < events.length; i++) {
KeyEvent pressKeyEvent = new KeyEvent(focusComponent, events[i], System.currentTimeMillis(), 0, 0, character.charValue());
OwnEventQueue.getInstance().dispatchEvent(pressKeyEvent);
}
robotKeystrokeSender.keyPress(KeyEvent.VK_RIGHT);
robotKeystrokeSender.delay(10);
robotKeystrokeSender.keyRelease(KeyEvent.VK_RIGHT);
Is your virtual keyboard used as a device by your OS ?
Or, in other words, have you tried considering it as a "real" keyboard ?
According to Java hardware abstraction, were your virtual keyboard to be considered as a driver, it should simply work like a real keyboard.
EDIT : according to comment, this is not a virtual device, but a Java application, as a consequence, probleme is different.
According to Javadoc, Robot can send key strokes given as int. To create those key strokes from characters, I would recommand you create them using getKeystroke(char) before to transform them into integer values using getKeycode(). This way, you would have integer values associatesd your unicode characters, whichever they are.
EDIT 2 : once again, a modification ;-)
it seems like getKeyStroke(String) "should" handle unicode characters.