appium 測驗是對幾個輸入欄位進行某種組合,其中一個欄位是密碼欄位。
如果用戶名已被占用,則密碼按鈕下方將顯示相應的訊息“用戶名已被占用”。但如果用戶名有效,但密碼錯誤,則會在下方顯示“用戶名和密碼組合無效”的訊息。
上述情況下的錯誤資訊顯示在android.widget.TextView**
兩個問題:
- 我使用 Appium Studio 錄制了測驗。當我運行測驗時,我想等待幾秒鐘并獲取錯誤訊息的文本。如何做等待部分并抓取文本?(更多詳情見下文)。挑戰在于 textView 沒有資源 ID,但可以使用 xpath 和其他詳細資訊。
注意:由于錯誤訊息顯示在 pwd 欄位的正下方,因此它沒有 resouceid,只有 xpath 可用。
這是結構
<android.widget.LinearLayout resource-id="com.a.b/textinputlayout_lgin_username">
<android.widget.FrameLayout>
<android.widget.EditText resource-id="com.a.b/lgin_username_edit">
<!--above 3 lines are for username field, just added for more clarity -->
<android.widget.LinearLayout resource-id="com.a.b:id/textinputlayout_lgin_pwd">
<android.widget.FrameLayout>
<android.widget.EditText>
<android.widget.ImageButton resource-id="com.a.b/text_input_end_icon">
<android.widget.TextView> <!-- ** this is the text I'm trying to grab -->
<android.widget.TextView resource-id="com.a.b/tv_forgotpwd">
<android.widget.Button resource-id="com.a.b/btn_login">
請幫忙。謝謝。
uj5u.com熱心網友回復:
我假設您無法更新應用程式以添加元素的資源 ID。
在這種情況下,您可以做的最好的事情是使用 xpath。
通過 resorce-id 屬性找到第一個 parent,然后往下按 tag 搜索。我在 LinearLayout 中看到 2 個文本標簽元素,因此您可以嘗試通過索引獲取第一個文本標簽,或者搜索沒有資源 id 屬性的文本標簽。
"(//android.widget.LinearLayout[@resouce-id='com.a.b:id/textinputlayout_lgin_pwd']//android.widget.TextView)[1]"
或者
"//android.widget.LinearLayout[@resouce-id='com.a.b:id/textinputlayout_lgin_pwd']//android.widget.TextView[not(@resouce-id)]"
基于您使用的 appium 客戶端,只需利用 webdriver 等待 預期條件并為元素使用 xpath 位置策略。
PS:這是代碼
public class Test_9 {
private String reportDirectory = "reports";
private String reportFormat = "xml";
private String testName = "Untitled";
protected AndroidDriver<AndroidElement> driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
@BeforeEach
public void setUp() throws MalformedURLException {
dc.setCapability("reportDirectory", reportDirectory);
dc.setCapability("reportFormat", reportFormat);
dc.setCapability("testName", testName);
dc.setCapability(MobileCapabilityType.UDID, "123456");
dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.a.b");
dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".view.base.SplashActivity");
driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), dc);
driver.setLogLevel(Level.INFO);
}
@Test
public void testUntitled() {
driver.findElement(By.xpath("(//*[@contentDescription='Google Map']/*[@class='android.view.View'])[26]")).click();
driver.findElement(By.xpath("//*[@text='log in']")).click();
driver.findElement(By.xpath("//*[@id='lgin_edit']")).sendKeys("1234567789");
driver.findElement(By.xpath("//*[@class='android.widget.EditText' and (./preceding-sibling::* | ./following-sibling::*)[@id='text_input_end_icon']]")).sendKeys("qwer");
new WebDriverWait(driver, 120).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@class='android.widget.EditText' and (./preceding-sibling::* | ./following-sibling::*)[@id='text_input_end_icon']]")));
driver.hideKeyboard();
driver.findElement(By.xpath("//*[@class='android.widget.EditText' and (./preceding-sibling::* | ./following-sibling::*)[@id='text_input_end_icon']]")).click();
driver.findElement(By.xpath("//*[@text='Log In']")).click();
driver.findElement(By.xpath("//*[@text='Log In']")).click();
}
@AfterEach
public void tearDown() {
//driver.quit();
}
}
uj5u.com熱心網友回復:
好的,這就是解決我的問題的原因。
發布 questino 后我意識到,該按鈕未啟用,這是因為未單擊先前的輸入文本欄位或其他內容。
這段時間我都是通過Appium錄制的,但是我是手動點擊手機上的鍵盤和按鈕,而不是appium的手機模擬器。
2.5天后,我用我的電腦滑鼠指標點擊了appium模擬器上的所有欄位和所有模擬,記錄并重新運行測驗,它起作用了。
我確實嘗試列印出按鈕狀態,但總是 btn.isEnabled() 回傳 false。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403856.html
標籤:
