简体   繁体   中英

is it possible to get a new app session with KIF test?

I am trying to write a test where my next test should have a new app session so that all the activities i did in my previous test are cleared out. I am wondering if it is possible with KIF?

It is not possible because KIF is instantiated from a separated target. Also a new app session is not possible due the system not allow this.

I did a lot of research for this answer, so it my not be accurate. Use these suggestions with care :)

let request = NSMutableURLRequest(url: url!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60)

    let shared1 = URLSession.shared
    let shared2 = URLSession.shared
    let default1 = URLSession.init(configuration: .default)
    let default2 = URLSession.init(configuration: .default)
    print("shared1 session was \(shared1) urlCache: \(shared1.configuration.urlCache)")
    print("shared2 session was \(shared2) urlCache: \(shared2.configuration.urlCache)")
    print("default1 session was \(default1) urlCache: \(default1.configuration.urlCache)")
    print("default2 session was \(default2) urlCache: \(default2.configuration.urlCache)")

would print:

shared1 session was <__NSURLSessionLocal: 0x7f976ac0fdd0> urlCache: Optional(<NSURLCache: 0x60400000fb20>)
shared2 session was <__NSURLSessionLocal: 0x7f976ac0fdd0> urlCache: Optional(<NSURLCache: 0x60400000fb20>)
default1 session was <__NSURLSessionLocal: 0x7f976ac10b50> urlCache: Optional(<NSURLCache: 0x60400000fb20>)
default2 session was <__NSURLSessionLocal: 0x7f976ac11170> urlCache: Optional(<NSURLCache: 0x60400000fb20>)

As you can see, the shared session will use the same reference and therefor the same session/cache.

For default it's different. (I didn't initialize my shared sessions. I just set a pointer to them). Yet for the default , I created a new pointer!

That being said the default cache of the default session also uses the shared cache! Meaning if you create 2 distinct instances of default session then they won't have to download an image twice! So you still have a problem!

For default sessions, the default value is the shared URL cache object.

Apple Docs: urlCache

What you can do is to create a brand new urlCache and make your changes with that.

let memoryCapacity = 50 * 1024 * 1024
let diskCapacity = 50 * 1024 * 1024
let cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: "myDataPath")
URLCache.shared = cache

Do this each time you need need a new session.

The URLCache.shared = cache part will change the session that is used for shared and default sessions.

Some other options I can think of:

use an ephemeral session and invalidate afterwards

by using an ephemeral session:

Make sure you see the link!

An ephemeral session configuration object is similar to a default session configuration object except that the corresponding session object does not store caches, credential stores, or any session-related data to disk .

let session = URLSession.init(configuration: .ephemeral)
session.invalidateAndCancel()

And at then end of your test, perhaps inside: afterAll , do session.invalidateAndCancel() , as this: "Cancels all outstanding tasks and then invalidates the session."

You can ALSO use default session and then invalidate the session but the ephemeral session may suit your needs better. Ephemeral is kinda like private browsing. (As the documentation says, you can't, invalidate a shared session, so don't try using that!)

I haven't used KIF that much, but I do remember its beforeAll being a bit buggy. I'm not sure if the same is true for its afterAll , so make sure it actually gets called


use reloadIgnoringLocalCacheData

It basically means, for this URL, don't ever use cached data!

let url = URL(string:"https://static.pexels.com/photos/132037/pexels-photo-132037.jpeg")
let request = NSMutableURLRequest(url: url!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60)

For most cases you would .useProtocolCachePolicy as your cachePolicy, but not here!

Using useProtocolCachePolicy , would just conform to whatever cachePolicy you get in the header response. If it was set to cache for 5 days, then you would cache for 5 days locally until making an actual URL request again!

If you use .reloadIgnoringLocalCacheData then it would just OVERRIDE whatever cache policy the headers specify and always request again from the server.

If I were you I would write a wrapper function for my network request and default them all to use this .reloadIgnoringLocalCacheData cachePolicy.


set your cacheSize to 0

 let memoryCapacity = 0
 let diskCapacity = 0
 cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: "myDataPath")
 URLCache.shared = cache!

AFAIK this cache size would affect your entire app! I'm not sure if you could create distinct caches in your app or different caches for each session, it kinda defeats the purpose!

This way nothing well ever get cached!


For more see here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM