Cant get any element after changing context to webview

NATIVE_APP
WEBVIEW_com.dkveuroservice.mobileappkit
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
bodyEl.size: 1

found. now try

               try {
                    Document doc = Jsoup.parseBodyFragment(driver.findElement(By.cssSelector("body")).getAttribute("outerHTML"));
                    System.out.println("\n" + doc.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }

i use

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.15.3</version>
            <scope>test</scope>
        </dependency>

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
java.lang.NullPointerException: Cannot invoke “String.length()” because “s” is null

something in your code. “s” is your contex. no “s” in code i wrote :slight_smile:

that is weird cause before i didn get it before and i didnt change anything in the code
@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?";

    var onboardingPage = new OnboardingPage();
    var onboardingLinearLayout = onboardingPage.clickSkipButton();
    var locationPopUp = onboardingLinearLayout.clickLaterButton();
    var analysisPopUp = locationPopUp.clickAllowAllTimeButton();
    analysisPopUp.clickActivateButton();
    var feedbackProfilePage = new DoYouLikePopUp().clickNoButton();

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();
}

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

    sleep(5000);
    try {
        Document doc = Jsoup.parseBodyFragment(((AndroidDriver) AppiumInit.getAppiumDriver()).findElement(By.cssSelector("body")).getAttribute("outerHTML"));
        System.out.println("\n" + doc.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

}
That is weird cause it happens when i add your code.

if you do not switch context. what is in ?

System.out.println(driver.getPageSource());

1 so you have all visible. why not try interact here without switch to WebView?
2 you app has few webviews or it is pure web? What you test?

  1. cause even without changing context i got error - org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.

and this page is webview so that is why i thought that i need to change the context
2. as far as i can see from the contexts - we have 1 NATIVE and 1 WEBVIEW. but if i dont use

    capabilities.setCapability("appPackage", "com.dkveuroservice.mobileappkit");
    capabilities.setCapability("appActivity", "com.dkveuroservice.mobileappkit.MainActivity");

i saw one more webview_staging

  1. what you need on this screen? Enter some value into edit input and tap Submit?
  2. i did not get. How many webViews you have? 1,2,5, many or all ?

  1. So i found out that i can use XPath(text) but I cant use id from this page.

  2. We have only 1 webview right now

One web view? wow forget about switch. try simple:

        WebElement inputField = driver.findElement(AppiumBy.className("android.widget.EditText"));
        inputField.sendKeys("test feedback");
        WebElement submitButton = driver.findElement(AppiumBy.className("android.widget.Button"));
        tap(submitButton); // tap function - whatever you know how to tap.

ooo didnt understand you - we have 5-7 webview pages

I thought that we are talking about webview context

But il try your way right now

In most cases it easier not to switch. Problem happens often when we have large and complex webView page or after we scroll it.

1 Like

yes it works but how can i understand why I cant use webview for testing?

no answer :slight_smile: sometimes it hard to understand issue without having test app…

1 Like

i thnk i understood why i can get any webview element

in devtools - I found one more page

So Feedback page is mine but I don’t know how did I get - about blank page

and it happens with all webview pages
And did I understand correctly if I have 2 webviews pages - I have to have 2 webviews contexts?

Had same. In such case full webcontext name helps to understand where to switch.

Context size = 2 - > switch to whatever webContext have. More then 2 need read context name and switch to correct.

Ps in one more difficult situation even wrote search element in all webContexs.

yes but even in this case i have only 1 webview context

NATIVE_APP
WEBVIEW_com.dkveuroservice.mobileappkit.staging

so how can I use the correct webview page?

Hybrid app has multiple WebViews showing in Chrome inspector; how do I switch between them? -> driver.getWindowHandles();

PS Andrew start using forum search :slight_smile:

1 Like