Sample code for Visual Studio code to run script on endpoint

Ask general questions here.
User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Sample code for Visual Studio code to run script on endpoint

Post by Aracknid » Thu Jun 29, 2017 7:43 pm

Hi,

Now that 7.1 is released, would it be possible for someone to please point me in the right direction with sample code on how to write a code in Visual Studio to :

- Create an endpoint (not to use Ranorex Studio and do it)
- Create a webdriver instance to use this endpoint and run on Edge

Basically I have all my scripts written in VS and I have all my own code to launch browsers and test against them. I don't use Ranorex Studio. I need to figure out how to change what I have to go thru the new webdriver code so that I can test against Edge.

Thanks,

Aracknid.

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Sample code for Visual Studio code to run script on endpoint

Post by Aracknid » Thu Jun 29, 2017 10:47 pm

Myself and a co-worker have been playing around with this a bit more. Perhaps we've made some progress, but we are still not where we want to be and perhaps we are running into unsupported items, or bugs, or lack of understanding... Anyway, here's what we are playing with and what is wrong...

What I'd like to accomplish is NOT to use Ranorex Studio to create endpoints. But To continue coding using Ranorex code, not selenium.

First, I found in the VS object browser some information about the RemoteEndpointFactory. Using your sample code provided here in C#, I converted it to VB.NET and wrote this:
(Note: this worked. It launched Chrome on my other machine and navigated to ranorex's website)
Dim sEPURL As String = "http://vmtaclt17-a99:4444/wd/hub"
Dim fac As New Ranorex.Core.RemoteEndpointFactory
Dim ep = fac.CreateTransientWebDriverEndpoint(New WebDriverEndpointInfo("TempEP", sEPURL))
Dim cfg = WebDriverConfiguration.FromJson("{""platform"": ""Windows 8.1"", ""browserName"": ""chrome"", ""version"": ""59.0""}")
'BTW: These next 2 lines both change each other's values. Bug?
cfg.Name = "MyConfig1"
cfg.Description = "Code sample Config"
ep.ActivateConfiguration(cfg)
ep.ConnectAsync().ContinueWith(Sub(antecendent) ep.MakeCurrentHostAsync()).Wait()

Dim b As OpenQA.Selenium.IWebDriver
b = ep.StartBrowser("chrome", "http://www.ranorex.com")

'So now I have a selenium web driver object... but I don't want to code in selenium. I want to code using Ranorex as this is already used throughout my framework.... Maybe I just need a little more info??

OK, code above is part of what I want. Creates dynamic endpoints.


Meanwhile, my co-worker was trying to approach things differently. He went into Ranorex Studio and defined an endpoint, calling it "Chrome". Here's what he did in code:
'We found that if you didn't kill localhost, it would not let you see the endpoints created in Studio
Host.Local.KillBrowser("localhost")
Host.MakeCurrentHost("Chrome")
Host.Current.OpenBrowser("www.ranorex.com", "chrome")

'So this also launches chrome on our other server (defined in the endpoint created via Studio) but now how do you use Ranorex to find the browser object???
'This cannot find the Chrome form. Also tried Host.Local. Didn't work either
Dim MyForm As Ranorex.Form = Nothing
If Not Host.Current.TryFindSingle(".//form[@ProcessName='chrome']", 30000, MyForm) Then
       Console.WriteLine("Oops. Can't find form.")
End If
So then I thought, why not combine the 2. That is, use the code in the first part to create the endpoint dynamically in the code, and then attach to it... but that didn't work. It cannot see the dynamic endpoint...
'First kill local browsers
Host.Local.KillBrowser("localhost")

'Use code from first example to create an endpoint
Dim sEPURL As String = "http://vmtaclt17-a99:4444/wd/hub"
Dim fac As New Ranorex.Core.RemoteEndpointFactory
Dim ep = fac.CreateTransientWebDriverEndpoint(New WebDriverEndpointInfo("TempEP", sEPURL))
Dim cfg = WebDriverConfiguration.FromJson("{""platform"": ""Windows 8.1"", ""browserName"": ""chrome"", ""version"": ""59.0""}")
cfg.Name = "MyConfig1"
cfg.Description = "Code sample Config"
ep.ActivateConfiguration(cfg)
ep.ConnectAsync().ContinueWith(Sub(antecendent) ep.MakeCurrentHostAsync()).Wait()

'Now use code from second example to connect to dynamic endpoint
Host.MakeCurrentHost("TempEP") '<== Fails here, no endpoint of such name exists.
Host.Current.OpenBrowser("www.ranorex.com", "chrome")
So, any thoughts?

Aracknid

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Sample code for Visual Studio code to run script on endpoint

Post by Aracknid » Thu Jun 29, 2017 11:05 pm

OK, I realized while stepping through the code, that when I dynamically created my own endpoint, it was automatically using it as the current host, so I didn't need to make it my current host (which didn't work).
'Skip this line
Host.MakeCurrentHost("TempEP") '<== Fails here, no endpoint of such name exists.
So I really only have the problem of finding the form on the remote system using Ranorex code.

