gpt4 book ai didi

testing - 等待元素出现或可点击

转载 作者:行者123 更新时间:2023-11-28 21:35:34 28 4
gpt4 key购买 nike

我在尝试测试的应用程序上等待元素时遇到问题。有时Appium找不到元素。

这是我的BaseTests类

package Base;

import helpers.TestValues;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class BaseTests {
protected AndroidDriver<AndroidElement> driver;

@BeforeMethod
public void setup () throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", TestValues.devicesInfo.Honor10);
caps.setCapability("platformName", TestValues.devicesInfo.PlatformName);
caps.setCapability("platformVersion", TestValues.devicesInfo.Android810Version);
caps.setCapability("skipUnlock", TestValues.devicesInfo.SkipUnlock);
caps.setCapability("appPackage", TestValues.devicesInfo.AppPackageWageName);
caps.setCapability("appActivity", TestValues.devicesInfo.AppPackageWageActivity);
caps.setCapability("noReset","false");
driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),caps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}

@AfterMethod
public void teardown(){
driver.quit();
}
}


这是带有页面对象的MainPage类

package PageObjects;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.support.PageFactory;



public class MainPage {
private AndroidDriver<AndroidElement> driver;

public MainPage(AndroidDriver<AndroidElement> driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver) ,this);
}
@AndroidFindBy(id = "add_task_button")
private AndroidElement AddTaskButton;

@AndroidFindBy(id = "com.android.packageinstaller:id/permission_allow_button")
private AndroidElement PermissionAllowButton;

public void ClickOnAllowButton(){
PermissionAllowButton.click();
}
public boolean IsAddTaskVisible(){
return AddTaskButton.isDisplayed();
}
public void ClickOnAddTaskButton() {

AddTaskButton.click();
}

}


和测试

@Test
public void PostTask (){
_landingPage.clickOnLoginButton();
_loginPage.TypeEmail(TestValues.Credentials.CorrectEmail);
_loginPage.TypePassword(TestValues.Credentials.CorrectPassword);
_loginPage.ClickOnLoginBUtton();
_mainPage.ClickOnAllowButton();
//here is loading spinner because app fetch data from API
_mainPage.ClickOnAddTaskButton();
_mainPage.ClickOnAllowButton();
_postTaskPage.SendPhoto();
}


在大多数情况下,即使加载微调器消失了,我的测试也无法单击添加任务按钮,Appium会尝试找到它,因为我在控制台中看到了

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“id”,“selector”:“add_task_button”,“context”:"",“multiple”:false}}

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'add_task_button' using 'ID' with the contextId: '' multiple: false

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=io.wageapp.android:id/add_task_button]

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/add_task_button]

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=add_task_button]

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[DESCRIPTION=add_task_button, INSTANCE=0]

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'add_task_button' using 'ID' wi


但大多数情况下,它会因错误而失败

org.openqa.selenium.NoSuchElementException: Can’t locate an element by this strategy: By.chained({By.id: add_task_button})


但是id是正确的(有时它可以成功找到它)
https://i.postimg.cc/KjVYPnQF/2019-12-04-12-52-38-Appium.png
我究竟做错了什么?我应该如何正确处理“等待”,以使我的测试可以抵抗这种情况。

提前致谢

最佳答案

首先:当您能够找到具有其他属性的元素时,请勿使用xpath。在特定情况下,您可以通过调用driver.findElementByAccessibilityId("Add offer");找到元素

其次:如果您不想获取NoSuchElementException,请确保该元素在页面中。在这种情况下,您可以使用!driver.findElementsByAccessibilityId("Add offer").isEmpty();,如果它返回true,请找到get此元素。

注意:该方法的名称为findElement s ByAccessibilityId,并返回List<T>

第三:确保您的元素具有相应的属性,例如vivsibleclickable等(根据您的需要)

关于testing - 等待元素出现或可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59175588/

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