I have used app.staticTexts["String"].tap() to tap on a button containing that string which is working completely fine.
But the problem here is that i want to print all the static texts which are present on that page, how can i do it in XCUITest ?
My aim here is to iterate through all the static texts present on the page and then add an if condition on my expected text.
You can use something like that:
for staticText in app.staticTexts.allElementsBoundByIndex {
if staticText.label == "test" {
}
}
//Returns all the labels,buttons, textfield,images in the page with its name.
//Just change the element name in loop according to the need.
for staticText in app.staticTexts.allElementsBoundByIndex{
if staticText != nil{
print(staticText.label)
}else{
print("No static text found")
}
}
I suppose the text is present in the identifier attribute of the staticText element. You can try the following
for i in 0..<app.staticTexts.count {
let text = app.staticTexts.element(boundBy: i).identifier
print(text)
if text == "Your String" {
// Your code
}
}
Related
sorry if the title not describe the problem well
i have 4 button with 4 letters ["g","o","o","d"]
it is a game about
if the player can guess the word ,
which is in this case the word is : "good"
i have a hint button that do this :
*give one letter from the four
*hide the button with the given letter
it works very well
but the problem is :
the word "good"
has two "o" letter
so the hint button now well delete two button with the letter "O"
here is what i did so far :
// this method will add a letter from the right word "qlap" to the answer textfield and it work perfectly :
let text_ans = ans.text?.count
ans.text! += qlab![text_ans!+0]
// the four letters
let buttin = [Letter1,Letter2,Letter3,Letter4]
// check if hint letter above == button title letter
for title in buttin {
if qlab![text_ans!+0] == title!.title(for: .normal) {
let tag = title!.tag
if title?.tag == tag {
// how can i just hide for once if there is a duplicate
title?.isHidden = true
}
}
}
and here is a gif to make it more clear :
i tried do it with the button tags rather than title
but at the end i went to the same result
You could use the first(where:) (documented here) function to find the first button with a title that matches what you are looking for and where isHidden == false.
So something along the lines of:
if let hintButton = buttin.first(where: { $0.title(for: .normal) == qlab![text_ans!+0] && !$0.isHidden }) {
// now you have the first - not hidden - button matching what you are looking for, time to hide it
hintButton.isHidden = true
}
note: I haven't checked the above in a compiler so there might be syntax errors which I'm sure the compiler will inform you about in it's friendly manner.
I am trying to do something that was simple to me in UIKit - but cannot get working in SwiftUI.
I am pulling data from an API. That data is dynamic - some parts of the call may not be present every time. I have protected the app by making all of that data optional and using a nil operator to return "N/A" in the text fields where there is no data. In UIKit I was able to simple use an if statement:
if self.cityLabel.text == "N/A" {
self.cityLabel.isHidden = true
}
Now in SwiftUI, I have the following:
HStack() {
Text(self.model?.city ?? "N/A")
}
When the data is present, it displays without any issue. However, I am not sure where to access this property or put an if statement because this data is in a View class and does not accept functions.
So basically, how do I hide that text and have the blocks around it "move up" (like display: none) in HTML while in SwiftUI? How can I implement if statements in the View code?
I'm sure it's probably simple, but assistance would be much appreciated! :)
if, if-let, and if-else now also work inside View (I think it's introduced in Xcode 12). This should work:
var body: some View {
HStack() {
if let city = self.model?.city {
Text(city)
}
}
}
Edit: If you don't care about what's inside self.model?.city, just do a simple boolean test for nil.
var body: some View {
HStack() {
if self.model?.city != nil {
/// self.model?.city is not nil!
Text("Not nil")
}
}
}
Edit 2: Ok, so if the text is "N/A", you don't want to display it. However, because the text itself (self.model?.city) is an optional, you need to unwrap it first.
var body: some View {
HStack() {
if let city = self.model?.city { /// unwrap self.model?.city
if city != "N/A" { /// city is not "N/A"!
/// show the text
Text(city)
} /// else, don't show anything
}
}
}
I have got a problem with UI SearchBar in swift. My problem is I want to use searchbar to find the word in Table View Cell. The word is not an English word.
Here is my code.
if ((searchText.contains("ၵ"))) {
filteredArray = homeDict.filter { $0._meaning.localizedCaseInsensitiveContains(searchText) }
}else{
filteredArray = homeDict.filter { $0._word.localizedCaseInsensitiveContains(searchText) }
}
if(filteredArray.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
That is work for only one word of "ၵ". I want to search, if the word in Searchbar the same as any word in Table View Cell, display result in cell row.
I believe this function uses the current Locale, not necessarily the locale that the letters might be in. Try testing with the device in the locale of the language the strings are in to check.
You could probably make your own contains with func lowercased(with locale: Locale?) -> String
You would need to know the locale the user intended the search to be in. You could also use this function to test what the lowercase version of a string is in various locales (to test the theory above).
I currently have four text fields. At the beginning, only one text field shows. After the user enters text to the first text field and taps a button a new text field is presented and the previous one is hidden with the text field hidden property. My current code only shows the first text field, hides it and shows a new one, and stays in the second text field. There are still two more text fields that need to have the same functionality.
Can you explain why my current code is not working?
Thank you
This is my current code
if nameTextField.text != nil {
nameTextField.resignFirstResponder()
emailTextField.isHidden = false
nameTextField.isHidden = true
emailTextField.becomeFirstResponder()
} else if emailTextField.text != nil {
emailTextField.resignFirstResponder()
emailTextField.isHidden = true
firstPasswordTextField.isHidden = false
firstPasswordTextField.becomeFirstResponder()
} else if firstPasswordTextField.text != nil {
firstPasswordTextField.resignFirstResponder()
firstPasswordTextField.isHidden = true
phoneNumberTextField.isHidden = false
phoneNumberTextField.becomeFirstResponder()
} else if phoneNumberTextField.text != nil {
phoneNumberTextField.resignFirstResponder()
}
Hi Mathew the condition what you have written is incorrect . Lets say user first enter in nameTextField and then you are checking emailTextField not equal to nil.. Sone ones user enter in both the textfield , it is always going to satisfy the first condition only.. Just put a brteakpoint and check your logic.
I have 5 buttons representing cities, I have assigned them tags via Attribute Inspector as follows, CityA as 0,.......CityE as 4.
Is there a way I can store these tags into a variable cityTag and make sure that if none of button is pressed while saving, I can send a message "Please select a city"
I created an action with multiple buttons, but I have no idea how to create a variable and assign tags to it.
For better readability of code yourcould define an Enum for that case which represents the name and tag as well :
for example:
enum City: Int {
case .cityA,
case .cityB,
case .cityC
}
You can store them in an Array:
var cities: [City] = []
To set a city:
if let cityA: City = City(rawValue: button.tag) {
cities.append(cityA)
}
To read the Int value:
let rawValue: Int = cityA.rawValue
Since you are adding tags via the attribute inspector, you can access the tapped button's tag via sender.tag property!
Initially create an NSMutableArray (that will hold a the tags of all buttons pressed) but will obviously be empty at the start! You can access the tag with the sender.tag property in the IBAction. If your NSMutableArray doesn't contain a tag when you try to save, you can show the alert.
Initialise citytag = -1. Change the value of citytag when you press a button like citytag = sender.tag and keep a check if citytag == -1 then send a message please select a city else selected city is ###
you can try this:
func checkbuttons -> BOOL {
var countBtn = 0
for view in self.view.subviews as [UIView]
{
if let btn = view as? UIButton
{
if btn.isSelected
{
countBtn = countBtn + 1
}
}
}
if(countBtn > 0)
{
return true
}
else
{
return false
}
}
On basis of return you will know button selected or not.
Hope it will help you