gpt4 book ai didi

android - Appium 无法通过 content-description/accessibilityID 找到元素

转载 作者:行者123 更新时间:2023-11-28 20:39:04 28 4
gpt4 key购买 nike

我一直在尝试使用 Java 获取 Appium 测试设置以在 Android 上进行测试。为了验证我的元素,我一直在使用 AndroidUiAutomator。在 UI Automator 查看器中,我可以看到可访问性标签已传播(见附图)。当我使用 findElement(By.name|By.AccessibilityId|etc 它通常只是超时,必须重新启动 Appium 服务器终端控制台才能运行测试。我使用 FindByXPath 得到类似的结果。我尝试使用超时和 waitdriver 等待确保元素已加载,但无济于事。我已经尝试了下面代码中列出的几乎所有内容。

        /**
* Created by appium on 12/6/16.
*/
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidElement;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.Select;
import java.util.*;



import java.net.URL;

public class FirstTest {
AndroidDriver driver;

@Before
public void setUp() throws Exception {

DesiredCapabilities capabilities = new DesiredCapabilities();


capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("app", apkloc);


capabilities.setCapability("VERSION", "5.0.0");
capabilities.setCapability("platformName", "Android");

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
// Thread.sleep(100);
//driver.manage().timeouts().implicitlyWait(100000, TimeUnit.SECONDS);

System.out.println("driver1=" + driver);


}




@Test
public void login() throws InterruptedException {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}

MobileElement mo = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"username\")");
WebDriverWait wait = new WebDriverWait(driver, 1000);
System.out.println("driver4=" + driver);
WebElement element = driver.findElement(By.xpath("//EditText[@text='username']"));
WebElement elly = driver.findElementByXPath("//[@id=wutitdo]");
System.out.println("olly-mint: "+elly);

WebElement element2 = driver.findElementByName("password");
//WebElement element3 = driver.findElementByXPath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[4]");
//WebElement element4 = driver.findElementByName("");

//WebElement element3 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[4]")));
// WebElement exp = (WebElement) (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.name("Trip")));
System.out.println("dr" + driver);
//element.click();
//WebDriverWait wait = new WebDriverWait(driver, 10);
// element2.click();
Thread.sleep(1000);
//element3.click();

//WebElement current = (new WebDriverWait(driver,100)).until(ExpectedConditions.presenceOfElementLocated(By.name("username")));
//current.sendKeys();
// WebElement dropdown = driver.findElement(By.xpath("//*[@id='wrapper']/table[2]/tbody/tr[24]/td[3]/select[1]"));

//WebElement dropdown = driver.findElement(By.xpath("//*[@id='wrapper']/table[2]/tbody/tr[24]/td[3]/select[1]"));


//Select listbox = new Select(driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[0]")));
//listbox.selectByIndex(0);
//listbox.selectByIndex(3);
//driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[3]")).click();
//Thread.sleep(2000);

//element4.click();
//Thread.sleep(1000);
}



@After
public void tearDown() throws Exception {


System.out.println("driver3=" + driver);
driver.quit();


}

}


[![enter image description here][1]][1]


[1]: /image/JPegT.png

最佳答案

您没有使用默认超时,因此如果定位器正确,appium 服务器可以获得时间来搜索元素。您应该使用显式等待来正确加载元素。

在功能中添加超时。

capabilities.setCapability("newCommandTimeout", 50);

Use these explicit wait functions based on element type if element is clickable try wait for clickable and if element is not clickable try with element’s visibility only

WebDriverWait wait = new WebDriverWait(driver, 30);

public void waitForMobileElementVisible(MobileElement element)
{
wait.until(ExpectedConditions.visibilityOf(element));

}



public void waitForMobileElementClickable(By by)
{
wait.until(ExpectedConditions.elementToBeClickable(by));
}

关于android - Appium 无法通过 content-description/accessibilityID 找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41045003/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com