Issue
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?
Solution
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.
Answered By - zobbo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.