What is the best practice to simulate an ENTER or RETURN using Selenium WebDriver - selenium

I came across this solution to my initial problem, which was to simulate an ENTER or RETURN key press using Selenium WebDriver.
However, in my code, I strictly want to use only one of the two WebElement.sendKeys(Keys.ENTER); vs WebElement.sendKeys(Keys.RETURN);.
What is the best practice when doing the same, since there seems to be divided opinion about using enter or return, since both work MOST of the time? In what scenarios would one or the other not work, and is there one which would ALWAYS work?

As a performancewise I do not get any change on both of these,
But yes I know one difference on them
Keys.Enter is used to enter key on the number pad
while
Keys.Return is used to one next to the letters
Generally I have preferred Keys.Enteras sometimes in some browser Keys.Return is not worked for me

Let us analyze Keys.ENTER and Keys.RETURN in details.
Keys.ENTER and Keys.RETURN both are from org.openqa.selenium.Keys, which extends java.lang.Enum<Keys> and implements java.lang.CharSequence
Enum Keys :
Enum Keys is the representations of pressable keys that aren't text. These are stored in the Unicode PUA (Private Use Area) code points, 0xE000-0xF8FF.
Key Codes :
The special keys codes for them are as follows :
RETURN = u'\ue006'
ENTER = u'\ue007'
The implementation of all the Enum Keys are handled the same way.
Hence there is No Functional or Operational difference while working with either sendKeys(Keys.ENTER); or WebElement.sendKeys(Keys.RETURN); through Selenium.
Enter Key and Return Key :
On computer keyboards, the Enter (or the Return on Mac OSX) in most cases causes a command line, window form, or dialog box to operate its default function. This is typically to finish an "entry" and begin the desired process, and is usually an alternative to pressing an OK button.
The Return is often also referred as the Enter and they usually perform identical functions; however in some particular applications (mainly page layout) Return operates specifically like the Carriage Return key from which it originates. In contrast, the Enter is commonly labelled with its name in plain text on generic PC keyboards.
Wiki References : Enter Key Carriage Return

As yourself, I failed to find a good explanation to this question online so I tested it myself using this Keyboard Events tester.
driver.get("https://dvcs.w3.org/hg/d4e/raw-file/tip/key-event-test.html");
WebElement textArea = driver.findElement(By.id("input"));
textArea.sendKeys(Keys.ENTER);
textArea.sendKeys(Keys.RETURN);
As a result I've got this output (this is Keys.ENTER followed by Keys.RETURN):
So it seems like there is no difference between these two options.

Related

VIM equivalent of IntelliJ's expand/shrink selection?

