Testing iOS App on Simulator with Appium - Element Not Found - ios

I'm using Appium client to record and generate the testing script for my iOS App.
On the App Inspector, I can tap on a login button and generate the script (in python) like below:
els1 = driver.find_elements_by_accessibility_id("login")
els1[0].click()
I can successfully log in to my app tapping the button on the App Inspector yet I got an error as I run the script on mac terminal:
els3[0].click()
IndexError: list index out of range
I tried different ways to access the button element by using accessibility id, name and class name, but none of the above work.
What did I miss? Is it a bug of the Appium software?

This error "IndexError: list index out of range" appears if we try to access list at index which is not present inside the list range
e.g.
thislist = ["apple", "banana", "cherry"] // here list index range is 0-2
thislist[1] = "blackcurrant" // this works fine as value of index is in range 0-2
But if I try below
// this is run time error i.e. "IndexError: list index out of range"
// as value of index is out of range 0-2
thislist[3] = "blackcurrant"
Note: List Index starts with 0
Consider a case where find_elements_by_accessibility_id("login") method does not return any element for any reason
els1 = driver.find_elements_by_accessibility_id("login");
And I try to access List els1 at 0 index which is empty so I get error "IndexError: list index out of range"
Now before accessing List we will check if List in not empty
els1 = driver.find_elements_by_accessibility_id("login")
if els1:
els1[0].click()
else :
print "Element not found and els1 array is empty"

After hours of googling and trying, I've found that the problem is about view refreshing.
Every time a view transition or navigation happens, it takes time to update the view. Once everything is updated the webDriver could successfully identify an element using the given search parameters.
So between every interaction just wait for a second:
el1 = driver.find_element_by_accessibility_id("login")
el1.click()
// wait for the view to get updated
driver.implicitly_wait(1)
els2 = driver.find_elements_by_name("Edit")
els2[0].click()
And the script would run as expected.

Related

IsTradeAllowed not returning what I would expect

Please see the script below:
void OnStart()
{
Alert(IsTradeAllowed()); //alert 1
Alert(IsTradeAllowed(NULL, TimeGMT())); //alert 2
Alert(IsTradeAllowed(Symbol(), TimeGMT())); //alert 3
Alert(IsTradeAllowed("GBPUSD", TimeGMT())); //alert 4
}
This returns:
true //for alert 1
true //for alert 2
false //for alert 3
false //for alert 4
As alert 2 returns: true, then I would expect alert 3 and alert 4 to return true.
I have tried running the code at multiple times of the day on weekdays. The code returns the same result at weekends. I have also tried putting the code in a script and an EA. Every time I get the same result. Is there an explanation for this? I have tried what is suggested here: https://www.mql5.com/en/docs/runtime/tradepermission
Symbol() returns: "GBPUSD". Each alert should return true in my mind, however this does not appear to be the case here. Incidentally I have notices that Symbol() returns the symbol at the top of the demo account watchlist if the script is run inside MetaEditor, however it returns the symbol displayed on the chart if run inside the demo account.
The broker is Oanda.
Update 04/03/21 at 19:55
I have now discovered that if I right click on the Market Watch and select: show all, then more symbols appear. I can then see that some symbols are greyed out and some symbols are not. The symbols that are not greyed out e.g. USDGBP-g return what I would expect when running the program above i.e. alert 1-alert 4 =true. The symbols that are greyed out e.g. USDGBP returns true; true; false; false in the program above. I now have two questions:
Why does: IsTradeAllowed(NULL, TimeGMT()); //alert 2 return true for symbols that are greyed out?
What does -g mean in GBPUSD-g?
IsTradeAllowed checks if the Expert Advisor is allowed to trade and trading context is not busy.
The version of the function without any arguments will check if the EA has been applied with the correct permissions ("Allow live trading" ticked and "AutoTrading" enabled).
The second form of the function:
bool IsTradeAllowed(const string symbol, datetime tested_time);
checks if the EA would be allowed to trade according to the specifications for the chart selected (to view this, from the Market Watch window right click a symbol, from the menu that pops up select "Specification").
For example
IsTradeAllowed(Symbol(), D'2021.03.06 12:00');
would check if the current symbol can be traded this coming Saturday (which should be false).
If you are getting undesirable results you should check that your broker has set the "Specifications" correctly.
EDIT
I've tested the command in OANDA which is the broker you are using and the command functions as expected.
NULL is not valid for a Symbol and its use makes the command function in its first form (ie timedate is ignored).
I would suggest rather than use Alerts to examine output, try the following.
string cmnt;
cmnt=StringConcatenate("Alert 1: ",IsTradeAllowed());
cmnt=StringConcatenate(cmnt+"\r\n","Alert 2: ",IsTradeAllowed(NULL, TimeGMT()));
cmnt=StringConcatenate(cmnt+"\r\n","Alert 3: ",IsTradeAllowed(Symbol(), TimeGMT()));
cmnt=StringConcatenate(cmnt+"\r\n","Alert 4: ",IsTradeAllowed("GBPUSD", TimeGMT()));
Comment(cmnt);
I tried your script in my ICMarkets version of MetaTrader4. With disabled auto trading i get result:
When I set auto trading to enable using Ctrl+E shortcut, in all cases script return true.
I recommend you to try this script on another account or different broker. If this will help, you should communicate with your broker about this issue. The last option is a reinstall MetaTrader to make sure, that all config files are correct.

