How to clear Safari session?

Hi,

Have a real device with iOS 8.1 and Xcode6.1.1 on OSX 10.10.1 and Appium 1.3.6.
Finally I succeed with launching SafariLauncher and stuck on the following problem:

Session between tests is not cleared, first test starts perfectly, but next one start with already logged in user:( Is there some way to fix that?

Password autofill is disabled in Safari.
driver.manage().deleteAllCookies(); and capabilities.setCapability("ensureCleanSession", true); has no effect.
Connect appium to the grid with the following command:

appium -a 111.22.333.44 -p 4723 -U 9f4165489e2b87ddfbda3s30e3708f8 --device-name iPad --platform-name iOS --platform-version 8.1 --nodeconfig /Users/qa/Desktop/ios.json --safari --browser-name Safari

Here is my json file:

{
  "capabilities":
      [
        {
          "browserName": "Safari",
          "deviceName": "iPad",
          "version": "8.1", 
          "maxInstances": 1,
          "platformName":"iOS",
          "udid":"9f4165489e2b87ddfbda3s30e3708f8"
        }
      ],
  "configuration":
  {
    "cleanUpCycle":2000,
    "timeout":30000,
    "maxSession": 1,
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444,
    "proxy":"org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "hubHost": "111.22.333.44"
  }
}

Hi Malina,
I am having the same issue, were you able to find a fix for this?

driver.manage().deleteAllCookies(); wont work for iOS safari browser as you’re using safaridriver to launch safari browser instead you are using safarulauncher to launch safari browser hence the default cookie delete wont work. So i came up with a work around to dele cookie as well as closing the opened tabs or windows in safari browser.

//Clearing cookies in safari browser

public void clearCookies(WebDriver webDriver){
((JavascriptExecutor)webDriver).executeScript(“var cookies = document.cookie.split(”;");"
+ “for (var i = 0; i < cookies.length; i++) {”
+ “var cookie = cookies[i];”
+ " var eqPos = cookie.indexOf("=");"
+ " var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;"
+ " document.cookie = name + “=;expires=Thu, 01 Jan 1970 00:00:00 GMT”;"
+ " };");
}

//Call at the end of the session like Aftermethod

public void closeSafariTabs(WebDriver webDriver){
	try{
		Set<String> contextNames = ((AppiumDriver) webDriver).getContextHandles();
		for (String contextName : contextNames) {                       
			System.out.println("contextName " + contextName);
			if (contextName.contains("WEBVIEW")) 
				((AppiumDriver) webDriver).context(contextName);
			webDriver.close();
		}
	}catch(Exception e){};
}
1 Like

Hello Sethu,

I am facing the below issues when trying to run test against safari browser on iPad air 8.3(Real Device).

  1. Cookies not deleting
  2. brower is not killing after every test method

I tried above code which is provided by you. but no luck.

public void clearCookies(WebDriver webDriver){
((JavascriptExecutor)webDriver).executeScript(“var cookies = document.cookie.split(”;");"

  • “for (var i = 0; i < cookies.length; i++) {”
  • “var cookie = cookies[i];”
  • " var eqPos = cookie.indexOf("=");"
  • " var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;"
  • " document.cookie = name + “=;expires=Thu, 01 Jan 1970 00:00:00 GMT”;"
  • " };");
    }

Highlighted in bold code is executing and then control is coming out. Could you please elaborate above code.
I need to change any thing in above code. could you pls provide the your desired capabilities.

Can we kill safari browser after every test method execution.
How to kill all the opened safari windows before launching safari browser on iPad real devices using appium.

Could you please help me out on this issues. Thanks in advance.

Regards,
Prasad