Adding a cookie to a IE browser session

Ask general questions here.
macgowan
Posts: 14
Joined: Mon Jun 23, 2014 9:20 pm

Adding a cookie to a IE browser session

Post by macgowan » Sun Jul 27, 2014 7:01 pm

Hi ...

We are opening a IE browser session using processStartInfo. I would like to set a cookie in that session. What is the best way to do this. Can I add the cookie to the URL when I open the browser with processStartInfo --- or should I use HttpCookie ??

A code sample would be appreciated as I'm having some issues with using HttpCookie -- processStartInfo is working ...

Thanks,
Chris

Code: Select all

public LaunchBrowser(){}

void ITestModule.Run()
{
    uri = this.createURI();
    Report.Info("URI: " + uri);
    Report.Info("Launching Internet Explorer Maximized..");

    ProcessStartInfo processStartInfo = new ProcessStartInfo(
        @"C:\Program Files\Internet Explorer\iexplore.exe");

    processStartInfo.WindowStyle = ProcessWindowStyle.Maximized;
    processStartInfo.Arguments = uri;
    SummaryBase.process = Process.Start(processStartInfo);
}

private String createURI()
{

    String summaryURI = uri;

    // Testing here
    // How does this work ???
    // return artifacts.ElementAt(randomNumber);

    Report.Info("-----------------------------");
    Report.Info("Inside createURI()");
    Report.Info("ArtifactUuid: " + ArtifactUuid);

    if (!UseJudicialSearch && (summaryURI != null))
    {
        if (summaryURI.Contains("?"))
        {
            summaryURI += "&searchuuid=" + ArtifactUuid;
        }
        else
        {
            summaryURI += "?searchuuid=" + ArtifactUuid;
        }
    }


    Report.Info("summaryURI: " + summaryURI);
    return summaryURI;
}



// We are going to set the cookie here and see what happens
// string url = "http://javascript:workbench.setCookie('mockFlagForTesting','true')";
// doc.Navigate(url, true, 10000);

HttpCookie myCookie = new HttpCookie("mockFlagForTesting");

// Set the cookie value.
myCookie.Value = "true";

// Add the cookie.
Response.Cookies.Add(myCookie);            



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

Re: Adding a cookie to a IE browser session

Post by Support Team » Tue Jul 29, 2014 3:33 pm

Hi macgowan,

I am afraid that this is not directly an issue related to Ranorex, but I would use the HttpCookie class as described on the Microsoft website.
Another sample can be found on the section How to: Write a Cookie.

Regards,
Bernhard
.
Image

macgowan
Posts: 14
Joined: Mon Jun 23, 2014 9:20 pm

Re: Adding a cookie to a IE browser session

Post by macgowan » Wed Jul 30, 2014 3:50 pm

Hi ...

I'm still working on way to do this programmatically - this is a hack that will use the IE browser development mode to set the cookie - using Ranorex commands.

Code: Select all

void ITestModule.Run()
{
    Mouse.DefaultMoveTime = 300;
    Keyboard.DefaultKeyPressTime = 100;
    Delay.SpeedFactor = 1.0;

    // We are goign to get the cookie here
    // IE is already running will be running 

    Report.Info("Press F12 to open the Developer tools (wait 5 sec)");
    repo.JudicialSummaryApplicationWindowsI.Self.PressKeys("{F12}");

    Report.Info("The Developer Tools window is open (wait 5 sec");
    System.Threading.Thread.Sleep(5000);    

    if (repo.JudicialSummaryApplicationDeveloper.ScriptInfo.Exists()) 
    { 
        Report.Info("Open Script View");
        repo.JudicialSummaryApplicationDeveloper.Script.Click(); 
        
        Report.Info("Open Script Console");
        repo.JudicialSummaryApplicationDeveloper.Iexplore.Console.Click(); 
        System.Threading.Thread.Sleep(5000);    

        Report.Info("Set focus to the console input");
        repo.JudicialSummaryApplicationDeveloper.Iexplore.ConsoleInput.PressKeys("javascript:workbench.setCookie(\"mockFlagForTesting\",\"true\")"); 
        System.Threading.Thread.Sleep(5000);    

        Report.Info("Run the script (wait 5 sec)");
        repo.JudicialSummaryApplicationDeveloper.Iexplore.RunScript.Click(); 
        System.Threading.Thread.Sleep(5000);    

        Report.Info("Close development window - Press {Alt down}{F4}{Alt up} (wait 5 sec)");
        repo.JudicialSummaryApplicationDeveloper.Self.PressKeys("{Alt down}{F4}{Alt up}");
        System.Threading.Thread.Sleep(5000);    
        
    }
    else
    { 
        Report.Failure("Could not Open the script view"); 
    } 
}