How do I reset the ScanStreamTransformer Accumulator?

I am writing a Flutter application using the BLOC pattern. I'm currently trying to search a database and return a list of results on the screen.
The first search will complete fine. The issue is when I hit the back button and try to do a second search. The first search results aren't cleared out. Instead they remain on the screen and the new search results are placed below them.
In a nutshell here's what I'm doing.(I'm using the RxDart library)
1.) Defining input and output streams:
PublishSubject<SearchRowModel> _searchFetcher =
PublishSubject<BibSearchRowModel>();
BehaviorSubject<Map<int, SearchRowModel>> _searchOutput =
BehaviorSubject<Map<int, SearchRowModel>>();
2.) Piping the streams together in the class constructor.
_searchFetcher.stream.transform(_resultsTransformer()).pipe(_searchOutput);
3.) Adding the results to the fetcher stream
results.searchRows.forEach((SearchRowModel row) {
_searchFetcher.sink.add(row);
});
4.) Using a ScanStreamTransformer to create a map of the results.
_resultsTransformer() {
return ScanStreamTransformer(
(Map<int, SearchRowModel> cache, SearchRowModel row, index) {
cache[index] = row;
return cache;
},
<int, SearchRowModel>{},
);
}
From my debugging, I've found that the code in step 4 appears to be the issue. That cache (or the Accumulator) isn't getting reset between searches. It just keeps adding additional results to the map.
I've yet to find a way of resetting the map in the accumulator / cache that works. I even tried completely destroying the streams and recreating them, but the original search data in the SearchstreamTransformer accumulator still persisted.

how do you iterate through elements in UI testing in Swift/iOS?

