AppiumLibrary: Element Should Not Be Visible Keyword? - appium

In AppiumLibrary, as of version 1.4.5, a very handy keyword called Element Should Be Visible has been introduced. But I am looking for the opposite version of that keyword too, which would be something like Element Should Not Be Visible. Since AppiumLibrary doesn't have that yet, is there any way we could achieve this?

There's always the possibility of expanding the library's python code, this shouldn't be very difficult. You basically just need to clone the Element Should Be Visible keyword definition and change the condition to do the opposite.
But, if that's not a possibility for you, perhaps you could use the keyword Run Keyword And Expect Error, in combination with the keyword you mentioned, Element Should Be Visible. Using this keyword on an element that isn't visible would throw an error, which in this particular case would be the desired outcome.
Still, that's a fishy workaround which will not help the readability of your test, and you should consider first looking into expanding the library itself.

Thanks to Verv for the guidelines. I tried both approaches that he suggested, and both seem to be super easy. For future reference, I'm explaining both methods here.
Method 1:
Under AppiumeLibrary/keywords directory, there's a file called _element.py, which defines the Element Should Be Visible keyword. I was able to clone it to create the new keyword I was looking for.
Below is the code snippet that defines Element Should Be Visible
def element_should_be_visible(self, locator, loglevel='INFO'):
"""Verifies that element identified with locator is visible.
Key attributes for arbitrary elements are `id` and `name`. See
`introduction` for details about locating elements.
New in AppiumLibrary 1.4.5
"""
if not self._element_find(locator, True, True).is_displayed():
self.log_source(loglevel)
raise AssertionError("Element '%s' should be visible "
"but did not" % locator)
Next to the above code snippet, you can add the following code snippet to define a new keyword Element Should Not Be Visible
def element_should_not_be_visible(self, locator, loglevel='INFO'):
"""Verifies that element identified with locator is visible.
Key attributes for arbitrary elements are `id` and `name`. See
`introduction` for details about locating elements.
New in AppiumLibrary 1.4.5
"""
if self._element_find(locator, True, True).is_displayed():
self.log_source(loglevel)
raise AssertionError("Element '%s' should not be visible "
"but did" % locator)
Method 2
If you don't want to expand the existing library, you could just use the combination of existing keywords as follows:
${isVisible}= Run Keyword And Return Status Element Should Be Visible 'someElementSelector'
Should Be Equal ${isVisible} ${FALSE}

Related

Structuring Wireshark dissector to make filtering easier

I am writing my first Wireshark dissector. I am writing it in Lua, using this as an example. My communication protocol embeds a command ID in the response header, followed by well-defined payloads that differ based on the command ID. So far, I've been structuring the ProtoFields such that the Abbreviated name of the field (the string used in filters) follows a naming convention like this
proto_name.command_name.field_name
Some commands have similar fields, like in the following example
myproto.cmd_update.updateId
myproto.cmd_update_ack.updateId
where, per the protocol, an update command must be acknowledged with a update_ack command with the same updateId payload. Ideally, i would like to create a wireshark filter such that I can see all packets pertaining to the updateId field. I tried creating a filter like
myproto.*.updateId == 0x1234
but that appears to be invalid wireshark filter syntax. I don't want to make the filter explicit like
myproto.cmd_update.updateId == 0x1234 or myproto.cmd_update_ack.updateId == 0x1234
because in my actual protocol there are many more commands with similar/related fields. Is there a filter syntax I can use? Or perhaps, should I structure my dissector's ProtoField abbreviations differently?
There doesn't appear to be a wildcard syntax for the filter line, so I wound up solving this in the dissector itself. In addition to the myproto.*.updateId fields, I also added another field called myproto.updateId (note the lack of the wildcard in the middle). Its value is set to the same thing as the full blown field name, which means that I now have just one field name to search against. I also set this field as hidden = true to hide it from view.
It's a bit of a hack, but gives me what I want.
You could try using a Wireshark display filter macro.

Can a <name> tag have a #nullValue and still have text inside?

The current implementation of a CCDA generator I'm working on, prints a message on a <name> tag (in header sections, where no <text> is available) when something's name is not found:
<name>No information</name>
I know the right way to express not found information is through the #nullFlavor attribute:
<name nullFlavor="NI" />
But right now there is a component on the application that reads the value on the tag and shows it in a human-readable view of the CCDA document. If I use #nullflavor only, the field that shows such name will be empty, instead of "No information".
In order to avoid changing such component, I was thinking on adding the #nullFlavor attribute but still letting the message there:
<name nullFlavor="NI">No information</name>
I know this is syntactically correct, because I've tested it with the reference validator and it passes. My question is: from a semantic point of view, is it valid?
Yes it's valid. The particular specification in question - the v3 abstract data types, simple says:
invariant(ST x) where x.nonNull {
x.headCharacter.notEmpty;
};
So if there's no nullFlavor, there must be some content. But the reverse rule is not applied; there can be content if there's a nullFlavor
Although it is not restricted, my point of view is that it is not a good strategy. I understand that you have a restriction regarding this component but, when you are building a CDA, it is important to keep in mind that it is something to be shared with everyone, and I would never expect to find content inside a nullFlavor attributed element.

