How can I call page function from automation code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JoeK

    How can I call page function from automation code?

    Hey all,

    I am automating a web page from Visual Foxpro. I can control all the
    textboxes, radio buttons, and command buttons using syntax such as:

    oIE.Document.Fo rms("searchform ").Item(<name>) .Value = <myvalue>

    But I cannot control a dropdown with an onchange event. I can set the
    dropdown's value and selectedIndex, but then calling the onChange() or
    Click() does not do anything. It only seems to fire the onchange if I
    use it interactively.

    I know what the onChange calls on the form, and I can call the
    function directly, but I don't know how. How does one call a straight
    JavaScript function that is in the script portion of the web page and
    have it fire as if it was called from the page itself?

    Just point me to any docs and I should be able to figure it out. I am
    just missing this one step, but it is crucial for this manipulation...

    You can email me at: junk (at) removethis.sute kh137.net, and I will
    try to monitor the newsgroup.

    Thanks in advance!
    JoeK
  • Robert

    #2
    Re: How can I call page function from automation code?

    joe@sutekh137.n et (JoeK) wrote in message news:<478a5314. 0408150749.468b 2d9@posting.goo gle.com>...[color=blue]
    > Hey all,
    >
    > I am automating a web page from Visual Foxpro.[/color]

    I do not know Visual Foxpro. Hope that my answer will still be on
    track.
    [color=blue]
    >
    > oIE.Document.Fo rms("searchform ").Item(<name>) .Value = <myvalue>
    >
    > But I cannot control a dropdown with an onchange event. I can set the
    > dropdown's value and selectedIndex,[/color]

    I assume you are setting these things from javascript.
    [color=blue]
    > but then calling the onChange() or
    > Click() does not do anything.[/color]

    I asume you mean the event handlers are not called when you chanage
    the values. This is true. They are not called. You need to call
    the functions directly after you change the data in javascript.
    [color=blue]
    > It only seems to fire the onchange if I
    > use it interactively.[/color]

    True. The event handlers response only to screen input.
    [color=blue]
    >
    > I know what the onChange calls on the form, and I can call the
    > function directly, but I don't know how.[/color]

    It is a standard call. The user of the this keyword makes the
    function call looks special, but it is a standard function call. You
    have to supply whatever the this would have supplied because you will
    not be in the this scope chain. ( I do not think, but you can try.)
    [color=blue]
    > How does one call a straight
    > JavaScript function that is in the script portion of the web page and
    > have it fire as if it was called from the page itself?
    >[/color]


    Here is my file where I have one style of call with the this and
    another without. It is probably best to first run this file. It will
    explain more about what is going on.

    Robert

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <HTML>
    <HEAD>
    <Title>My color test</Title>
    <script type="text/javascript">

    function processColors(f orm)
    {
    var box;
    box = document.create Element("p");
    document.body.a ppendChild(box) ;
    box.innerHTML = "<p>Display all <b>three</b> inputs.</p>";

    for (var i=1; i<=3; i++)
    {

    // Here I call the onchange function.
    displayOneColor (form,"color"+i );
    }

    return true;
    } //end function

    function displayOneColor (form,theColor)
    {
    var colorList = ["green","blue", "red","blac k"];
    var boxColor;
    var box;

    boxColor=form.e lements[theColor].value;
    box = document.create Element("p");
    document.body.a ppendChild(box) ;
    box.innerHTML =
    "<p><span style='color:" + colorList[boxColor] + "';>" +
    "You picked color "+ colorList[boxColor] +
    " for input " + theColor + "." +
    "</span></p>";
    }


    function validateOnSubmi t(form)
    {
    alert("in validateOnSubmi t.");
    }

    </script>
    </HEAD>
    <BODY >
    <p>This files show how you can call an onchange event
    handler directly in the onchange event or indirectly from
    another function. The
    function displayOneColor is called from each of the onchange
    event handler in the select tags. To see the direct call, look in the
    for loop
    of processColors.</p>
    <p>Changing on of the colors in the three select box
    will display a message at the bottom of the screen.
    Clicking on the GO button will show all three color
    selections. Clicking on Submit Query will erase all
    the color messages since the file is being reloaded. </p>

    <form name="form1"[color=blue]
    >[/color]
    Pick color 1:
    <SELECT name="color1" name=color1 style="WIDTH: 180px"
    onchange="displ ayOneColor(this .form,this.name );">
    <OPTION value = "0" >Green
    <OPTION value = "1" >Blue
    <option value = "2" >Red
    <option value = "3" >Black
    </OPTION></SELECT>
    <br><br>
    Pick color 2:
    <SELECT name="color2" style="WIDTH: 180px"
    onchange="displ ayOneColor(this .form,this.name );">
    <OPTION value = "0" >Green
    <OPTION value = "1" >Blue
    <option value = "2" >Red
    <option value = "3" >Black
    </OPTION></SELECT>
    <br><br>
    Pick color 3:
    <SELECT name="color3" style="width: 180px"
    onchange="displ ayOneColor(this .form,this.name );">
    <OPTION value = "0" >Green
    <OPTION value = "1" >Blue
    <option value = "2" >Red
    <option value = "3" >Black
    </OPTION></SELECT>
    <br>
    <input TYPE=BUTTON NAME="cmdCalc" VALUE="GO"
    onClick="proces sColors(this.fo rm)">
    <br><br>
    <input type="submit">

    </BODY>
    </HTML>

    Comment

    • Richard Cornford

      #3
      Re: How can I call page function from automation code?

      JoeK wrote:[color=blue]
      > I am automating a web page from Visual Foxpro.[/color]

      The odds are that there are not many (if any) regulars on this group who
      have ever done that.
      [color=blue]
      > I can control all the textboxes, radio buttons,
      > and command buttons using syntax such as:[/color]
      [color=blue]
      > oIE.Document.Fo rms("searchform ").Item(<name>) .Value = <myvalue>[/color]

      That code strongly resembles VBScript (is it?). A javascript (or,
      probably more correctly, JScript) version might go:-

      oIE.document.fo rms["searchform "].elements[<name>].value = '<myvalue>';

      And automating IE with WSH might acquire a reference to a new IE
      application as:-

      var oIE = new ActiveXObject(" internetexplore r.application") ;

      - but you haven't gone into how you are getting your - oIE - reference.
      [color=blue]
      > But I cannot control a dropdown with an onchange event. I can set the
      > dropdown's value and selectedIndex, but then calling the onChange() or
      > Click() does not do anything. It only seems to fire the onchange if I
      > use it interactively.[/color]

      Browser form controls do not tend to fire change events when their
      values are set through scripts. But scripts that set the values of
      controls may call the corresponding onchange handlers themselves as
      method of the form control. In JScript that would be:-

      var frmEls = oIE.document.fo rms["searchform "].elements;
      frmEls[<name>].onclick();
      - or -
      frmEls[<name>].click(); //for the - click - method.

      (javascript/JScript/ECMAScript is case sensitive.)
      [color=blue]
      > I know what the onChange calls on the form, and I can call the
      > function directly, but I don't know how. How does one call a straight
      > JavaScript function that is in the script portion of the web page and
      > have it fire as if it was called from the page itself?[/color]
      <snip>

      In JScript, as above, but in whichever language you are using I don't
      know. Though it will almost certainly follow a similar form/structure.

      Richard.


      Comment

      • Robert

        #4
        Re: How can I call page function from automation code?

        > > How does one call a straight[color=blue][color=green]
        > > JavaScript function that is in the script portion of the web page and
        > > have it fire as if it was called from the page itself?
        > >[/color]
        >[/color]

        There is a more elegant way of calling the event handler. Since the
        handler are functions, you can call the function directly. This is an
        example:


        // Call the event handler for the select
        form.elements["color" + i].onchange();


        "Note, however, that invoking an event handler is not a way to
        simulate what happens when the event actually occurs. If we invoke
        the onclick method of a Link object, for example, it does not make the
        browser follow the link and load a new document." from Javascrip: The
        Definitive Guide page 357.

        Here is the revised function...

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <HTML>
        <HEAD>
        <Title>My color test</Title>
        <script type="text/javascript">

        function processColors(f orm)
        {
        var box;
        box = document.create Element("p");
        document.body.a ppendChild(box) ;
        box.innerHTML = "<p>Display all <b>three</b> inputs.</p>";

        for (var i=1; i<=3; i++)
        {

        // Here I call the onchange function.
        // The commented line directly invokes the underlaying
        // function.
        // displayOneColor (form,"color"+i );

        // Call the event handler for the select
        form.elements["color" + i].onchange();
        }

        return true;
        } //end function

        function displayOneColor (form,theColor)
        {
        var colorList = ["green","blue", "red","blac k"];
        var boxColor;
        var box;

        boxColor=form.e lements[theColor].value;
        box = document.create Element("p");
        document.body.a ppendChild(box) ;
        box.innerHTML =
        "<p><span style='color:" + colorList[boxColor] + "';>" +
        "You picked color "+ colorList[boxColor] +
        " for input " + theColor + "." +
        "</span></p>";
        }


        function validateOnSubmi t(form)
        {
        alert("in validateOnSubmi t.");
        }

        </script>
        </HEAD>
        <BODY >
        <p>This files show how you can call an onchange event
        handler directly in the onchange event or indirectly from
        another function. The
        function displayOneColor is called from each of the onchange
        event handler in the select tags. To see the direct call, look in the
        for loop
        of processColors.</p>
        <p>Changing on of the colors in the three select box
        will display a message at the bottom of the screen.
        Clicking on the GO button will show all three color
        selections. Clicking on Submit Query will erase all
        the color messages since the file is being reloaded. </p>

        <form name="form1"[color=blue]
        >[/color]
        Pick color 1:
        <SELECT name="color1" name=color1 style="WIDTH: 180px"
        onchange="displ ayOneColor(this .form,this.name );">
        <OPTION value = "0" >Green
        <OPTION value = "1" >Blue
        <option value = "2" >Red
        <option value = "3" >Black
        </OPTION></SELECT>
        <br><br>
        Pick color 2:
        <SELECT name="color2" style="WIDTH: 180px"
        onchange="displ ayOneColor(this .form,this.name );">
        <OPTION value = "0" >Green
        <OPTION value = "1" >Blue
        <option value = "2" >Red
        <option value = "3" >Black
        </OPTION></SELECT>
        <br><br>
        Pick color 3:
        <SELECT name="color3" style="width: 180px"
        onchange="displ ayOneColor(this .form,this.name );">
        <OPTION value = "0" >Green
        <OPTION value = "1" >Blue
        <option value = "2" >Red
        <option value = "3" >Black
        </OPTION></SELECT>
        <br>
        <input TYPE=BUTTON NAME="cmdCalc" VALUE="GO"
        onClick="proces sColors(this.fo rm)">
        <br><br>
        <input type="submit">

        </BODY>
        </HTML>


        I know the prior poster mentioned this. I thought I was invoking the
        event function, but I was invoking my event function not the DOM event
        function and I didn't know I could do it this way until today.

        Robert

        Robert

        Comment

        Working...