pass EditText to a string - android-edittext

EditText hi = (EditText) findViewById(R.id.editText1);
String hi2 = hi.toString();
editor.putString("user_email", hi2);
editor.commit();
The code above in my on click works and compiles fine but the value i get when i go into my settings is
androd.widget.EditText#414e4e70
Does anyone know the correct syntax for me to pass a value from a user input box into a my pref.
if i use
editor.putString("user_email","helloworld#google.com");
then it works because its a full string.

EditText hi = (EditText) findViewById(R.id.editText1);
String hi2 = hi.getEditableText().toString();
editor.putString("user_email", hi2); //put into the email settings the value from the user input
editor.commit();
I missed the getEditableText()
Late night coding :(

Related

How do I assert that a text field is empty?

I have an empty text field on my UI, though it has a placeholder text (whose value is foo) set in the storyboard. In my UI test, I am trying to check that its text value starts out empty, but when I query it's value, it seems to be giving me the placeholder value instead:
func testTextFieldInitiallyEmpty {
let input = XCUIApplication().textFields["My Text Field"]
XCTAssertEqual(input.value as! String, "")
}
as the test fails with this message:
XCTAssertEqual failed: ("foo") is not equal to ("")
Of course, foo is the placeholder value, but it's not the text value of that text field. I would have expected that error message if I had written:
XCTAssertEqual(input.placeholderValue as! String, "")
input is a XCUIElement, which implements XCUIElementAttributes, so I don't see anything else that would do the trick here.
How do I check (assert) that the text field is empty?
Edit
After doing some further research and trying out the suggestions below for using the input's properties of accessibilityValue, label, and title, I have not found any solution that will give me the text field's text value when there is text, and an empty string when only the placeholder is visible.
This seems like either (a) a bug, or (b) a questionable design decision from the test framework to not provide that ability. At a minimum, the documentation for XCUIElementAttributes#value seems inadequate to me, as the only detail is:
The exact type of value varies based on the type of the element.
Still looking for a better solution...
You can compare to the XCUIElementAttributes's placeholderValue variable in addition to checking for a blank string
extension XCUIElement {
func noTextEntered() -> Bool {
return self.value as? String != "" && self.value as? String != placeholderValue
}
}
Then you can run XCAssert(input.noTextEntered(), "Unexpected text entered into field")
Just make sure your placeholder is not something a user would type in. This way you don't have to hardcode placeholder values to check against
Kind of ridiculous that this is actually the case it works and that it needs a workaround.
Anyway, my solution to get the value w/o the placeholder interfering, based on #Jason's answer.
extension XCUIElement {
var valueWithoutPlaceholder: String {
if let v = value as? String, v != placeholderValue {
return v
}
return ""
}
}
Be aware, if the input is actually the placeholder this would break!
Try using accessibilityValue property of input.
func testTextFieldInitiallyEmpty {
let input = XCUIApplication().textFields["My Text Field"]
XCTAssertEqual(input.accessibilityValue, "")
}
If you command+click the property, you can see the following..
/*
Returns a localized string that represents the value of the element, such as the value
of a slider or the text in a text field. Use only when the label of the element
differs from a value. For example: A volume slider has a label of "Volume", but a value of "60%".
default == nil
default on UIKit controls == values for appropriate controls
Setting the property will change the value that is returned to the accessibility client.
*/
public var accessibilityValue: String?

what to do when two elements have same resource id in appium?

what to do when the resource-id of two elements in list view in UIAutomator of appium is same??
here in the images below:-
both the elements have same resource id:-net.one97.paytm:id/smart_list_root
Get the text using xpath:
String text = driver.findElement(By.xpath("//android.widget.RelativeLayout")).getText();
Compare the text and select the correct UIElement to perform desired action.
if(text.equals("Mobile Prepaid")){
......
}
Take it with multiple elements for xpath for textview as
"//android.widget.TextView[#text='Mobile Prepaid']"
Hope it will work
In this case, you can use xpath or name like below:
Way 1:
driver.findElement(By.xpath("//android.widget.TextView[#text='Mobile Prepaid']"));
driver.findElement(By.xpath("//android.widget.TextView[#text='Mobile Postpaid']"));
Way 2:
driver.findElement(By.name("Mobile Prepaid"));
driver.findElement(By.name("Mobile Postpaid"));
You can use By.name like,
driver.findElement(By.name("Mobile Prepaid"));
driver.findElement(By.name("Mobile Postpaid"));
String ac = driver.findElement(By.xpath("(//*[#resource-id = 'account-balance-amount'])[1]")).getText();
String ac = driver.findElement(By.xpath("(//*[#resource-id = 'account-balance-amount'])[2]")).getText();

editText get text kotlin

How to get editText in kotlin and display with toast.
var editTextHello = findViewById(R.id.editTextHello)
I tried this but shows object
Toast.makeText(this,editTextHello.toString(),Toast.LENGTH_SHORT).show()
This is Kotlin, not java. You do not need to get the id of it. In kotlin, just write:
var editTextHello = editTextHello.text.toString()
use the beauty of kotlin ;-)
P.s: BTW, better to choose xml IDs like edx_hello and for the kotlin part, var editTextHello. Then you can differentiate between xml vars and kotlin vars.
You're missing a cast of the View you get from findViewById to EditText:
var editTextHello = findViewById(R.id.editTextHello) as EditText
Then, you want to display the text property of the EditText in your toast:
Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()
For the record, this is just the more idiomatic Kotlin equivalent to calling getText() on your EditText, like you'd do it in Java:
Toast.makeText(this, editTextHello.getText(), Toast.LENGTH_SHORT).show()
The voted answer is correct but it is not the best one for the Kotlin's world. If you're really interested in entering into this world, I'd recommend you to use extensions. From Kotlin you have kotlin-android-extensions and with it you can do this:
import kotlinx.android.synthetic.reference_to_your_view.editTextHello
and this:
Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()
please, forget about the getText()... use just this, it is more clean.
ps: read about extensions and you will see you can create your own extensions and do an even more clean usage of the Toast. Something like this:
fun Context.showToast(text: CharSequence, duration: Int = Toast.LENGTH_LONG) = Toast.makeText(this, text, duration).show()
and it being used like this through your classes:
showToast("uhuuu")
but this is beyond the scope we're talking about here.
from: https://kotlinlang.org/docs/tutorials/android-plugin.html
Use this instead it's working fine
val obj=findViewById<EditText>(R.id.editText)
Toast.makeText(this,obj.text, Toast.LENGTH_LONG).show()
use editTextHello.text
Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()
Toast.makeText(this, editTextHello.text.toString(), Toast.LENGTH_SHORT).show()
If you make edittext as nullable then the line would be
Toast.makeText(this, editTextHello?.text.toString(), Toast.LENGTH_SHORT).show()
In Kotlin calling .text on your EditText is fine no need to do getText or toString
Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()
with button click
button?.setOnClickListener {
Toast.makeText(this,editText.text, Toast.LENGTH_LONG).show()
}
Even don't need findViewById
You're missing a cast of the View you get from findViewById to EditText.
But you need a "if" if the control exist then get the text:
val editText = findViewById(R.id.editText_main) as EditText
if (editText != null) {
val showString = editText.text.toString()
Toast.makeText(this, showString, Toast.LENGTH_SHORT).show()
}