How would one achieve the same result. I believe the keybinding for macOS Intellij is op+up/down and on windows it is alt+w/d.
Essentially the function highlights the current word, then, with successive presses, expands out to the full string/line/area in-between parenthesis/further out to the next set of parenthesis. Very useful for developing in LISP.
The closest I've gotten is this: https://vi.stackexchange.com/a/19028
Try this plug in: https://github.com/terryma/vim-expand-region
It expands selections based on Vim’s text objects.
Well this may seem comfortable but does not correspondent with the internal logic of vim itself.
See, in vim everything you enter is like a sentence. va{ for example: there is a verb v -> visually select and an object (or movement) { -> paragraph. In this case there is also a modifier a around. You can exchange stuff in this sentence and it will still work vaw, dil, cB and so on. The power of vim is greatly based on that concept.
Of course you can write a function that does vaw first, then S-v and lastly va{ but that will only work with visual selection. It will not work with c or d or anything. So I will recommend to get used to use different keys for different actions.
The visual selection is mostly not needed anyway. Change a paragraph? directly use ca} and so on.
I have found that VI/VA + WOBO (as many times as you need to expand) works similarly. Not as fast but its the same concept and you can even expand/shrink asymmetrically based on your WO's and BO's (Or OW's and OB's depending on how you look at it)

How to create a custom Allure step function for sensible data

I am currently working on a Testing Automation team, using Python and Allure to make reports of all the test cases that we run. Sometimes we deal with sensible data (e.g: passwords) that I can't show on the reports. If I use a function with a step decorator, something like this:
Which takes an element (a text box) and enters the value in it. In the step function I display the value that I want to enter, I could easily change that but the problem resides in the actual report. No matter what I enter on the step title, the report always shows the info that was passed as arguments to the function:
Thus, the "value" argument will always be displayed and that is something that I cannot have on certain projects. Is there anyway to make a custom step function that solves my problem?. I could either use with not showing the value at all or to change it to something like '*****'.
Just a thought.
#allure.step("Entering a value in element {3}")
def setSecureBoxValue(driver, element, value, box_name):
I solved my problem with the use of Fernet cryptography library.
I created a new function for the sensible data that encrypts the strings and then, inside this new function I call the one I shared on the screenshot (with a slight modification to decrypt the data). This results in the following report:

Android WebDriver not able to enter alphanumeric character

I am trying to automate a mobile website on android device, using Android WebDriver library.
When the script enters some alphanumeric characters , eg Test12345, in a text field the textbox shows "TEST!##$%" as entered text. It is true for any alphanumeric word starting with capital letter.
But, if I make the first character a SMALL character or a number then correct word is inserted.
Here is the code which I am using (same scenario happens on any textbox field)
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Test12345");
Is it a bug or am I missing something?
It seems that this is a bug. As a workaround you can type any (except NULL U+E000 or "\xEE\x80\x80" in UTF-8) character before your string. E.g. you can start typing with sending two SHIFT (U+E008 or "\xEE\x80\x88" in UTF-8) characters, or one CLEAR (U+E005 or "\xEE\x80\x85" in UTF-8) character. Just anything unprintable, and after that type your string.
FYI: I've confirmed the bug still exists. I'm looking at the android webdriver code to see if I can fathom out why it's occurring. I have some ideas, it's likely to be in one or other of:
java/client/src/org/openqa/selenium/android/library/AndroidKeys.java
java/client/src/org/openqa/selenium/android/library/EventSender.java
java/client/src/org/openqa/selenium/Keys.java
and related to mapping keys between Selenium-Webdriver and Android.
I have found another workaround, similar to the one mentioned by Ivan. Mine involves splitting the text into 2 parts, the first capitalized character e.g. the T and use sendKeys to send it; then send the rest of the text e.g. est12345
searchBox.sendKeys("T");
searchBox.sendKeys("est12345");
Ugly, shouldn't be necessary, but a practical option that doesn't involve sending extra (unprintable) characters.

target.frontMostApp().keyboard() failed to locate key 'N'

I'm trying to automate keyboard typing with UI Automation.
target.frontMostApp().keyboard().typeString("INTERCOM")
But i will get this error after first 'I' is typed
target.frontMostApp().keyboard() failed to locate key 'N'
Script threw an uncaught JavaScript error: target.frontMostApp().keyboard() failed to locate key 'N'
I have a localized swedish keyboard.
Anyone know if this a bug or something I've missed?
This might help:
var vKeyboard = target.frontMostApp().keyboard();
vKeyboard.setInterKeyDelay(0.1);
vKeyboard.typeString("INTERCOM");
By default this delay is set with 0.03 seconds. This is not enough for your application to update the keys on your keyboard. Increasing this timeout between determining keys for typeString keyboard method will help you. There is no description for setInterKeyDelay on UIAKeyboard reference page but this method is available for UIAKeyboard.
Also I'm not sure about other languages. I do not know if typeString allows to type on other languages but this 100% works for English keyboard for iOS 5.x.
try{
target.delay(1);
target.frontMostApp().mainWindow().textFields()[0].tap();
target.delay(1);
target.frontMostApp().mainWindow().textFields()[0].setValue("INTERCOM");
}
catch(err){
target.delay(1);
target.frontMostApp().mainWindow().scrollViews()[0].textFields()[0].tap();
target.delay(1);
target.frontMostApp().mainWindow().scrollViews()[0].textFields()[0].setValue("INTERCOM");
}
I've had this problem as well and I believe it's a case of the string being typed too quickly.
It seems that the names of the key,change depending on the status of the shift button.If shift is enabled then the key is called 'N',if shift is not enabled then it's 'n'.You'll notice as a string is being typed,that the shift button is tapped before an uppercase letter is typed.Your test is attempting to press the 'N' key before the 'Shift' button has been pressed.It doesn't affect the first letter of your sentence because the keyboard has shift enabled for the first letter.
This also affects typing a lowercase character after an uppercase character:the lowercase character may be typed whilst the shift button is in the process of being unpressed.
I use a workaround of typing each letter of the string with separate typeString() methods.
for (i = 0; i < title.length; i++)
{
var strChar = title.charAt(i);
target.frontMostApp().keyboard().typeString(strChar);
}
The downside to this is that it takes a lot longer to type the full string.
You may also want to look at the following link which provides a similar solution but uses the app.keyboard().keys().tap() method for each character of the string instead of the typeString() method.
http://jojitsoriano.wordpress.com/2011/06/27/ios-ui-automation-typing-a-string-in-a-uiatextfield/

How can I validate text box input?

I am creating a program and I need to validate my text boxes. For the program the user needs to put in a phrase. But I am not sure how to make sure that the user actually entered in a phrase, the phrase isn't (ex.) skldkfdl, or that there isn't a space.
Strings in Java
You could do a String.Trim() to get rid of trailing whitespaces first...
then do a String.IndexOf(" ") to check for a space.
If the function returns -1, it means there is no space in the string.
Running on the assumption that you're using VB.Net - Add an event handler for the event where you want to validate the text, such as when a "Submit" button is clicked. You may want to use a CancelEventHandler, so that you can cancel the click.
In the event handler, if you're looking for just simple validation, you can use if-statements to check some simple conditions, such as if you just want to check "if input.equals(password)".
Look here for an example of using CancelEventHandler
If you're looking for some more complex validation, you'll want to use regular expressions.
This page might help get you started
Checking to see if something is "a phrase", as in, proper English, would be very difficult. You would need to make sure that all of the words are in the dictionary, and then you would need to check for proper grammar, which is incredibly complex, given English grammar rules. You may want to simplify your approach, depending on your problem. For example, maybe just check that no weird characters are used, that there is more than one space, and that each word contains a vowel.