Forum Discussion

dbaechtel's avatar
dbaechtel
Contributor
11 years ago

How to use the Browser Back button when the Page URL is not static?

I am navigating through a list of Links that are children of a page. I am trying to Click each Link in turn and then use the Browser back button to return.



I am getting an unspecified VB runtime error (nice!).



What is wrong with the attached syntax? What is the correct vbscript to use the Back button?

See attached source file. My Syntax:





For iChild = 1 To Aliases.browser.pageSafetydirectSitemap.formAspnetform.panelMastercontent.ChildCount Step 1


  'Posts an information message to the test log. Call Log.Message(iChild, "iChild =") 


  Set theObj = Aliases.browser.pageSafetydirectSitemap.formAspnetform.panelMastercontent.Child(iChild-1) 


  If (theObj.ObjectType = "Link") Then 


   If (Instr(theObj.Name,"PlaceHolder") = 0 _


      And Instr(theObj.contentText,"Logout") = 0 _


      And Instr(theObj.contentText,"Video") = 0 _


        ) Then 


    Call Log.Message(iChild, "Link =") 


    Call Log.Message(theObj.contentText, "content =") 


    Call theObj.Click() 


    ' Obtains the browser process 


    Set browser = Sys.Browser("iexplore") 


    ' Obtains the page currently opened in Internet Explorer 


    Set page = browser.Page("*") 


    Call page.Wait 


    Set browser = Sys.Browser("iexplore") 


    Set page = browser.Page("*") 


    Call page.Goback 


   End If 


  End If 


Next




10 Replies

  • What line of code causes the error message to appear?

    What if you will try to post BackSpace key press to the page instead of using GoBack method: page.Keys("[BS]") ?
  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Sys.Browser("chrome").BrowserWindow(0).Keys("[BS]")'no pun intended
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Don,



    First of all, without knowing what code line causes an error in your code we may only guess.

    Secondly:

    > The article mentioned does not have any reference to using the Browser Back button or Page.GoBack.



    Suggested approach does not require this. What your code basically does? It navigates to the link and, after the page is loaded (note - just loaded from the server, but not processed by the browser, while this processing might cause additional requests), it navigates back to the original page (hopefully, as the link click might cause one or more redirections). So, it looks like that your code just validates that the links on the initial page are valid. This is what the suggested code does. Its advantage is that it does not require UI (and even browser) to be executed and thus is more fast and reliable.
  • Can I get a simple answer to the question that I asked?

    How can I use the Browser Back function or Page.GoBack when the Page URL is unknown (because it can be one of many possibilities) ?

    I need to iterate through a List, click on a Link from the List, navigate to a new Page, and then GoBack to the previous Page and continue the iteration.

    Is this so hard to do?

    How would you code this up?

    Page
    Panel
    List
    Links

    The Links are children of the Panel.
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    > Can I get a simple answer to the question that I asked?

    I'm afraid that this will be not easy because it is not clear what you are asking for.



    In the initial post you asked:

    > What is wrong with the attached syntax?

    And did not mention (even after you was asked) what line gives an error.



    > What is the correct vbscript to use the Back button?

    This was answered by Ryan: Sys.Browser("chrome").BrowserWindow(0).Keys("[BS]")

    Quite correct vbscript.



    > How can I use the Browser Back function or Page.GoBack when the Page URL is unknown (because it can be one of many possibilities) ?

    Why page URL is not known? As you was able to get to the initial page you already know its URL (if not, than just read url property of the page object, e.g.: strURL = Aliases.browser.page.URL).



    So, unless it is not required by site functionality or some other requirements (but you did not mention this, so we may only guess) I see no reason to use the Back navigation.

    Personally I would use the UI-less aproach I provided the link to previously.

    If you still like UI way, why not to store initial URL in the variable and navigate to this initial address every time after you clicked the link?

    Note, that after you navigated forth and back, the initial page is reloaded and thus is recreated in the browser, thus invalidating all objects that you've got in your code. This means that you:

    a) Will need to obtain them a-new; and;

    b) Somehow keep the track of the already clicked links in order not to click them for the second time.

    The above two points are another good reason to consider the UI-less approach.



    And finally, haven't you tried what was suggested by Ryan (or whatever shortcut initiates back navigation in IE and other browsers)? It should work technically but remember about invalidated objects been found previously.
  • If I use



    Sys.Browser("iexplore").BrowserWindow(0).Keys("[BS]"),



    how do I wait to make sure that the page that I am runturning to is fully loaded when I don't know what that page may be?






  • The article mentioned does not have any reference to using the Browser Back button or Page.GoBack.



    Isn't there a way in TestComplete to using the Browser Back button without knowing whick page is being displayed?



    Why is this such a mystery?



    Why isn't it documented well?



  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Did you try the keys method I mentioned?

    It's really not any different from pushing the back button.