Using PopupWatcher to accept browser ssl certificates

Class library usage, coding and language questions.
borisveis
Posts: 7
Joined: Tue Nov 12, 2013 3:02 am

Using PopupWatcher to accept browser ssl certificates

Post by borisveis » Tue Nov 12, 2013 3:32 am

//code here
I would like to use the PopupWatcher to always accept certificates presented in a browser. Though, I'll need to do it for IE, FF, and Chrome, I'm just trying to get Firefox working and then the other two. Another thing I should mention is that I come from Java land and have limited exposure to .Net.

All the popup watcher discussions seem to lead to one of two posts. Sorry for the abriviated link. This forum doesn't allow me to post URLs
1. Implemented in Program.cs
popupwatcher-in-ranorex-4-problem-with-repo-items-t4332.html
2. Implemented in a class which implements ITestModule
test-automation-code-examples.html#c4678

I wasn't able to get the thread to run in #2 (did not see inserted logging message appear in console), so I tried to get it working directly in Program.cs. I did see the log message like this, but doesn't provide any info about the problem:

Code: Select all

at Ranorex.Core.Repository.RepoItemInfo.Find[T](Boolean findSingle, Boolean throwException) at Ranorex.Core.Repository.RepoItemInfo.CreateAdapter[T](Boolean throwException) at Flex_vSphere_Client.SSLCertsRepoFolders.FFCertAppFolder.get_UnderstandRisk() in c:\source\ranorex\Flex-vSphere-Client\SSLCertsRepo.cs:line 183 at Flex_vSphere_Client.Program.AcceptCertFF(RxPath path, Element myElement) in c:\source\ranorex\Flex-vSphere-Client\Program.cs:line 61 at Ranorex.PopupWatcher.FindPopups(PopupItemCallback itemCallback, PopupPathCallback pathCallback, RxPath absPath, String itemName, RepoItemInfo optionalInfo)
My code in Program.cs looks like this

Code: Select all

    class Program
    {
    	public static SSLCertsRepo repo = SSLCertsRepo.Instance;
        [STAThread]
        public static int Main(string[] args)
        {
            // Uncomment the following 2 lines if you want to automate Windows apps
            // by starting the test executable directly
            //if (Util.IsRestartRequiredForWinAppAccess)
            //    return Util.RestartWithUiAccess();

            Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
            int error = 0;
            
            PopupWatcher sslCertWatcherFF = new PopupWatcher();
			//sslCertWatcherFF.UpdateInterval = 5000;
			
			Report.Log(ReportLevel.Info, "popup watcher trigger in Runner");
			sslCertWatcherFF.Watch("/dom//button[@innertext='I Understand the Risks']", AcceptCertFF);
			sslCertWatcherFF.Start();

            try
            {
                error = TestSuiteRunner.Run(typeof(Program), Environment.CommandLine);
            }
            catch (Exception e)
            {
                Report.Error("Unexpected exception occurred: " + e.ToString());
                error = -1;
            }
            return error;
        }
        
        public static void AcceptCertFF(Ranorex.Core.RxPath path, Ranorex.Core.Element myElement)
		{
			Report.Log(ReportLevel.Info, "popup watcher trigger");
			//myElement.As<Ranorex.Button>().Click();
			repo.FFCert.UnderstandRisk.Click();
			repo.FFCert.ExceptionDialogButton.Click();
		}
    }
My repo looks like this:
repoImage.jpg
repo screenshot
repoImage.jpg (30.86 KiB) Viewed 2641 times
Thanks in advance
--Boris

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

Re: Using PopupWatcher to accept browser ssl certificates

Post by Support Team » Mon Nov 18, 2013 5:25 pm

Hello Boris,

I have seen that you have contacted us via email as well. One of my colleagues answered your question and it would be nice if you could post the final solution here.
Thank you!

Regards,
Bernhard
.
Image

borisveis
Posts: 7
Joined: Tue Nov 12, 2013 3:02 am

Re: Using PopupWatcher to accept browser ssl certificates

Post by borisveis » Thu Nov 21, 2013 2:39 am

I tried several suggested solutions. One thing I didn't see on any forum or docs is that the watcher can watch for multiple items. Once I found this, I registered both the IE and FF objects to look for, each reflecting to a different method (note FF vs IE in AccertCertXX)

Code: Select all

