Karate Robot - how to fetch element properties? - karate

I have been using the Karate framework for a while now and more recently its Robot component for desktop UI automation. I'm curious if there's a way to fetch the value of a given element property. Following this example from the documentation:
* def fun = function(){ return optional('Close').enabled }
* waitUntil(fun)
I'd like to be able to fetch the value of IsOffscreen, IsControlElement, etc.
Any suggestions will be appreciated.

That's a good question, so I've just added a way to do it, but unfortunately can't test it just yet: https://github.com/intuit/karate/tree/develop/karate-robot#property-value
* def button = locate('Close')
* def isOffScreen = button.property('IsOffscreen')
Would you be able to build this locally and test, that would really help. There is am easy developer guide; https://github.com/intuit/karate/wiki/Developer-Guide

Related

How to iterate over an element (node) list on Android Jetpack Compose UI Tests?

I'm implementing some instrumented tests using the Jetpack Compose testing library. I'm not too familiar with Kotlin / Android development yet, but I have years of experience with Selenium and other testing libraries, so I'm missing some basic things and have no idea how to implement them.
What I want to do:
Iterate over an element (node) list. I have this list an all items are identified by the same test tag "item". I need to click on each one of these items.
On Selenium I can easily do that:
elements = driver.find_elements("item")
elements.each do |element|
element.click
end
But on Kotlin with the Composing Testing Framework I have no clue how to do that. The method below (responsible for returning a list of nodes) doesn't support forEach:
composeTestRule.onAllNodes(hasTestTag("item")
I also want to retrieve the list size.
On Selenium the method below returns the qty of items found:
driver.find_elements("item").size
But, again, there's nothing like that available with composing:
composeTestRule.onAllNodes(hasTestTag("item")
I've already read the official JetPack Compose Testing Tutorial, but it doesn't provide much details
I'm not sure how you'd go about iterating over a SemanticsNodeInteractionCollection. I'm also uncertain of why you'd want to do that. That said, in a testing scenario you'd likely have an expected count of items in a collection. Therefore, you can create a range and get the SemanticsNodeInteraction for each element that .OnAllNodes() returns.
Example where I expect there to be 10 ui elements returned:
val nodes = composeTestRule.onAllNodes(hasTestTag("item"))
for (index in 0..10) {
val node = nodes[index]
// node.assert whatever you want here.
}
Asserting the count equals something can also be done through:
composeTestRule.onAllNodes(hasTestTag("item")).assertCountEquals(10)
If you just want to get the total count and not assert it. I'd argue there might be something wrong with the tests themselves. I'd expect your test to be a controlled environment where you know exactly how many items should be shown on the screen at any given time.
Let me know if this helps, otherwise please elaborate on your exact scenario.
To iterate:
composeTestRule.onAllNodes(hasTestTag("item")).apply {
fetchSemanticsNodes().forEachIndexed { i, _ ->
get(i).performClick()
}
}
To check size:
composeTestRule.onAllNodes(hasTestTag("item")).fetchSemanticsNodes().size == 1

Swithcpage or new tab is not working in karate framework - it is always pointing to first page [duplicate]

I am having an issue with switching tabs using Karate UI. My code is as follows:
Scenario: SwitchPage Test
Given driver 'original URL'
* retry(5, 10000).waitFor('#someID')
* input('#someID', ['numbers', 'input', Key.ENTER]) // this will open the new page
* print driver.title \\ this prints the original title of the original URL
* switchPage('NewURL')
* delay(10000) // I've put this just in case but it doesn't work without it either
Then print driver.title // this still prints the original title of the original URL
Any help would be really appreciated, I think it is a great tool but I'm having difficulty with this scenario and our application opens new tabs with every module.
Thank you
Yes I know this can be hard. In 0.9.6 we have an option to switch tab by a) index and b) sub-string of the URL of that tab
Do consider submitting a simple example that can help us simulate and improve Karate if needed: https://github.com/intuit/karate/tree/develop/examples/ui-test
Finally I recommend this option if possible. If there is some way for you to derive the URL of the tab that will get opened, try to re-use the current tab, and that makes for a simpler test. See this thread for more details: https://github.com/intuit/karate/issues/1269#issuecomment-682553602
Also see: https://stackoverflow.com/a/62727612/143475

