2

Does anyone know how to clear the cache on a new start of a test while running SafariDriver? I've tried to use java robot to keypress command + option + e, but that does not seem to work. It does not focus on the browser.

Robot r = new Robot();

try {
    Robot robot = new Robot();

    r.keyPress(KeyEvent.META_MASK);
    r.keyPress(KeyEvent.VK_META);
    r.keyPress(KeyEvent.VK_E);
    r.keyRelease(KeyEvent.VK_E);
    r.keyRelease(KeyEvent.VK_META);
    r.keyRelease(KeyEvent.META_MASK);

} catch (AWTException e) {
    e.printStackTrace();
}

Ive also tried to do an actions.builder method but that does not seem to work

    String clearCache = Keys.chord(Keys.CONTROL, Keys.COMMAND, "E");

    Actions builder = new Actions(browser);

    builder.sendKeys(clearCache);

    Action clearCacheAction = builder.build();

    clearCacheAction.perform();

I've also looked into using SafariDriver options but my java is not that good to fully understand how to implement it. Below is the code that Ive been trying to use. I created a SafariOptions Class and tried to instantiate it in my @before class.

package test

import org.openqa.selenium.safari.SafariDriver;

public class SafariOptions extends SafariDriver {
    private static SafariOptions ourInstance = new SafariOptions();

    public static SafariOptions getInstance() {
        return ourInstance;
    }

    public void setUseCleanSession(boolean useCleanSession){

    }

    public SafariOptions() {

         boolean useCleanSession = true;
    }
}

@Before
    public void createDriver() {

        assumeTrue(isSupportedPlatform());
        browser = new SafariDriver();
        SafariDriver options = new SafariOptions();
}

Nothing seems to clear the Safari cache on each test run.

user513951
  • 10,578
  • 7
  • 59
  • 72
adom
  • 129
  • 1
  • 3
  • 9
  • I've never had a cache issue with Selenium. Riddle me this: Why do you need to do this? I'm sure you can solve your issue some other way. – ddavison Sep 30 '13 at 14:09
  • My login system caches the login data and does not return the page cleanly. Not all elements are visible on a returning user load of the page and not all of the form elements are the same. – adom Sep 30 '13 at 18:58
  • This seems to be a known problem with Safari / SafariDriver; see https://code.google.com/p/selenium/issues/detail?id=5212 – Vince Bowdren Jul 09 '15 at 14:16

1 Answers1

1

Quick and easy solution for all who might want to know.

Add the following code to a .sh file to root.

killall cookied
rm -rf ~/Library/Caches/com.apple.Safari/*
rm -rf ~/Library/Safari/LocalStorage/*
rm -rf ~/Library/Cookies/*

Call on the file in @Before

Runtime runtime = Runtime.getRuntime(); runtime.exec("file.sh");

System.out.println("Cookies Removed");

adom
  • 129
  • 1
  • 3
  • 9