0

Requirement:

  1. Launch the setting application.
  2. select safari option.
  3. clear the cache and data.

Followed below link to close this issue but its not working. https://developers.perfectomobile.com/pages/viewpage.action?pageId=21430825

Can someone please help me to resolve this issue.

Code: Please find the attached text file.

package Cookies.Cookies;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteExecuteMethod;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import io.appium.java_client.remote.MobileCapabilityType;
public class test {
  static Integer OSVersion;
  public static URL url;
  public static DesiredCapabilities capabilities;
  public static IOSDriver < IOSElement > driver;

  //1
  @BeforeSuite
  public void setupAppium() throws MalformedURLException, InterruptedException {
    //2
    final String URL_STRING = "http://127.0.0.1:4723/wd/hub";
    url = new URL(URL_STRING);

    //3

    capabilities = new DesiredCapabilities();
    capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
    capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "13.3.1");
    capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
    //capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
    capabilities.setCapability(MobileCapabilityType.CLEAR_SYSTEM_FILES, true);
    capabilities.setCapability("bundleId", "com.apple.Preferences");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Cigniti's iPhone");
    capabilities.setCapability(MobileCapabilityType.UDID, "00008030-000E4C461A21802E");
    capabilities.setCapability(MobileCapabilityType.FULL_RESET, false);
    capabilities.setCapability(MobileCapabilityType.NO_RESET, false);
    driver = new IOSDriver < IOSElement > (url, capabilities);

    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    //driver.resetApp();
    System.out.println("BeforeSuite executing");
    OSVersion = Integer.parseInt(deviceInfo(driver, "osVersion").substring(0, 2));
    System.out.println("==============" + OSVersion);
    ClearSafariCache(driver);
  } //5

  @Test(enabled = true)
  public void myFirstTest() throws InterruptedException {
    driver.resetApp();
    System.out.println("Test executed");
  }




  //Suppoted Methods

  private static void ClearSafariCache(RemoteWebDriver driver) throws InterruptedException {
    // TODO Auto-generated method stub

    Boolean aLreadyInSafari = true;

    //Go to Home Page
    Map < String, Object > param = new HashMap < String, Object > ();
    param.put("keySequence", "HOME ");
    driver.executeScript("mobile:presskey", param);

    System.out.println(param);
    //Launch Settings Menu
    startApp("Settings", driver);

    switchToContext(driver, "NATIVE_APP");

    //if(OSVersion==10){
    if (true) {

      if (waitForElementToAppear(driver, By.xpath("//*[@label=\"Back\"] |//UIANavigationBar//*[@label=\"Safari\"]"), 5)) {

        //If is safari setting menu opened Go ahead and clear the safari cache
        if (waitForElementToAppear(driver, By.xpath("//UIANavigationBar//*[@label=\"Safari\"]"), 5)) {



          processClearing1(driver, false);

          //else come back to main Settings menu and Clear the Cache
        } else {
          driver.findElementByXPath("//*[@label=\"Back\"]").click();
          processClearing(driver, false);
        }

      } else {

        processClearing(driver, false);
      }


    } else {

      if (waitForElementToAppear(driver, By.xpath("//XCUIElementTypeButton[@label=\"Settings\"]"), 5)) {

        //If is safari setting menu opened Go ahead and clear the safari cache
        if (waitForElementToAppear(driver, By.xpath("//*[@label=\"Safari\" and @visible=\"true\"]"), 5)) {



          processClearing1(driver, false);

          //else come back to main Settings menu and Clear the Cache
        } else {
          driver.findElementByXPath("//XCUIElementTypeButton[@label=\"Settings\"]").click();
          processClearing(driver, false);
        }

      } else {

        processClearing(driver, false);
      }

    }

  }

  public static void switchToContext(RemoteWebDriver driver, String context) {
    RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(driver);
    Map < String, String > params = new HashMap < String, String > ();
    params.put("name", context);
    executeMethod.execute(DriverCommand.SWITCH_TO_CONTEXT, params);
  }

  public static void processClearing(RemoteWebDriver driver, Boolean aLreadyInSafariSetting) throws InterruptedException {

    //skipping if screen already in safari page
    if (aLreadyInSafariSetting == false) {

      if (deviceInfo(driver, "model").contains("iPad")) {
        try {
          driver.findElementByXPath("//*[@label=\"Cancel\"]").click();
        } catch (Exception e) {

          System.out.println("no such element found");
        }
      }

      if (OSVersion == 10) {

        try {
          scrolltoXPath(driver, "//UIASearchBar[@label=\"Settings\" and @visible=\"true\"] | //XCUIElementTypeSearchField | //UIASearchBar");
          driver.findElementByXPath("//*[@label=\"Safari Suggestions\"]").click();
        } catch (NoSuchElementException e1) {

          System.out.println("no such element found");
        }

        if (deviceInfo(driver, "model").contains("iPad")) {

          driver.findElementByXPath("//UIAButton[@label=\"Search\"]").click();

        }
      } else {
        try {
          scrolltoXPath(driver, "//*[@label='Search']");
          driver.findElementByXPath("(//*[@label='Safari' and @visible='true'])[2]").click();
        } catch (NoSuchElementException e1) {

          System.out.println("no such element found");
        }

        if (deviceInfo(driver, "model").contains("iPad")) {

          driver.findElementByXPath("//UIAButton[@label=\"Search\"]").click();

        }

      }

      Thread.sleep(2000);

    }

    ScrolltoText("Clear History and Website Data", driver);



    //Clear only if Clear History is enabled      
    if (waitForElementToAppear(driver, By.xpath("//XCUIElementTypeCell[@label=\"Clear History and Website Data\" and @enabled=\"true\"]|//UIATableCell[@label=\"Clear History and Website Data\" and @enabled = 'true']"), 5)) {

      //Some times Clear History Widget not opened. Checking and trying again if not opened
      if (!waitForElementToAppear(driver, By.xpath("//*[@label=\"Cancel\"]"), 5)) {


        driver.findElementByXPath("//*[@value=\"Clear History and Website Data\"]").click();

      }

      driver.findElementByXPath("//*[@label=\"Clear History and Data\"]").click();

      //For IPAD

      if (deviceInfo(driver, "model").contains("iPad")) {
        try {
          driver.findElementByXPath("//*[@label=\"Clear\"]").click();
        } catch (Exception e) {
          // TODO Auto-generated catch block
          System.out.println(" no such element found");
        }
      }
    } else

      System.out.println("Cache Already Cleared. Nothing to clear");

  }

  public static void processClearing1(RemoteWebDriver driver, Boolean aLreadyInSafariSetting) throws InterruptedException {

    //skipping if screen already in safari page
    if (aLreadyInSafariSetting == false) {

      ScrolltoText("Clear History and Website Data", driver);

      //Clear only if Clear History is enabled      
      if (waitForElementToAppear(driver, By.xpath("//XCUIElementTypeCell[@label=\"Clear History and Website Data\" and @enabled=\"true\"]|//UIATableCell[@label=\"Clear History and Website Data\" and @enabled = 'true']|//*[@value=\"Clear History and Data\"]"), 5)) {

        //Some times Clear History Widget not opened. Checking and trying again if not opened
        if (!waitForElementToAppear(driver, By.xpath("//*[@label=\"Cancel\"]"), 5)) {


          driver.findElementByXPath("//*[@value=\"Clear History and Website Data\"]").click();

        }
        driver.findElementByXPath("//*[@label=\"Clear History and Data\"]").click();

        //For IPAD
        if (deviceInfo(driver, "model").contains("iPad")) {
          try {
            driver.findElementByXPath("//*[@label=\"Clear\"]").click();
          } catch (NoSuchElementException e) {

          }
        }
      } else
        System.out.println("Cache Already Cleared. Nothing to clear");
    }
  }


  public static Boolean waitForElementToAppear(RemoteWebDriver driver, By selector, long timeOutInSeconds) {

    driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);

    //https://stackoverflow.com/questions/7991522/selenium-webdriver-test-if-element-is-present

    try {
      driver.findElement(selector);
    } catch (Exception e) {
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      return false;
    }
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    return true;
  }


  public static void startApp(String appName, RemoteWebDriver d) {
    Map < String, String > params = new HashMap < String, String > ();
    params.put("name", appName);
    d.executeScript("mobile:application:open", params);
  }

  public static void closeApp(String appName, RemoteWebDriver d) {
    Map < String, String > params = new HashMap < String, String > ();
    params.clear();
    params.put("name", appName);
    d.executeScript("mobile:application:close", params);
  }


  public static void scrolltoXPath(RemoteWebDriver driver, String xPath) {

    do {
      try {
        driver.findElement(By.xpath(xPath)).sendKeys("safari");
        break;

      } catch (Exception NoSuchElementException) {

        Map < String, Object > params = new HashMap < String, Object > ();
        params.put("end", "20%,90%");
        params.put("start", "20%,20%");
        params.put("duration", "2");
        Object res = driver.executeScript("mobile:touch:swipe", params);

      }

    } while (true);
  }

  public static void ScrolltoText(String LabelText, RemoteWebDriver driver) {

    Map < String, Object > params2 = new HashMap < String, Object > ();

    params2.put("content", LabelText);
    params2.put("scrolling", "scroll");
    params2.put("maxscroll", "5");
    params2.put("next", "SWIPE_UP");
    params2.put("words", "words");
    Object result2 = driver.executeScript("mobile:text:select", params2);

  }

  public static String deviceInfo(RemoteWebDriver driver, String deviceProperty) {

    Map < String, String > params = new HashMap < String, String > ();
    params.put("property", deviceProperty);
    System.out.println(String.valueOf(OSVersion));
    return (String) driver.executeScript("mobile:deviceInfo", params);
  }
}

Error: image

  • https://stackoverflow.com/questions/29342596/how-to-clear-cache-in-safari-web-driver-using-selenium is anyone got the solution for the above mentioned question. – G karnakar Nov 11 '20 at 12:30
  • Welcome to StackOverflow. But take the time to improve your question. You'll be more likely to get a useful answer. Your question doesn't sufficiently describe the "issue*. You are just stating "its not working". What are you trying to achieve? What steps do you execute? What do you expect to happen? What happens instead? And the huge amount of code is unlikely to be studied by someone. If possible, narrow the problem down and only present the relevant code. – Codo Nov 11 '20 at 18:47

0 Answers0