In Appium 1.0+, now that finding elements by tag is deprecated, how do I find an element by tag name when operating on the DOM of the webview?

Once I switch my context to the DOM of the webview, I want to be able to search those elements by tag, but I get the error that searching by tag is deprecated and to search by class instead. This won't work to find DOM elements by tag. Is there still a way to do it? Thanks!
As per Appium documentation for migrating to 1.0:
We've removed the following locator strategies:
-name
-tag name
... tag name has been replaced by class name. So to find an element by its
UI type, use the class name locator strategy for your client.
Why searching by tag name?
Although Selenium still supports this type of query, Appium decided not to do anymore. Actually when interacting with the device, searching by tag name is very inefficient.
Why would you want to do that? Think about it, if your page has a bit of content, you will end up having many p, div, span tags. Your search will return many elements and then you will have to go thorugh the list and locate the one you are interested in. If your page is very little, then you will probably end up with one tag of the type you are looking for, however why not applying a class and solve the problem?
Classes are not for CSS style
Remember that HTML attribute class was not introduced by W3C for applying CSS style. It is used to provide an element with more informationa bout its purpose in the DOM. When you apply a class to an element, you should do that basing on the role that element holds! Thus locating an element by class is sure better.
So forget searching by tag name. You should change your strategy and apply class names to your tags in your hybrid app. If you do not want to do so, then do not switch to the new version of Appium but this will keep you far from future innovations!
Migrating from a tagname based element location to a class name
orientd one is good practice. That's why you should change too.
maybe this can help
element.getAttribute("class")

Directly modify a specific item in a TKinter listbox?

This is one of those just-making-sure-I-didn't-miss-anything posts.
I have a TKinter GUI in Python 2.7.3 that includes a listbox, and there are circumstances where I'd like to directly modify the text of a specific item at a known index. I've scoured the documents and there's no lb.itemset() method or anything like it. As best I can tell I have two options, either of which would work but just seem kind of klunky to me:
lb.delete() the old item and lb.insert() the new value for it at the same index (including a step to re-select the new value if the old deleted one happened to be selected).
Create a listvariable for the listbox, then use get() and set() on it -- with a pile of replace/split/join acrobatics in between to handle the differing string formats involved.
Is there some simpler, more direct way to do it that I'm missing? Or have I turned up all the available options?
Assuming from silence that there's nothing I missed. I went with option 2 -- the acrobatics weren't quite as complex as I'd thought. I just created a behind-the-scenes list wrapped up in a class; every time I update the list, the class syncs up the content of the listbox by doing a ' '.join on the list then setting the listbox's listvariable to the resulting string.

Ruby REXML: Get Value Of An XML Element

I am trying to put the values of some xml elements into an array using rexml. Here is an example of what I am doing:
doc = Document.new("<data><title>This is one title</title><title>This is another title</title></data>")
XPath.each( doc, "*/title") { |element|
puts element.text
}
However, that outputs:
[<title> ... </>, <title> ... </>]
How can I get it to output an array containing "This is one title" and "This is another title"?
Moving my comment to an answer, per request:
While puts may convert its argument its argument to a string anyway, you can have the XPath return the text node in the first place:
XPath.each(doc, "*/title/text()") {...
Are you sure about that? Here's a complete program:
#!/usr/bin/ruby
require 'rexml/document'
include REXML
doc = Document.new("<data><title>This is one title</title><title>This is another title</title></data>")
XPath.each( doc, "*/title") { |element|
puts element.text
}
Output:
This is one title
This is another title
Edit: It sounds like the OP has moved on, but I think there should be some clarification added here for future visitors. I upvoted #LarsH's good answer, but it should be noted that, given the OP's specific input, element.text should produce exactly the same output as would result from selecting the text() nodes in the first place. From the docs:
text( path = nil )
A convenience method which returns the String value
of the first child text element, if one exists, and nil otherwise.
The sample input given in the original question shows <title> elements containing only one text node in each case. Therefore, these two methods are the same (in this case).
However, pay attention to this important note:
Note that an element may have multiple Text elements, perhaps
separated by other children. Be aware that this method only returns
the first Text node.
You can get all of an element's child text nodes using texts() (plural).
What I suspect a lot of people are really looking for is an equivalent of the DOM's textContent (or its illegitimate cousin innerText). Here's how you might do that in Ruby:
XPath.each(doc, "*/title") { |el|
puts XPath.match(el,'.//text()').join
}
This joins all of the text descendants of each element into a single string.
The short answer is that there's no short answer. Which one of these you want, if any, is highly context-specific. The only requirement in the original question is to "put the values of some xml elements into an array", which isn't really much of a specification.

Resources