Maintain session variables with Selenium and Visual Basic - vb.net

I'm trying to open a new window and get the session variables (for example Session("sessionVariable")) with Selenium but when I do
Dim driver As ChromeDriver = New ChromeDriver()
driver.Navigate().GoToUrl(url)
And I try to get the session variable value in code-behind, it's empty, because it's a new window. I want to maintain the variable session in this new Selenium window. How can I do this? Tried with sessionStorage of javascript, and cookies, but I need to store a Object, not a string.
Perhaps a solution could be to write the contents of the serialized object to a file, then open the page with ChromeDriver and read the file, but surely there is a more elegant way to do this, right?

Related

How can I stop the browser from closing automatically?

I am using Selenium to log in to some website via EDGE.
The browser window is closing.
How can I stop the window from closing?
Sub Login_EdgeChrome2()
Dim wd As WebDriver
Set wd = New ChromeDriver
Const URL = "https://mms.fsicloud.me/Evolution/!System/Security/Login.aspx?ReturnUrl=%2fEvolution"
With wd
.Start "Edge"
.Get URL
.FindElementById("ctl00_contentPlaceHolder_loginControl_UserName").SendKeys "username"
.FindElementById("ctl00_contentPlaceHolder_loginControl_Password").SendKeys "password"
.FindElementById("ctl00_contentPlaceHolder_loginControl_LoginButton").Click '- this will log in with the correct credentials.
End With
wd.Wait (Now + TimeValue("00:01:30"))
End Sub
Used to do this in IE. However some features don't work well now in IE.
I tried Selenium to log in via EDGE. However, it logs automatically. So I put the time delay, but after the time lapsed, it will close again.
I confess I don't know much about VB - but from the looks of it, you have a subroutine that creates a localized instance of WebDriver. So when Login_EdgeChrome2 ends, the local instance disappears. In my world (in the languages I am familiar with), I'd have to return wd as a result of that subroutine to make it "survive".

How to find xpath in a new window handle

My site has a button that opens a new window handle.
I recognize the new window handle and switch to it.
when I try to inspect elements on this new handle I must press F12 and the DevTools open in a new window.
I enter my xpath and it highlights the object correctly.
when I search the object in my code it does not find it, and it seems that the HTML is not complete.
Any ideas?

Access WebView2 newly opened window

If I run window.open("url") it will open url in new popup window, even if it is Edge WebView2. It will just have default Windows title-bar, and read-only url bar. I need to open url from WebView exactly in new popup window, via window.open("url").
Is there any way to access that window from my program as far as original WebView2 child-window? By accessing I mean injecting scripts, and getting js-response after injecting.
Or, at least, perhaps there is way to open url with some pre-injected script, like window.open("http://google.com -javascript: alert(5)")?
Update
Found MSDN article, probably this is what do I need. Trying to understand that.
Yes, the ICoreWebView2::add_NewWindowRequested event lets you intercept WebView2 opening new windows.
If you do nothing in the event handler, or don't subscribe to the event, you'll get a new popup like you describe. In script the return value of window.open is connected up to the newly opened popup window. But in your native code you have no access to or control over the newly opened window.
Or you can set the Handled property to true on the NewWindowRequested event's args, and no new window will be created. This is useful if you want to prevent opening windows or if you want to send new window opens to the default browser or somewhere else. In script the return value of window.open is null.
Or you can provide your own ICoreWebView2 via the NewWindow property. In this case you are responsible for and get to choose how to host that ICoreWebView2 in some manner. The ICoreWebView2 you provide as the NewWindow will be the target of the new window navigation and connected back up via script to the return value of the window.open call.
Because obtaining a new WebView2 may require asynchronous method calls, you can use the GetDeferral method on the event args to stop the completion of the window requested event until you call Complete on the deferral asynchronously later.
You can see a working sample of the add_NewWindowRequested event in our sample app.

Editor specific command state

I have created a command and a handler that interacts with text editors (objects implementing ITextEditor). For each editor that the handler interacts with I'd like to store some data that the handler has access to.
What is the best way to do this?
Information about my specific problem
My command modifies the selection of text of the active editor. When the command is executed I want to store information about the previous selection. Another command should be able read this information and restore the previous selection.
Thoughts and observations
If one handler instance was created for each editor that would solve my problem. Is there a way to achieve this?
I've looked at the source code of the Java editor, trying to understand how things work there. The editor stores references to action objects. Since I can't create new fields for my command I can't use the same approach.
I've looked at the <state> plugin.xml tag, but I don't understand how to get one piece of state for each editor. But maybe there is a way?
I could manually maintain a map from editor to state object, and maybe register a listener for when editors are opened and closed. But I would prefer if there was a simpler way, where I didn't have to do this myself.
Maybe actions (IAction) could be used? But aren't they an old mechanism that has been substituted by commands?
I could maybe manually set an IAction object which as an reference to the stack using the ITextEditor.setAction.
If you are using Eclipse 4 you could use the transient data of the MPart object associated with the IEditorPart.
To get the MPart from an IEditorPart use:
MPart part = editor.getSite().getService(MPart.class);
Map<String, Object> transientData = part.getTransientData();
You can store anything you like in the transient data, use a key unique to your handler for the map key. The data is discarded when the editor closes.

How to open IE with its maximum window size using VB?

I need to open and also need to able to close a IE window, so I use
Proc = Process.Start("C:\Program Files\Internet Explorer\IEXPLORE.EXE", WebLink)
and
Proc.Kill()
However, I also need to make the IE size to be maximum. How I can do that? Thanks
I would suggest using the Process.Start() overload that accepts a ProcessStartInfo as the parameter. Starting your process using this will allow you to set the ProcessStartInfo.WindowStyle property to control the window style that the process is to be started with.
If the above doesn't work, you can try to maximize the window by using ShowWindow() with the SW_MAXIMIZE flag. In order to use native methods from within your managed executable, you will need to use P/Invoke. To get the window handle, you can try using the Process.MainWindowHandle property. If this still doesn't work you can try using EnumWindows() to iterate through all of the open windows, checking if they're owned by your created process by comparing the process ID (Process.Id) to the window's parent process ID (GetWindowThreadProcessId()).