zobbo
zobbo

Reputation: 127

Setting LocalStorage with Chrome and Selenium

I'm trying to set a local storage key and value using OpenQA Selenium and Chrome. I thought this would be fairly trivial but I can't seem to get it to work. I'm quite new to C# so I maybe missing something. Anyway I have this function:

public static void SetMockData() { OpenQA.Selenium.Remote.RemoteWebStorage storage = new OpenQA.Selenium.Remote.RemoteWebStorage((OpenQA.Selenium.Remote.RemoteWebDriver)webDriver); storage.LocalStorage.SetItem("useTestData", "true"); }

I'm not sure if this is correct. Anyway when I run it I get the following exception:

System.NotImplementedException : unknown command: Cannot call non W3C standard command while in W3C mode

But I can't for the life of me turn off W3C. I can't see a chrome command argument to do it and I don't seem to have a way of adding a chrome option. There is an option in ChromeOptions called UseSpecCompliantProtocol which doesn't seem to do anything when set to false or true.

How can I set local storage for selenium in this case?

Upvotes: 1

Views: 4080

Answers (2)

Aaron Jacobson
Aaron Jacobson

Reputation: 11

In Visual Studio, when I drill down through the ChromeDriver base classes, I eventually get to OpenQA.Selenium.Webdriver. Many of the properties there are deprecated with a helpful message. For example:

    [Obsolete("Use JavaScript instead for managing localStorage and sessionStorage. This property will be removed in a future release.")]
    public bool HasWebStorage { get; }
    [Obsolete("Use JavaScript instead for managing localStorage and sessionStorage. This property will be removed in a future release.")]
    public IWebStorage WebStorage { get; }
    [Obsolete("Use JavaScript instead for managing applicationCache. This property will be removed in a future release.")]
    public bool HasApplicationCache { get; }
    [Obsolete("Use JavaScript instead for managing applicationCache. This property will be removed in a future release.")]
    public IApplicationCache ApplicationCache { get; }
    [Obsolete("Getting and setting geolocation information is not supported by the W3C WebDriver Specification. This property will be removed in a future release.")]
    public bool HasLocationContext { get; }
    public ICommandExecutor CommandExecutor { get; }
    [Obsolete("Getting and setting geolocation information is not supported by the W3C WebDriver Specification. This property will be removed in a future release.")]
    public ILocationContext LocationContext { get; }

Upvotes: 1

zobbo
zobbo

Reputation: 127

So the original code I used was wrong. With the help of this post

I got to the following code which is correct:

ILocalStorage webStorage = ((IHasWebStorage)webDriver).WebStorage.LocalStorage; 
webStorage.SetItem("useTestData", "true");

However, as with the poster in the link this gave me an exception:

System.InvalidOperationException: 'Driver does not support manipulating HTML5 web storage

So it seems the web driver doesn't support that feature. With the help of this post I got the following code:

IJavaScriptExecutor js = (IJavaScriptExecutor)webDriver;
js.ExecuteScript("localStorage.setItem('useTestData','true');");

This successfully adds the key to local storage. Just be careful when you add the key as it seems to be associated with the URL, so if you load it too early before loading the URL it will fail.

Upvotes: 7

Related Questions