How to switch to a window (NOT TAB) in karate? [duplicate]

I am having an issue with switching tabs using Karate UI. My code is as follows:
Scenario: SwitchPage Test
Given driver 'original URL'
* retry(5, 10000).waitFor('#someID')
* input('#someID', ['numbers', 'input', Key.ENTER]) // this will open the new page
* print driver.title \\ this prints the original title of the original URL
* switchPage('NewURL')
* delay(10000) // I've put this just in case but it doesn't work without it either
Then print driver.title // this still prints the original title of the original URL
Any help would be really appreciated, I think it is a great tool but I'm having difficulty with this scenario and our application opens new tabs with every module.
Thank you
Yes I know this can be hard. In 0.9.6 we have an option to switch tab by a) index and b) sub-string of the URL of that tab
Do consider submitting a simple example that can help us simulate and improve Karate if needed: https://github.com/intuit/karate/tree/develop/examples/ui-test
Finally I recommend this option if possible. If there is some way for you to derive the URL of the tab that will get opened, try to re-use the current tab, and that makes for a simpler test. See this thread for more details: https://github.com/intuit/karate/issues/1269#issuecomment-682553602
Also see: https://stackoverflow.com/a/62727612/143475

Is #FindBy (id = 'blabla') same thing as //*[#id='blabla]

I decided to give a try to Katalon and got this question because Katalon Studio, when user choose "locate by attribute 'id' " it shows that //*[#id='blabla'] will be used to locate element. But as for me it looks like variation of xPath. So i wonder, when in JAVA - Selenium i write #findBy (id='blabla') - under the hood selenium convert it to //*[#id = 'blabla'] ???
Could`t find answer
Google, forums, katalon docs
UPDATE:
I want to know if this two methods are exactly the same. 1. Will it take same amount of time to find element? 2. Are both of methods going to start looking for element from top of the DOM ?
Functionally these all do the same thing:
The #FindBy(id = "foo") annotation
driver.findElement(By.id("foo"))
driver.findElement(By.xpath("//*[#id = 'foo']"))
driver.findElement(By.css("#foo"))
They all find an element by its Id.
I'm not sure what that translates to in API calls to the Selenium Server, but Selenium is open source. Have a look for yourself.
The #FindBy(...) annotation is defined in FindBy.java, which inherits from AbstractFindByBuilder. Trace through the code and you'll figure it out.
It is the equivalent. (But it could depend on the particular Selenium bindings)
If to look into By class of Selenium Java library there you can see the code like this:
public List<WebElement> findElements(SearchContext context) {
return context instanceof FindsById ? ((FindsById)context).findElementsById(this.id) : ((FindsByXPath)context).findElementsByXPath(".//*[#id = '" + this.id + "']");
}
A single element lookup just reuses lookup of a collection.

Getting description using selenium xpath

I am trying to get the job description for job search page indeed.com This is how it looks like
Provide technical leadership around
QA
automation to IT teams. Work with various team to promote
QA
processes, practices and standardization....
Any idea how can I get that description? I tried the following:
//span[contains(#class,'summary')]
That does not give me the text description. Should I xpath or is there any other solution? Thanks in advance for your time.
This XPath are correct.
//span[contains(#class,'summary')]
//span[#class='summary']
I'm a Python guy, But I translated it to Java. You can do:
element = driver.findElement(By.name("summary"));
element = driver.findElement(By.className("summary"));
element = driver.findElement(By.cssSelector('span[class="summary"]');
And remember that If you want the element text, every element has the method .getText(), the find* functions only retrieve the element/s.
Double check you were not using driver.findElements(By.xpath()) in plural. In that case you should first retrieve the individual elements. Then access to the .getText() method.
description = driver.findElement(By.className("summary")).getText();
System.out.print(description);
Alternatively you could do:
description = driver.findElement(By.className("summary"));
description_text = description.getAttribute("innerHTML");
System.out.print(description_text);
If your problem is that your element is not visible or reachable (stale). Then you can use javascript.
element = driver.executeScript("return document.querySelector('span[class=\"summary\"]');");
For more reference:
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html