Aracknid.

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Sample code for Visual Studio code to run script on endpoint

Post by Aracknid » Fri Jun 30, 2017 4:56 pm

OK, I figured out what I was doing wrong and what to do to find the Browser on the remote system.

By using the Spy tool and the endpoints functionality in Spy to spy on the remote system, I realized of course you can only see the DOM object since after all this is a webdriver... so I changed my code to this:
Dim MyDom As Ranorex.WebDocument = Nothing  
If Not Host.Current.TryFindSingle(".//dom[@domain='www.ranorex.com']", 30000, MyDom) Then  
       Console.WriteLine("Oops. Can't find Dom.")  
End If
So that is great.

I was also able to remotely run other browsers and find them as well. But due to the way all my code works, I need to do this locally on the machine upon which the script is running. So I changed my endpoint code to launch "http://localhost:4444/wb/hub" and on the system ran the selenium server and launch Edge. This works now too. Of course I need to use Host.Current, and my framework is Host.Local, so I'll have to change that.

I was then able to launch Edge locally and log into my site and do some very basic things....

So I think I'm off and running here.

Aracknid
Last edited by Aracknid on Mon Jul 10, 2017 3:34 pm, edited 1 time in total.

User avatar
Vega
Posts: 109
Joined: Mon Jul 11, 2016 2:15 pm

Re: Sample code for Visual Studio code to run script on endpoint

Post by Vega » Mon Jul 03, 2017 8:38 pm

Seems like you're getting up and running but you may want to have a look at the sample project that came with the 7.1 release notes:
private void Init()
        {
            // Your recording specific initialization code goes here
            
            var webDriverEndpoint = Host.Current.TryGetAsWebDriverEndpoint();
            
            if(webDriverEndpoint == null) { throw new InvalidOperationException("This test is not running on a web driver host"); }
            
            var driver = webDriverEndpoint.StartBrowser("firefox", "http://orf.at");
            
            var getDriverFromExisting = webDriverEndpoint.WebDrivers.Single(wd => wd.Title == "news.orf.at");
            
 			// From here on the driver instances can be used to do anything, native webdriver can do !
 			// These driver instances are the natvie C# web driver implementations (with the latest version we integrated into the plugin!)
        }

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Sample code for Visual Studio code to run script on endpoint

Post by Aracknid » Mon Jul 10, 2017 9:30 pm

I'm currently stuck trying to deal with another edge browser being spawned once I log in. How do I attach to it and interact with it. I was hoping there would be a nice simple way to use Host.Current and instead of OpenBrowser, you could just AttachBrowser or ConnectBrowser for an existing window, but that doesn't exist.

The code provided above by Vega sort of gave me an idea, but I ended up spinning my wheels for too long and going nowhere. Also note I'm using VB.NET, not C#. When I tried to do the line of code wd => wd.title I couldn't figure that out...

Aracknid.

User avatar
Vega
Posts: 109
Joined: Mon Jul 11, 2016 2:15 pm

Re: Sample code for Visual Studio code to run script on endpoint

Post by Vega » Tue Jul 11, 2017 9:21 pm

the "wd => wd.Title == "news.orf.at" is basically an inline expression in C# which is called a lambda expresison.
Not sure how I would write it in VB but using a .NET converter returned the below:
Dim getDriverFromExisting = webDriverEndpoint.WebDrivers.[Single](Function(wd) wd.Title = "news.orf.at")
In regards to where you are stuck, it sounds like you're trying to control a browser that is already open? How was the browser spawned?

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Sample code for Visual Studio code to run script on endpoint

Post by Aracknid » Tue Jul 11, 2017 9:30 pm

Thanks for the explanation of the => and the VB.net equivalent .

The second browser is spawned from the first browser window. In the Spy tool for Ranorex, if I use local, I see 2 different DOM objects and 2 different WinApps for Edge. When I switch to the endpoint I created for testing purposes, I only see the original DOM and not the second one.

I posted something separately about this here: https://www.ranorex.com/forum/having-we ... 11115.html

Aracknid

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Sample code for Visual Studio code to run script on endpoint

Post by Aracknid » Fri Jul 21, 2017 6:44 pm

Hi,

I' d really like an answer from support on this. I even e-mailed you and heard nothing back (been almost a week).

I need to know in code how to 'see' and 'interact' with the second DOM.

Thanks,

Aracknid.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Sample code for Visual Studio code to run script on endpoint

Post by Support Team » Mon Jul 24, 2017 4:55 pm

Hi Aracknid,

Thank you for the reply.

I have thoroughly checked our support ticketing system and did not see an email related to this issue. So unfortunately something must of happened while the message was in transit to us. Please send an email to [email protected] and we will be happy to assist you. Alternatively, you may also use the support query form located here:

http://www.ranorex.com/support/support-query.html

We will be keeping an eye out for your email and will provide confirmation upon it being received.

Hope this helps!

-Jon
.
Image