org.openqa.selenium.NoSuchElementException 具有 webview 上下文的 Appium

org.openqa.selenium.NoSuchElementException Appium with webview context

提问人:Андрей Медведев 提问时间:10/6/2023 更新时间:10/6/2023 访问量:16

问:

我们正在使用 Appium for Android 应用程序创建自动测试。我们的应用程序有几个 Web 视图页面。 重定向到 webview 页面后 - 我更改了上下文,但仍然无法使用 webview 元素。

    @test
    @DisplayName("DAF-T14 Check feedback form after clicking it from profile page")
    public void ProfileAppFeedbackFlow() throws Exception {

    String expectedFeedbackTitle = "Feedback";
    String expectedFeedbackWebTitle = "What are your experiences with the DKV app?";
    String expectedDescription = "You have noticed incorrect station information? Please repor";

    var onboardingPage = new OnboardingPage();
    var onboardingLinearLayout = onboardingPage.clickSkipButton();
    var locationPopUp = onboardingLinearLayout.clickLaterButton();
    var analysisPopUp = locationPopUp.clickAllowAllTimeButton();
    analysisPopUp.clickActivateButton();
    var feedbackProfilePage = new DoYouLikePopUp().clickNoButton();
    Assertions.assertEquals(feedbackProfilePage.getFeedbackTittle(), expectedFeedbackTitle);

现在我正在检查并将上下文更改为 webview

    Set strcontext = null;
    try{
    strcontext = ((AndroidDriver) AppiumInit.getAppiumDriver()).getContextHandles();
    System.out.println("Found contexts size: '" + strcontext.size() + "'");
    for (String s :strcontext) {
    System.out.println("Found context: '" + s + "'");
    if (s != null && s.contains("WEBVIEW")) {
    System.out.println("Switching context to: '" + s + "'");
    ((AndroidDriver) AppiumInit.getAppiumDriver()).context(s);
    break;
    }
    }
    } catch (Exception e){
    e.printStackTrace();
    }

检查上下文是否为 webview

    String currentContext = ((AndroidDriver) AppiumInit.getAppiumDriver()).getContext();
    System.out.println("Current Context: " + currentContext);

并检查标题文本

   Assertions.assertEquals(feedbackProfilePage.getFeedbackWebTitl(), expectedFeedbackWebTitle); 

哪里

    public String getFeedbackWebTitl() {
    WebElement feedbackWebTitle = driver.findElement(By.xpath("//span[@id='title']"));
    return feedbackWebTitle.getText();
    }

我可以在日志中看到什么:

Found contexts size: '2'
Found context: 'NATIVE_APP'
Found context: 'WEBVIEW_com.dkveuroservice.mobileappkit'
Switching context to: 'WEBVIEW_com.dkveuroservice.mobileappkit'
Current Context: WEBVIEW_com.dkveuroservice.mobileappkit
<html><head></head><body></body></html>

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//span[@id='title']”} (会话信息:chrome=103.0.5060.71) 有关此错误的文档,请访问: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception 内部版本信息:版本:“4.10.0”,修订版本:“c14d967899” 系统信息: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '13.5.1', java.version: '17.0.6' 驱动程序信息:io.appium.java_client.android.AndroidDriver 命令:[629bae0e-84be-463f-86a7-49e1c58ddbcb, findElement {using=xpath, value=//span[@id='title']}] 功能 {appium:app: //Users//nifargo//Documents..., appium:appActivity: com.dkveuroservice.mobileap..., appium:appPackage: com.dkveuroservice.mobileap..., appium:automationName: UiAutomator2, appium:databaseEnabled: false, appium:desired: {app: //Users//nifargo//Documents..., appActivity: com.dkveuroservice.mobileap..., appPackage: com.dkveuroservice.mobileap..., automationName: UiAutomator2, deviceName: autoTests, newCommandTimeout: 800, platformName: ANDROID},appium:deviceApiLevel: 33, appium:deviceManufacturer: Google, appium:deviceModel: sdk_gphone64_arm64, appium:deviceName: emulator-5554, appium:deviceScreenDensity: 560, appium:deviceScreenSize: 1440x2560, appium:deviceUDID: emulator-5554, appium:javascriptEnabled: true, appium:locationContextEnabled: false, appium:networkConnectionEnabled: true, appium:newCommandTimeout: 800, appium:pixelRatio: 3.5, appium:platformVersion: 13, appium:statBarHeight: 84, appium:takesScreenshot: true, appium:viewportRect: {height: 2392, left: 0, top: 84, width: 1440}, appium:warnings: {}, appium:webStorageEnabled: false, platformName: ANDROID} 会话 ID:629bae0e-84be-463f-86a7-49e1c58ddbcb 改变

LOGs https://gist.github.com/Nifargo/2920de27ba7ea76625f7430a6bf6bf96

期望我可以切入 web 元素的文本

Android WebView Appium 原生 NosuchElementException

评论


答: 暂无答案