I understand how I could, for example look at one element in a table, but what's the correct way to simply iterate through all the elements, however many are found, to for example tap on them?
let indexFromTheQuery = tables.staticTexts.elementBoundByIndex(2)
Okay I succeeded in figuring out the simple syntax. I have just started working with Swift and so it took me sleeping on it to think of the answer.
This code works:
var elementLabels = [String]()
for i in 0..<tables.staticTexts.count {
elementLabels.append (tables.staticTexts.elementBoundByIndex(i).label)
}
print (elementLabels)
My guess is the answer is there is no way to do so.
The reason is because of my experience with one of the critical iOS components while making a number of UI tests: UIDatePicker.
If you record a test where you get the page up and then you spin the picker, you will notice that the commands are all non-specific and are screen related. In the case of the picker, however, community requests resulted in the addition of a method for doing tests: How to select a picker view item in an iOS UI test in Xcode?.
Maybe you can add a helper method to whatever controller contains this table. Also, keep in mind that you can easily add methods without polluting the class interface by defining them as extensions that are in test scope only.
For Xcode 11.2.1, SwiftUI and swift 5 based app, the following code works for testing a list, each element in this case appears as a button in the test code. The table is set up like this (for each row) :
NavigationLink(destination: TopicDetail(name: "Topic name", longDesc: "A description")) {
TopicRow(thisTopic: top).accessibility(identifier: "newTopicRow_\(top.name!)")
}
Then I catch the members of the table by getting the buttons into an array:
let myTable = app.tables.matching(identifier: "newTopicTable")
var elementLabels = [String]()
for i in 0..<myTable.buttons.count {
elementLabels.append (tablesQuery.buttons.element(boundBy: i).label)
}
print (elementLabels)
Finally, I deleted each member of the table by selecting the detail view where I have a delete button, again with
.accessibility(identifier: "deleteTopic"
I wanted to delete all members of the table:
for topicLabel in elementLabels {
let myButton = app.buttons[topicLabel]
myButton.firstMatch.tap()
app.buttons["deleteTopic"].tap()
}

How to get list of child elements of an element in Appium?

I'm working with native iOS app through Appium.
I have the following structure:
UIAApplication ->
UIAWindow ->
UIATextBox
UIALabel
UIAWindow ->
SomethingElse
I have found a way to get the first UIAWindow and I'd like to get the list of all elements in that window. How do I do that?
I'd like to get UIATextBox and UIALabel from first UIAWindow but NOT SomethingElse element.
How do I do list of child elements in general?
#Test
public void testListWindows() {
List<MobileElement> windows = driver.findElementsByClassName("UIAWindow");
List<MobileElement> elements = windows.get(0).?????
}
You're almost there. What you have is giving you a list of all UIAWindows when what you want is a list of all the elements of the first UIAWindow. I'd suggest using Xpath with a wildcard.
This will get you every element that is a direct child of the first UIAWindow:
List<MobileElement> elements = driver.findElements(MobileBy.xpath("//UIAWindow[1]/*");
And this will get you every child and sub-child and sub-sub-child etc. of the first UIAWindow:
List<MobileElement> elements = driver.findElements(MobileBy.xpath("//UIAWindow[1]//*");
An extra tip: If you're automating for iOS, I assume that means you have access to OS X, where you can install the Appium dot app and use inspector. The inspector is a great tool to test out xpath queries before putting them into your code.
You can also find textfield and label by its class for iOS app in Appium. findElementsByClassName method will return all elements on current screen that matches that class.
List<MobileElement> textFields = driver.findElementsByClassName("UIATextField");
MobileElement textField = textFields.get(0);
List<MobileElement> labels = driver.findElementsByClassName("UIAStaticText");
MobileElement label = labels.get(0);

running UIAutomation script in many macs?

I could create and replay the following script very well in the mac I Use.
var target = UIATarget.localTarget();
UIATarget.localTarget().delay(15);
target.frontMostApp().mainWindow().tableViews()[0].textFields()[0].tap();
When I run the above script in another mac, It shows error in the 3rd line.
after changing the above script's third line as following, it is replaying fine.
target.frontMostApp().mainWindow().tableViews()[1].textFields()[0].tap();
Just I have changed tableview's index from 0 to 1. How can I achieve this in multiple
mac systems? Both mac are having same xcode version(xcode 5) and simulator version(6.1) and mac version.Why Instruments are taking scripts API differently in different macs?
For more consistent results one of the ways can be to access AX element by name (or other identifier/expected content/number of contained cells etc.). For example here Trouble Getting Elements by Name from UIAElementArray in UIAutomation SO question and corresponding discussion about how name for element can be set in different ways.
For example:
var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
tableViews['TableView'].textFields()[0].tap();
or
var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
tableViews['TableView'].textFields.withName("TextFieldName")[0].tap();
If using names are not practicable can be analysed content of the table and according to that right table can be selected. For example if table has some cell with name "Cell name":
var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
if (tableViews[0].cells().firstWithName("Cell name")) {
tableViews[0].textFields()[0].tap();
} else if (tableViews[1].cells().firstWithName("Cell name")) {
tableViews[1].textFields()[0].tap();
}
More details about identifying cells in tableView for example in SO question UIACollectionView cells vs visibleCells This looks not very nice but it can be reasonable workaround and should work quite reliable. If number of cells are know and different in these tables than number of cells can be compared to find required table.

Resources