I wanted to iterate through checkboxes and tap on the checkboxes. I wrote foreach loop without using a function and its working but when I had given foreach loop inside a method its showing error as foreach not applicable to type 'io.appium.java_client.android.AndroidElement' .
Code:
For Each loop without using a method - Working
List<AndroidElement> checkbox = (List<AndroidElement>) driver.findElementsById("io.appium.android.apis:id/tasklist_finished");
for (AndroidElement check : checkbox) {
tap(check);
}
For Each loop inside a method - Not Working
List<AndroidElement> checkbox = (List<AndroidElement>) driver.findElementsById("io.appium.android.apis:id/tasklist_finished");
Checkboxes((AndroidElement) checkbox);
public static void Checkboxes(AndroidElement checkbox){
for (AndroidElement check : checkbox) { **-->Issue**
tap(check);
}
Error: java: for-each not applicable to expression type
required: array or java.lang.Iterable
found: io.appium.java_client.android.AndroidElement
foreach not applicable to type 'io.appium.java_client.android.AndroidElement'
Do not iterate over AndroidElement type, iterate over list:
List<AndroidElement> checkbox = (List<AndroidElement>) driver.findElementsById("io.appium.android.apis:id/tasklist_finished");
Checkboxes(checkbox);
public static void Checkboxes(List<AndroidElement> checkbox)
{
foreach (AndroidElement check in checkbox)
{
tap(check);
}
}
Related
I was trying to automate to select drop down option with set<WebElement> , but while iterating it gives error as NullPointerException . I same tried with List<WebElement> , It works fine.
UserPageObject.java
----------------------------
#FindAll({#FindBy(xpath ="//li[#role='option']/span[#class='ng-star-inserted']")})
private Set<WebElement> DropDownElementStatus;
public Set<WebElement> getDropDownElementStatus() {
return DropDownElementStatus;
}
public void setDropDownElementStatus(Set<WebElement> dropDownElementStatus) {
DropDownElementStatus = dropDownElementStatus;
}
ActionsUtilities.java
---------------------------
public void AllDropDownSetElements(Set<WebElement> dropDownsElements, String DropDownOption ){
Iterator<WebElement> dropDownIteratorElements= dropDownsElements.iterator(); //getting error as NullPointerException on this line
while(dropDownIteratorElements.hasNext())
{
WebElement element= dropDownIteratorElements.next();
if(element.getText().trim().equals(DropDownOption))
element.click();
}
}
UserStepDefifnation.java
-----------------------------
objectList.getActionsUtilities().AllDropDownSetElements(objectList.getUserPageObject().getDropDownElementStatus(),"INACTIVE");
Just like findElements() returns a List of WebElements:
java.util.List<WebElement> findElements​(By by)
Annotation Type FindAll() is used to mark a field on a Page Object to indicate that lookup should use a series of #FindBy tags. It will then search for all elements that match any of the FindBy criteria. However, elements are not guaranteed to be in document order.
#FindAll({#FindBy(xpath ="//li[#role='option']/span[#class='ng-star-inserted']")})
private List<WebElement> DropDownElementStatus;
It can be used on a types as well as follows:
#FindAll({#FindBy(how = How.ID, using = "foo"),
#FindBy(className = "bar")})
To conclude, #FindAll would return a List but not a Set, which you have to incorporate within yours tests.
I'm creating a list of IWebElements to access each of elements from same type, but the test works slowly when I want to access a certain element. I came with the idea to create a dictionary of elements and access each element by it's name (text that is stored in this element). I found some topics here but was unable to make them work for me. This is the way I'm accessing elements.
public IWebElement OneElement
{
get
{
return this.Driver.FindElement(By.Id("oneElement"));
}
}
public List<IWebElement> ListOfNames
{
get
{
return this.Driver.FindElements(By.Id("name")).ToList();
}
}
You can try the following code to get element by it's text and it is fast compared the list of elements as list needs to be searched till element found. it will take time if searched element is at the last position in the list. Below code will be direct fetch of particular element. Try it and let us know.
public IWebElement GetElementByText(String text)
{
get
{
return this.Driver.FindElement(By.xpath(String.Format("//*[#id='name'][text()='{0}']",text)));
}
}
I'm trying to sort a Documents Collection using a java.util.ArrayList.
var myarraylist:java.util.ArrayList = new java.util.ArrayList()
var doc:NotesDocument = docs.getFirstDocument();
while (doc != null) {
myarraylist.add(doc)
doc = docs.getNextDocument(doc);
}
The reason I'm trying with ArrayList and not with TreeMaps or HashMaps is because the field I need for sorting is not unique; which is a limitation for those two objects (I can't create my own key).
The problem I'm facing is calling CustomComparator:
Here how I'm trying to sort my arraylist:
java.util.Collections.sort(myarraylist, new CustomComparator());
Here my class:
import java.util.Comparator;
import lotus.notes.NotesException;
public class CustomComparator implements Comparator<lotus.notes.Document>{
public int compare(lotus.notes.Document doc1, lotus.notes.Document doc2) {
try {
System.out.println("Here");
System.out.println(doc1.getItemValueString("Form"));
return doc1.getItemValueString("Ranking").compareTo(doc2.getItemValueString("Ranking"));
} catch (NotesException e) {
e.printStackTrace();
}
return 0;
}
}
Error:
Script interpreter error, line=44, col=23: Error calling method
'sort(java.util.ArrayList, com.myjavacode.CustomComparator)' on java
class 'java.util.Collections'
Any help will be appreciated.
I tried to run your SSJS code in a try-catch block, printing the error in exception in catch block and I got the following message - java.lang.ClassCastException: lotus.domino.local.Document incompatible with lotus.notes.Document
I think you have got incorrect fully qualified class names of Document and NotesException. They should be lotus.domino.Document and lotus.domino.NotesException respectively.
Here the SSJS from RepeatControl:
var docs:NotesDocumentCollection = database.search(query, null, 0);
var myarraylist:java.util.ArrayList = new java.util.ArrayList()
var doc:NotesDocument = docs.getFirstDocument();
while (doc != null) {
myarraylist.add(doc)
doc = docs.getNextDocument(doc);
}
java.util.Collections.sort(myarraylist, new com.mycode.CustomComparator());
return myarraylist;
Here my class:
package com.mycode;
import java.util.Comparator;
public class CustomComparator implements Comparator<lotus.domino.Document>{
public int compare(lotus.domino.Document doc1, lotus.domino.Document doc2) {
try {
// Numeric comparison
Double num1 = doc1.getItemValueDouble("Ranking");
Double num2 = doc2.getItemValueDouble("Ranking");
return num1.compareTo(num2);
// String comparison
// return doc1.getItemValueString("Description").compareTo(doc2.getItemValueString("Description"));
} catch (lotus.domino.NotesException e) {
e.printStackTrace();
}
return 0;
}
}
Not that this answer is necessarily the best practice for you, but the last time I tried to do the same thing, I realized I could instead grab the documents as a NotesViewEntryCollection, via SSJS:
var col:NotesViewEntryCollection = database.getView("myView").getAllEntriesByKey(mtgUnidVal)
instead of a NotesDocumentCollection. I just ran through each entry, grabbed the UNIDs for those that met my criteria, added to a java.util.ArrayList(), then sent onward to its destination. I was already sorting the documents for display elsewhere, using a categorized column by parent UNID, so this is probably what I should have done first; still on leading edge of the XPages/Notes learning curve, so every day brings something new.
Again, if your collection is not equatable to a piece of a Notes View, sorry, but for those with an available simple approach, KISS. I remind myself frequently.
Is there a way in the latest version of Selenium DotNet Webdriver (2.22.0) to check to see if an element is visible before clicking/interacting with it?
The only way I've found is to try to handle the ElementNotVisible exception that occurs when you try to send keys, or click on it. Unfortunately this only occurs after an attempt to interact with the element has been made. I'm using a recursive function to find elements with a certain value, and some of these elements are only visible in certain scenarios (but their html is still there no matter what, so they can be found).
It's my understanding that the RenderedWebElement class is deprecated as well other variants. So no casting to that.
Thanks.
For Java there is isDisplayed() on the RemoteWebElement - as well is isEnabled()
In C#, there is a Displayed & Enabled property.
Both must be true for an element to be on the page and visible to a user.
In the case of "html is still there no matter what, so they can be found", simply check BOTH isDisplayed (Java) / Displayed (C#) AND isEnabled (Java) / Enabled (C#).
Example, in C#:
public void Test()
{
IWebDriver driver = new FirefoxDriver();
IWebElement element = null;
if (TryFindElement(By.CssSelector("div.logintextbox"), out element)
{
bool visible = IsElementVisible(element);
if (visible)
{
// do something
}
}
}
public bool TryFindElement(By by, out IWebElement element)
{
try
{
element = driver.FindElement(by);
}
catch (NoSuchElementException ex)
{
return false;
}
return true;
}
public bool IsElementVisible(IWebElement element)
{
return element.Displayed && element.Enabled;
}
It seems the current answer to this question is outdated: With WebDriver 3.13 both the Displayed and Enabled properties will return true as long as the element exists on the page, even if it is outside of the viewport. The following C# code works for WebDriver 3.13 (from this StackOverflow answer):
{
return (bool)((IJavaScriptExecutor)Driver).ExecuteScript(#"
var element = arguments[0];
var boundingBox = element.getBoundingClientRect();
var cx = boundingBox.left + boundingBox.width/2, cy = boundingBox.top + boundingBox.height/2;
return !!document.elementFromPoint(cx, cy);
", element);
}
There is a simple way to do that, follow below:
public bool ElementDisplayed(By locator)
{
new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(condition: ExpectedConditions.PresenceOfAllElementsLocatedBy(locator));
return driver.FindElement(locator).Displayed ;
}
You can use the following:
WebDriver web = new FirefoxDriver(;
String visibility = web.findElement(By.xpath("//your xpath")).getCssValue("display");
I have a remote function testdwr, which returns a list of objects(test). How should i parse the list of objects in the handler method? Code shown below
public List testdwr(String message) {
Test test = new Test();
test.setName("mahati");
List arrayList = new ArrayList();
arrayList.add(test);
return arrayList;
}
handler method:
function update()
{ findaccounts.testdwr("somestring : ",function(data){
alert(data); }
the alert box gives the output as "object Object"!!
The ArrayList returned from the server will looks like,
[Object { name="Mahati"}, Object { name="meena"}, Object { name="keerthi" }.....]
You can have a loop like this,
for(var i=0; i<arrayList.length; i++)
{
var testObj = arrayList[i];
//Here, you can do what you want! like...
alert(testObj.name);
alert(testObj.age);
}
Like Blake said, try to use FireBug, its an addon for firefox.
You should be able to do something like data[0].name
Using alert(data.length) show show 1.
Have you tried using Firebug on Firefox. It allows you to set a break point in the javascript code and examin the variables.