how to get value of an XCUIElement? - xcode7

How to get the value of textfield in XCODE7 UITesting?
var b = XCUIApplication().childrenMatchingType(.textField).elementBoundByIndex(0).stringValue

Let's suppose you have a Text Field like so:
(I'm hardcoding it's value there ("My Text") for the sake of the example.)
Give it an Accessibility Label. In this case, I'm giving it the "Text Field" label.
Now to access its value, you could do something like:
func testExample() {
let app = XCUIApplication()
let textField = app.textFields["Text Field"]
XCTAssertTrue(textField.value as! String == "My Text")
// So basically "textField.value"
}
On a side note: when in doubt on how to access certain properties of a given XCUIElement, I found that its debugDescription helps a lot.
For example, set a breakpoint after the property declaration, execute the test, wait for the app to stop at the breakpoint, go to lldb console, type po propertyName.debugDescription, and check the output:
I hope that helps.

Lets assume I want to get the string that gets printed for the nav bar of my app
What I did was I created a XCUIElement for my nav bar:
XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *navBarTitle = [app.navigationBars elementBoundByIndex:0];
I then put a breakpoint after the creation of the navBarTitle object and used the debug console to print out the details of the navBarTitle object:
You see in the print out of the debug console that there is a key called identifier.
To extract that string from that object, I created an NSString object using the following method:
NSString *nameofuser = [navBarTitle valueForKey:#"identifier"];
I used the XCUIElement navBarTitle and then used the method valueForKey. valueForKey extracts the string value for the key identifier.
You can read up about this method here:
NSKeyValueCoding
valueForKey is the KEY to unlocking the answer to this question....pun intended :)

app.staticTexts.element(boundBy: 1).label

Related

Array to string conversion in EasyAdmin 3 (Symfony 5)

I'm trying to display a json array on the EasyAdmin detail page. I read here Is there a way to represent a JSON field in EasyAdmin 3? that you can use ArrayField in EasyAdmin 3 to display a json array, which would make sense to me as that is the only field in EasyAdmin that could display it so I added it like this:
public function configureFields(string $pageName): iterable
{
return [
ArrayField::new('status', $this->translator->trans('form.label.status'))->onlyOnDetail(),
];
}
But it gives me this error:
An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"). Do I need to add something else to it or does it not work at all?
change "status" to a multiplicity "statuses"
Worked for me with changing "print" to "prints"
I found a workaround that resolved the issue in my situation where I wanted just to show JSON on details page
So in your entity add a get function as an unmapped field as indicated in the official documentaiton
https://symfony.com/bundles/EasyAdminBundle/current/fields.html#unmapped-fields
for example
public function getJsonField() {
return json_encode($this->yourEntityJsonAttribute);
}
then in configureFields function in your CrudController add your field like this
TextAreaField::new('jsonField')->onlyOnDetail()
You can also read the json attribute and generate an HTML string in the getJsonField function then in configure field just add renderAsHtml in th field like this
TextAreaField::new('jsonField')->onlyOnDetail()->renderAsHtml()
Wish this suits your case too, Good luck

Json element value returning null though found in request body

I am trying to pass some json data
{
"name":"srk",
"age":25,
"interests":"programming"
}
through ajax and fetching the request body into play controller as shown below.
#BodyParser.Of(BodyParser.Json.class)
public Result example(){
JsonNode jsonNode = request().body().asJson();
Logger.info("requestBody = "+ jsonNode);
}
The above logger prints the request payload sent during the request exactly
like below:
{"name": "srk", "age":25, "interests" : "programming"}
But when I try to print age they it is showing up as null.
String age = jsonNode.findPath("age").textValue();
Logger.info("age= "+ age);
This is printing age= null
Any ideas would help.
As I cannot yet comment, I'm writing a proper answer in the name of #DBug.
#srk, please choose a correct answer and mark the question as solved =)
The JSonNode .textValue() method seems not to automatically convert between content types. Since age is a numeric field, the textValue() is actually null. There are two possible solutions:
You can retrieve the node content as the correct type with:
jsonNode.findPath("age").asInt();
You can retrieve the node content as a String with either:
jsonNode.findPath("age").asText();
//or
jsonNode.findPath("age").toString();
I went looking for the documentation here: com.fasterxml.jackson.databind.JsonNode
Direct link to textValue() description.
Direct link to asInt() description.
Direct link to asText() description.
You should use jsonNode.get("age").asInt() to get the value instead of findPath

Is there a way to "grep" for a keyword in a JavaScript object in Chrome Dev Tools?

I often work with large JavaScript objects and instead of manually opening and closing "branches", I would like to simply search for a particular string and show any key or value that matches.
Sort of like "grepping" for a keyword in a JavaScript object. Is this possible (especially in Chrome Dev Tool)?
Unfortunately I was hoping I could at least try the JSON.stringify() trick and then search on the raw JSON in a text editor, but I get the following error:
Uncaught TypeError: Converting circular structure to JSON
You can look at the object's keys and match against them:
function grepKeys(o, query){
var ret = {};
Object.keys(o).filter(function(key){
return key.includes(query);
}).forEach(function(key){ // can reduce instead
ret[key] = o[key]; // copy over
});
return ret;
}
Which'd let you return a partial object with all the keys that contain the string you specified. Note that this will not show any prototype keys but can be easily extended to allow it (by using a for... in instead of an Object.keys or by using recursion):
var o = grepKeys({buzz:5, fuzz:3, foo:4}, "zz");
o; // Object {buzz: 5, fuzz: 3}

how to add a dictionary as a value to another dictionary in swift

I'd like to add a dictionary to another dictionary in swift but I get the following error.
My aim is to mirror the structure of a nested object into a dictionary so that I am able to serialize it to JSON.
func toDictionary() -> Dictionary<String,AnyObject>{
var dic = Dictionary<String,AnyObject>()
println(From.Text)
println(From.Email)
var fromDic = ["Address":From.Email,"Text":From.Text]
println(fromDic)
dic.updateValue(fromDic, forKey: "From")
the code you show works fine for me - the error is likely not on that line xcode shows.
xcode6 often misses the correct line and the real error is some lines below
Assuming you have declarations for From, From.Email and From.Text, then it appears to work fine:

Check Dictionary WIth ObjectForKey IsEqualToString not working

i have this code and searched the internet and find it ok but its not working with me
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
.....
NSLog(#"%#",[dict objectForKey:#"STATUS"]);
if ([[dict objectForKey:#"STATUS"] isEqualToString:#"Y"]) {
NSLog(#"Done");
}
The NSLog Shows Y Which Should Work With if statment
When you do:
[dict setObject:[arr objectAtIndex:1] ...
i think this is adding the strings to the dictionary as objects. The NSLog line is working because NSLog sends each object it is passed the "description" message to convert it to its string value. you can do the same thing.
try changing this:
if ([[dict objectForKey:#"STATUS"] isEqualToString:#"Y"])
to this:
if ([[[dict objectForKey:#"STATUS"] description] isEqualToString:#"Y"])
That would get the string value of the object returned by objectForKey: before doing the comparison.
If you think there might be a white space throwing off the compare, trim the string before comparing, like this:
if ([[[dict objectForKey:#"STATUS"] stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:#"Y"])