Appium Find Element: Search for an Element.
-
Find the first
WebElement
using the given method. - This method is affected by the 'implicit wait' times in force at the time of execution.
- The findElement(..) invocation will return a matching row, or try again repeatedly until the configured timeout is reached.
-
FindElement should not be used to look for non-present elements,
use
findElements(By)
and assert zero length response instead.
Example:
MobileElement elementOne =driver.findElementByAccessibilityId("SomeAccessibilityID");
MobileElement elementTwo = driver.findElementByClassName("SomeClassName");
Below is the java program with findElement:
package practise;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import utils.extentReports.ExtentTestManager;
public class FindElementExample {
static AppiumDriver<MobileElement> driver;
@Test
public void setup(Method method) throws InterruptedException, MalformedURLException {
ExtentTestManager.startTest(method.getName(), "Setup Method");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "emulator-5554");
cap.setCapability("udid", "emulator-5554");
cap.setCapability("appActivity", "com.android.calculator2.Calculator");
cap.setCapability("appPackage", "com.android.calculator2");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9.0");
driver = new AppiumDriver<MobileElement>(new URL("http://127.0.0.2:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Thread.sleep(5000);
driver.findElement(By.id("com.android.calculator2:id/digit_1")).click();
driver.findElement(By.xpath("//*[@text='1']")).click();
}
}
Appium Find Elements: This command will be used to search for multiple elements
- Find all elements within the current context using the given mechanism.
- When using xpath be aware that webdriver follows standard conventions: a search prefixed with "//" will search the entire document, not just the children of this current node.
- Use ".//" to limit your search to the children of this WebElement.
- This method is affected by the 'implicit wait' times in force at the time of execution.
- When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.
Example:
List<MobileElement> elementsOne = driver.findElementsByAccessibilityId("SomeAccessibilityID"); List<MobileElement> elementsTwo =
driver.findElementsByClassName("SomeClassName");
Below is the java program with findElements Example:
package practise;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import utils.extentReports.ExtentTestManager;
public class FindElementsExample {
static AppiumDriver<MobileElement> driver;
@Test
public void setup(Method method) throws InterruptedException, MalformedURLException {
ExtentTestManager.startTest(method.getName(), "Setup Method");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "emulator-5554");
cap.setCapability("udid", "emulator-5554");
cap.setCapability("appActivity", "com.android.calculator2.Calculator");
cap.setCapability("appPackage", "com.android.calculator2");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9.0");
driver = new AppiumDriver<MobileElement>(new URL("http://127.0.0.2:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
List<MobileElement> elementsOne =driver.findElementsByAccessibilityId("SomeAccessibilityID");
List<MobileElement> elementsTwo =driver.findElementsByClassName("SomeClassName");
}
}
Final Note:
- FindElement is used for finding single element, if multiple elements exist, will throw the exception.
- FindElements is used for finding more than one element. If single element exist, still statement will execute
No comments:
Post a Comment
If any suggestions or issue, please provide