SwiftUI XCUIElement - How to grab XCUIElement on the screen? - swift

TAKE_A_LOOK_AT_SCREENSHOT_HERE
I'm facing an issue with the UI test written in SwiftUI.
I need to grab an element on the screen which is implemented with following the code:
HStack {
Button(action: {
if value != 0 {
value -= 1
}
}) {
Image(systemName: "minus.circle")
}
Text(String(value))
.accessibilityIdentifier("person_\(text.lowercased())_count")
Button(action: {
value += 1
}) {
Image(systemName: "plus.circle")
}
}
When I'm trying to get access to minus.circle or plus.circle the recorder prints me the following code e.g for minus:
app.windows.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .button)
.matching(identifier: "Remove").element(boundBy: 0)
My question is:
How to reduce the amount of calling .children(matching: .other) or create another correct path to get access to these buttons?
I tried to use:
var minusCircleTeens: XCUIElement {
app.windows.children(matching:.other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .other)
.element.children(matching: .button)
.matching(identifier: "Remove").element(boundBy: 0)
}
func removeTeens() {
minusCircleTeens.tap()
}
And it works properly but I would like to cut the long path to the element and do that in a better way. Any ideas?

Related

How to access variable that is in inside the method of class from top level function

I am trying to access current value of map that is in class level method from outside the function.
//This is function for call back
fireAlaram(String message_map) {
Assistant(message_map).speak();
}
//this is the function that is in stateful class
void do_backgroundTask() {
var ke = todoBox.keys.cast<int>().toList();
if (ke.length > 0) {
for (var _k in ke) {
if (todoBox.get(_k)!.time.compareTo(DateTime.now()) >= 0) {
m.addAll(todoBox.get(_k)!.toMap());
}
}
if (m.length > 0) {
products = m.keys.toList();
products.sort();
setState(() {
t = products[0];
});
AndroidAlarmManager.oneShotAt(t, 1,fireAlaram(m[t])
);
}
}
}
I want to get value of map string value when key is t, it gives null error

Loop over types in flutter/dart

Is there a way to loop over the custom types in flutter.
class Square {}
class Rectangle {}
getShape<T>() {
if(T is Square) {
return Square();
}
}
void main() {
List shapes = [Square, Rectangle];
for (dynamic T in shapes) {
getShape<T>();
}
}
In the above code, T is not getting identified.
You can use it like this:
class Square {}
class Rectangle {}
getShape(T) {
print(T is Rectangle);
if(T is Square) {
return Square();
}
}
void main() {
List shapes = [Square(), Rectangle()];
for (dynamic T in shapes) {
getShape(T);
}
}
The code produces this diagnostic because T is a variable, not a type:
Dart documentation
Either you can send it as the Function argument
getShape(T){}
or
getShape<T>(){}
then
for (final T in shapes) {
if (T == Square) {
print(getShape<Square>());
}
}
You can't really do a lot of useful things with Type objects. You can, however, use them as keys in a Map:
class Square {}
class Rectangle {}
final shapeFactories = <Type, dynamic Function()>{
Square: () => Square(),
Rectangle: () => Rectangle(),
};
void main() {
var shapes = <Type>[Square, Rectangle];
for (var T in shapes) {
var shape = shapeFactories[T]!();
print(shape.runtimeType);
}
}

android webview javascript bridge, why passed in string got resolved to the div element

