selenium执行脚本前自动清空缓存

package clearTemp;

import java.io.File;

import org.apache.commons.io.FileUtils;

public class ClearTemp {
    
    public static void  clearTempFile() throws Exception{

    try
    {
        File objTempFolder = new File(System.getProperty("java.io.tmpdir"));
        for(File objEach : objTempFolder.listFiles())
        {
            if(objEach.getName().toString().toLowerCase().contains("scoped_dir"))
            {
                if(objEach.isDirectory())
                {
                    FileUtils.deleteDirectory(objEach);
                }
            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    
    }
    
}

定义成静态方法  可以直接使用

在启动driver之前使用该方法。

@BeforeClass(alwaysRun = true) 
public void setUp() throws Exception {    
//chrome 谷歌浏览器初始化
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");  
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
ClearTemp.clearTempFile();
driver = new ChromeDriver(options);        
//页面元素初始化
loginPage=new LoginPage(driver);
projectPage = new ProjectPage(driver);
registerPage=new RegisterPage(driver);
adminLoginPage=new AdminLoginPage(driver);
navigationBar=new NavigationBar(driver);
collegePage=new AdminCollegePage(driver);
coursePage=new CoursePage(driver);
actions=new Actions(driver);
dataMarketPage=new DataMarketPage(driver);
adminDataMarketPage=new AdminDataMarketPage(driver);
js =(JavascriptExecutor) driver;
wait=new WebDriverWait(driver,20);
//最大化窗口

 

posted @ 2016-08-18 11:32  logo_mm  阅读(1814)  评论(0编辑  收藏  举报