Parsing a UITextDocumentProxy in Swift 3

I'm currently developing a custom keyboard app and am having trouble parsing what the keyboard has outputted onto the text document proxy. How does one go about this? I feel like I'm losing my mind. Currently I'm looping:
for letter in (proxy.documentContextBeforeInput?.characters)!
However, this is only getting the text on the line the cursor is currently on, before the cursor, such that if my textDocumentProxy contains:
Some text above
Some text below (cursor position)
My loop only iterates throught the "Some text below" portion.
Is there any way to loop through the entirety of a UITextDocumentProxy? Thank you.
documentContextBeforeInput, as its name mentions, only returns the text before the input. To get the full text string, you should do something like:
let entireText = (proxy.documentContextBeforeInput ?? "") + (proxy.documentContextAfterInput ?? "")
if let chars = entireText.characters {
for letter in chars {
//DO something useful
}
}

UI Testing iOS, Selecting Secure Text Entry textField

I have password and password conf textFields that I have applied the "Secure Text Entry", to mask the password. However, when I run the UI tests, those fields can not be found getting
UI Testing Failure - No matches found for "password" TextField
I'm attempting to select the fields like so:
let passwordTextField = app.textFields["password"]
but then it fails when I try to tap:
passwordTextField.tap()
Any ideas on how I can access the field?
As discussed in the comments, when accessing a secure text field use the secureTextFields selector.
let passwordTextField = app.secureTextFields["password"]
passwordTextField.tap()
passwordTextField.typeText("my secure password")
Also if you need to type to secureTextField
If menu I/O Hardware Keyboard is ON typeText not working for secureTextField.
Use below to type to secure field
password.tap()
app.keys["t"].tap()
app.keys["e"].tap()
app.keys["s"].tap()
app.keys["t"].tap()
Joe's answer is good, but it was insufficient for my use case. I had to find all text fields by index, no matter if it's a normal text field or a secure text field. I couldn't find any information on the web about this.
That's what I came up with:
// elementType must be specified as integer
//
// See:
// * https://developer.apple.com/documentation/xctest/xcuielementtype/xcuielementtypetextfield
// * https://developer.apple.com/documentation/xctest/xcuielementtype/xcuielementtypesecuretextfield
let textFieldPredicate = NSPredicate(format: "elementType == 49")
let secureTextFieldPredicate = NSPredicate(format: "elementType == 50")
let predicate = NSCompoundPredicate(
orPredicateWithSubpredicates: [
textFieldPredicate,
secureTextFieldPredicate
]
)
let textFields = app.descendants(matching: .any).matching(predicate)
let textField = textFields.firstMatch
textField.tap()
textField.typeText('yeah boi')

Resources