PopupWatcher sslPopupWatcher = new PopupWatcher();
sslPopupWatcher.Watch(sslRepo.UntrustedConnection.FFIUnderstandTheRisksInfo, AcceptCertFF);
sslPopupWatcher.Watch(sslRepo.UntrustedConnection.IEOverridelinkInfo, AcceptCertIE);  
Another issue I had was with the various overloaded versions of Watch(...). I eventually settled on the Watch(Info, method) version.

And lastly, where should I put this code? I tried Program.cs and that did work (not with code below, but similar). However, this was not the appropriate place. Instead, I create a code module and added it to the [SETUP] section of the test suite. This way, the cert acceptance is valid for all tests in the suites and developers would not need to add special code in test cases.

The repo looks like this:
repoImage1.jpg
repoImage1.jpg (27.02 KiB) Viewed 2587 times
This is the code of the code module

Code: Select all

/*
 * Created by Ranorex
 * User: Boris Veis
 * Date: 11/15/2013
 * Time: 9:48 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace Flex_vSphere_Client.SSLCertWatcher
{
    /// <summary>
    /// Description of UserCodeModule1.
    /// </summary>
    [TestModule("79D1386C-EA72-4255-A592-D21235C88896", ModuleType.UserCode, 1)]
    public class SslCertWatcherCodeModule : ITestModule
    {
    	public static Flex_vSphere_Client.SSLCertWatcher.SSLCertsRepo sslRepo = Flex_vSphere_Client.SSLCertWatcher.SSLCertsRepo.Instance; 
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public SslCertWatcherCodeModule()
        {
            // Do not delete - a parameterless constructor is required!
        }

        /// <summary>
        /// Performs the playback of actions in this module.
        /// </summary>
        /// <remarks>You should not call this method directly, instead pass the module
        /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
        /// that will in turn invoke this method.</remarks>
        void ITestModule.Run()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;
            
             // Create PopupWatcher  
			 PopupWatcher sslPopupWatcher = new PopupWatcher();
			 sslPopupWatcher.Watch(sslRepo.UntrustedConnection.FFIUnderstandTheRisksInfo, AcceptCertFF);
			 sslPopupWatcher.Watch(sslRepo.UntrustedConnection.IEOverridelinkInfo, AcceptCertIE);  
		 
			 // Start PopupWatcher  
			 sslPopupWatcher.Start();
        }
        
        public static void AcceptCertFF(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)
		{
			Report.Log(ReportLevel.Info, "This appears to be Firefox");
	    	sslRepo.UntrustedConnection.FFIUnderstandTheRisks.Click();
	    	//Inserting delays for ease of debugging. You need a few seconds in case something goes wrong and you need to stop run
			Delay.Duration(3000, false);
        	sslRepo.UntrustedConnection.FFExceptionDialogButton.Click();
        	Delay.Duration(3000, false);
        	Keyboard.Press("{Tab}{Tab}{Tab}{Tab}");
        	Keyboard.Press("{Return}");	
		}
        
        public static void AcceptCertIE(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)
		{
			Report.Log(ReportLevel.Info, "This appears to be IE");
			sslRepo.UntrustedConnection.IEOverridelink.Click();
		}
        
    }
}

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

Re: Using PopupWatcher to accept browser ssl certificates

Post by Support Team » Fri Nov 22, 2013 3:00 pm

Hello Boris,

Thank you for providing your solution.

Regards,
Bernhard
.
Image

mtaylor
Posts: 10
Joined: Wed Feb 11, 2015 5:36 pm

Re: Using PopupWatcher to accept browser ssl certificates

Post by mtaylor » Wed Feb 11, 2015 5:39 pm

Please explain how you got the full path of your repository.

I see you used sslrepo.xxx.xxx.xxx

but where did you get this parent folder info from.
Nevermind I forgot to make an instance.

However I am still not able to get the code to work for chrome as it's error SSL page has not elements to attach to.

Matt

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

Re: Using PopupWatcher to accept browser ssl certificates

Post by Support Team » Fri Feb 13, 2015 2:13 pm

Hello Matt,

May I ask you to explain your issue in more detail? What exactly is not working on your side?

Thanks,
Robert
.
Image

mtaylor
Posts: 10
Joined: Wed Feb 11, 2015 5:36 pm

Re: Using PopupWatcher to accept browser ssl certificates

Post by mtaylor » Fri Feb 13, 2015 5:40 pm

A reply from Jay @ Ranorex
use a key sequence of {Enter} or {Tab}{Enter} to navigate around the tab focus and deal with the dialog using keyboard actions.
The easiest way for me to bypass Chrome's Security Warning page was to record a sequence of key strokes with the ranorex capture mode and then go to the source code and copy and paste that code into my usercode with a popupwatcher.

You can see it under the method ConfirmSecurityWarningCE

This is a crude way but it works.

Here is my code:

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace Windows7Test
{
    public partial class OpenBrowser
    {
    	
    	public static Windows7Test.Windows7TestRepository SSLRepo = Windows7Test.Windows7TestRepository.Instance;
    	
    	   	
        /// <summary>
        /// This method gets called right after the recording has been started.
        /// It can be used to execute recording specific initialization code.
        /// </summary>
        ///      
        private void Init()
        {

   	    	PopupWatcher securityPopupWatcher = new PopupWatcher();
   	    	securityPopupWatcher.Watch(SSLRepo.CertificateErrorNavigationBlocked.OverridelinkInfo, ConfirmSecurityWarningIE);
   	    	securityPopupWatcher.Watch(SSLRepo.UntrustedConnection.IUnderstandTheRisksInfo, ConfirmSecurityWarningFFAdd);
   	    	securityPopupWatcher.Watch(SSLRepo.AddSecurityException, ConfirmSecurityWarningFFConfirm);
   	    	securityPopupWatcher.Watch(SSLRepo.PrivacyErrorGoogleChrome, ConfirmSecurityWarningCE);
//   	    	securityPopupWatcher.Watch(SSLRepo.Safari.ClientInfo, ConfirmSecurityWarningSF);
   	    	securityPopupWatcher.Start();
    	    
        }

        public void ConfirmSecurityWarningIE(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)
        {
        
        	Report.Log(ReportLevel.Info, "This appears to be IE");
        	SSLRepo.CertificateErrorNavigationBlocked.Overridelink.Click();
        	Report.Log(ReportLevel.Info, "Succesfully Accecpted Security Warning for Internet Explorer");
        }
   
        public void ConfirmSecurityWarningFFAdd(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)
        {
        
        	Report.Log(ReportLevel.Info, "This appears to be Firefox");
        	SSLRepo.UntrustedConnection.IUnderstandTheRisks.Click();
        	SSLRepo.UntrustedConnection.ExceptionDialogButton.Click();
        	       	
        }
        
        public void ConfirmSecurityWarningFFConfirm(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)
        {
        	SSLRepo.AddSecurityException.PermanentlyStoreThisException.Uncheck();
        	SSLRepo.AddSecurityException.ConfirmSecurityException.Click();
        	Report.Log(ReportLevel.Info, "Succesfully Accecpted Security Warning for Firefox");
        }

        public void ConfirmSecurityWarningCE(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)
        {
        
        	Report.Log(ReportLevel.Info, "This appears to be Chrome");
        	SSLRepo.PrivacyErrorGoogleChrome.AddressAndSearchBar.Click("259;14");
            Delay.Milliseconds(200);
            SSLRepo.PrivacyErrorGoogleChrome.AddressAndSearchBar.PressKeys("{Tab}");
            Keyboard.Press("{Tab}");           
            Keyboard.Press("{Return}");
            Keyboard.Press("{Tab}");
            Keyboard.Press("{Return}");
            Validate.Exists(repo.Groov.SomeDivTag.LoginPanelInfo);
            Report.Log(ReportLevel.Info, "Succesfully Accecpted Security Warning for Chrome");
        	        	
        }
        
//         public void ConfirmSecurityWarningSF(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)
//        {
//        
//        	Report.Log(ReportLevel.Info, "This appears to be Safari");
//        	SSLRepo.Safari.Continue.Click();
//        	Report.Log(ReportLevel.Info, "Succesfully Accecpted Security Warning for Safari");
//        	
//        }

        public void CheckandConfirmSecurity()
        {
        }
     
    }
}

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

Re: Using PopupWatcher to accept browser ssl certificates

Post by Support Team » Mon Feb 16, 2015 9:24 am

Hello mtaylor,

I'm glad to hear that you found a solution to your issue.

Regards,
Robert
.
Image

mtaylor
Posts: 10
Joined: Wed Feb 11, 2015 5:36 pm

Re: Using PopupWatcher to accept browser ssl certificates

Post by mtaylor » Fri Nov 20, 2015 7:27 pm

So the issue now is Microsoft Edge because it first starts out as a winAPP you cannot use web.document to latch on to the HTML elements.

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

Re: Using PopupWatcher to accept browser ssl certificates

Post by Support Team » Tue Nov 24, 2015 11:10 am

Hello all,

Thread is continued here: http://www.ranorex.com/forum/microsoft- ... t8791.html

Sincerely,
Robert
.
Image