/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.appium.java_client.ios;

import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException;
import org.junit.BeforeClass;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class BaseIOSWebViewTest extends BaseIOSTest {

    @BeforeClass public static void beforeClass() throws IOException {
        final String ip = startAppiumServer();

        if (service == null || !service.isRunning()) {
            throw new AppiumServerHasNotBeenStartedLocallyException("An appium server node is not started!");
        }

        File appDir = new File("src/test/java/io/appium/java_client");
        File app = new File(appDir, "vodqa.zip");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, PLATFORM_VERSION);
        //sometimes environment has performance problems
        capabilities.setCapability(IOSMobileCapabilityType.LAUNCH_TIMEOUT, 500000);
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, DEVICE_NAME);
        capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
        driver = new IOSDriver<>(new URL("http://" + ip + ":" + PORT + "/wd/hub"), capabilities);
    }

    protected void findAndSwitchToWebView() throws InterruptedException {
        Thread.sleep(10000);
        driver.getContextHandles().forEach((handle) -> {
            if (handle.contains("WEBVIEW")) {
                driver.context(handle);
            }
        });
    }
}

@@ -1067,16 +1067,43 @@
 net.URL;
+%0Aimport java.time.Duration;
 %0A%0Apublic
@@ -1149,16 +1149,185 @@
 OSTest %7B
+%0A    private static final Duration WEB_VIEW_DETECT_INTERVAL = Duration.ofSeconds(1);%0A    private static final Duration WEB_VIEW_DETECT_DURATION = Duration.ofSeconds(15);
 %0A%0A    @B
@@ -1336,16 +1336,20 @@
 oreClass
+%0A   
  public 
@@ -2403,36 +2403,187 @@
     
-Thread.sleep(10000);%0A       
+final long msStarted = System.currentTimeMillis();%0A        while (System.currentTimeMillis() - msStarted %3C= WEB_VIEW_DETECT_DURATION.toMillis()) %7B%0A            for (String handle :
  dri
@@ -2609,31 +2609,16 @@
 es()
-.forEach((handle) -%3E %7B%0A
+) %7B%0A    
     
@@ -2671,24 +2671,28 @@
             
+    
 driver.conte
@@ -2719,19 +2719,290 @@
     
-%7D%0A        %7D
+        return;%0A                %7D%0A            %7D%0A            Thread.sleep(WEB_VIEW_DETECT_INTERVAL.toMillis());%0A        %7D%0A        throw new IllegalStateException(String.format(%22No web views have been detected within %25sms timeout%22,%0A                WEB_VIEW_DETECT_DURATION.toMillis())
 );%0A 

Update web view detection algorithm for iOS tests (#1294)