In webview code it calls into the loaded javascript, passing in the string for the html div element id `"module_1", <div id="module_1" class="module"></div>:
my_web_view.evaluateJavascript(
"javascript:sendHtmlMarkerLocation("module_1");", null)
and the javascript is to accepting a string for the div element id, and then lookup the div element and get its location:
function sendHtmlMarkerLocation( moduleElementId ) {
console.log('+++ enter sendHtmlMarkerLocation()'+moduleElementId+',
window.androidObj:'+window.androidObj);
var elm = null
var moduleId = moduleElementId
if (moduleElementId instanceof String) {
elm = document.getElementById(moduleElementId)
} else if (moduleElementId instanceof Element) { // how it changes to the div element ???
elm = moduleElementId
moduleId = elm.id
}
var _x = 0;
var _y = 0;
while( elm && !isNaN( elm.offsetLeft ) && !isNaN( elm.offsetTop ) ) {
_x += elm.offsetLeft - elm.scrollLeft;
_y += elm.offsetTop - elm.scrollTop;
elm = elm.offsetParent;
}
var width = getWindowWidth()
var height = getWindowHeight()
window.androidObj.sendLocationToAndroid(moduleId, _x, _y, width, height);
}
but it crashes since the passed in moduleElementId is not the string div id "module_1" anymore, instead it is resolved to the <div id="module_1" class="module"></div>.
the coresponding console.log is:
+++ enter sendHtmlMarkerLocation()[object HTMLDivElement], window.androidObj:function AndroidClass(){} # 48
How it is changed from string id to the <div> element?

How to reorder children of QML Row (using drag and drop)?

Let's say, I have a QML Row which has some children objects (not necessarily of the same type). I would like to be able to reorder them at will. Bonus is, to be able to implement drag and drop behavior for the user. Here is a code sample, a Row with different components which I would like to reorder:
import QtQuick 2.5
import QtQuick.Controls 1.4
ApplicationWindow {
visible: true
width: 300
height: 300
Component {
id: rect
Rectangle {
color: "blue"
}
}
Component {
id: rectWithText
Rectangle {
property alias text: txt.text
color: "red"
Text {
id: txt
}
}
}
Row {
id: r
Component.onCompleted: {
rect.createObject(r,{"width": 60,"height":40})
rectWithText.createObject(r,{"width": 100,"height":40,text:"foo"})
rect.createObject(r,{"width": 40,"height":40})
}
}
}
In QtWidgets, there are some functions like layout->removeWidget(widget), layout->insertWidget(index,widget) and so on, but they seem to be missing in QML.
I found some examples that used ListView/GridView and model/delegate approach, but they usually can't handle different components and thus they are not really viable as a replacement for a Row.
Is there a way to do that in QML or am I out of luck?
This is a bit of a hack but works regardless. It does not require the use of a model.
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.1
Window {
visible: true
function shuffle(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
Column {
id: column
anchors.fill: parent
anchors.centerIn: parent
Button {
text: "shuffle"
onClicked: {
var array = [button1, button2, button3]
for (var a in array) { array[a].parent = null }
array = shuffle(array)
for (a in array) { array[a].parent = column }
}
}
Button {
id: button1
text: "I am button 1"
}
Button {
id: button2
text: "I am button 2 "
}
Button {
id: button3
text: "I am button 3"
}
}
}
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.1
Window {
visible: true
Component {
id: buttonComponent
Button { text: "I'm a button" }
}
Component {
id: labelComponent
Label { text: "I'm a label" }
}
property var buttonModel: [buttonComponent, labelComponent, buttonComponent, buttonComponent,labelComponent]
function shuffle(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
Column {
anchors.fill: parent
anchors.centerIn: parent
Repeater {
model: buttonModel
Loader {
sourceComponent: modelData
}
}
Button {
text: "shuffle"
onClicked: buttonModel = shuffle(buttonModel)
}
}
}
You can also use a ListView in the same way. That gives you even more flexibility when animating Items.

How to get FootnoteRefrence Id in OpenXML using C#

I'm having a OOXML document's Paragraph Element like this.
Now i want the FootNoteRefrence id from this text programmatically using c#.
Text From the document.xml
<w:p>
<w:r>
<w:rPr>
<w:rStyle w:val="FootnoteReference" />
</w:rPr>
<w:footnoteReference w:id="2" />
</w:r>
</w:p>
C# Code
private BodyPara writePara(BodyPara bPara2, OpenXmlElement pTag)
{
Footnotes fn = null;
foreach (var run in pTag.Descendants<Run>())
{
if (run.HasChildren)
{
foreach (var runProp in run.Descendants<RunProperties>())
{
foreach (var runStyle in runProp.Descendants<RunStyle>())
{
if (runStyle.Val != null)
{
string runSty = runStyle.Val.Value;
if (runSty == "FootnoteReference")
{
if (fn != null)
{
bPara2.FootNotes.Add(fn);
}
fn = new Footnotes();
}
else if (runSty == "CommentReference")
{
}
else
{
if (fn != null)
{
fn.FootText = fn.FootText + run.InnerText;
}
}
}
}
//FootnotesPart footnotesPart = wordDoc.MainDocumentPart.FootnotesPart;
//if (footnotesPart != null)
//{
// IEnumerable<Footnote> footnotes = footnotesPart.Footnotes.Elements<Footnote>();
// ...
//}
if (runProp.NextSibling() != null)
{
OpenXmlElement fr = runProp.NextSibling();
foreach (var fnref in fr)
{
if (fnref != null)
{
// fn.FootnoteID = fnref.Id.Value.ToString();
}
}
}
foreach (var shd in runProp.Descendants<Shading>())
{
if (shd.Fill != null)
{
string shdvalue = shd.Fill.Value;
}
}
}
}
}
return bPara2;
}
I'm using this to get Footnote Reference id of Each footnote.
In this loop i cant get the Descendants of Run of Type FootNoteReference and also its value.
Pls Help me with this.
Thank You.
Sorry I did a mistake in the parameters, Instead of using Paragraph pTag in the parameter list, i used OpenXmlElement pTag. Now i changed it from generic to specific. It works for now.