我目前正在致力于一些小型移动应用程序的自动化测试,并且是第一次尝试实现Appium。
运行测试时出现以下错误 - org.openqa.selenium.NoSuchElementException:使用给定的搜索参数无法在页面上找到元素。
尝试了很多事情,但没有成功。我已经在本地安装了Appium服务器版本2.1.3,这是目前最新的。我还安装了 Appium Inspector,我可以在其中获取稍后在测试中访问的元素的资源 ID。
这是我正在尝试的测试示例
package com.example.myfood_kotlin.appiumtests.foodjokes.favorites
import io.appium.java_client.android.AndroidDriver
import io.appium.java_client.remote.AndroidMobileCapabilityType
import io.appium.java_client.remote.MobileCapabilityType
import org.junit.Test
import org.openqa.selenium.By
import org.openqa.selenium.remote.DesiredCapabilities
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
import java.net.URL
import java.time.Duration
class FavoritesRecipesAppiumTest{
@Test
fun testRecyclerViewIsDisplayed() {
// my-ip-address should be replaced with the appropriate ip - see from appium command terminal
val appiumServerUrl = URL("http://(my-ip-address):4723") // NOTE: when using appium 2.* version the default remote path is / (not /wd/hub)
val capabilities = DesiredCapabilities()
capabilities.setCapability("device", "selendroid");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android")
capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.example.myfood_kotlin")
capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.example.myfood_kotlin.ui.MainActivity")
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2")
capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, true)
capabilities.setCapability(MobileCapabilityType.NO_RESET, true)
val driver = AndroidDriver(appiumServerUrl, capabilities)
// Thread.sleep(4000)
// val wait = WebDriverWait(driver, Duration.ofSeconds(10))
// wait.until(ExpectedConditions.presenceOfElementLocated(By.id("com.example.myfood_kotlin:id/search_button")))
// Click on the favorites button (assuming it's an element with resource id)
val fragment = driver.findElement(By.id("com.example.myfood_kotlin:id/favoriteRecipesFragment"))
fragment.click()
// Verify that the RecyclerView is displayed (assuming it's an element with resource id)
// val view = driver.findElement(By.id("com.example.myfood_kotlin:id/favoriteRecipesRecyclerView"))
// view.isDisplayed
// Thread.sleep(4000)
driver.quit()
}
}
我也尝试过隐式等待、线程休眠、尝试不同的选择器,但没有任何帮助。 在检查器中,我们可以看到我尝试访问的元素的资源 ID。
注意:我使用的是在 Android Studio 中作为设备管理器的一部分创建的虚拟设备,并且在调试模式下,当涉及到获取元素的部分时,AndroidDriver 正在初始化 (在上面的示例中标记为片段) 它压碎了。
提前谢谢你:))
如果您没有放置元素的所有路径,将会有所帮助,
你能试试这个吗?
val fragment = driver.findElement(By.id("favoriteRecipesFragment"))