Forum Discussion

Lagencie's avatar
Lagencie
Frequent Contributor
7 years ago
Solved

Resizing Window

Hello,

 

how exactly should I resize the browser window in test complete?

 

I tried it with BrowserWindow.Position(0,0,width,1000) in a function where I give the width as parameter ... But it sets the window size to idk 1697 or something rather than my 1710 I sent to the function.

 

Is there a better way to resize the BrowserWindow during runtime?

  • Hi,

     

    So far I ended-up with this code. Hopefully it will work for you as well...

    //----------------------------------------------------------------------------
    
    // Sets the size of browser's viewport
    function BrowserViewportSizeSet(page, iWidth, iHeight)
    {
      var wnd;
      var iBorderWidth, iBorderHeight;
      var iWinWidth, iWinHeight;
      var res;
    
      wnd = BrowserWindowByPageGet(page);
    
      iBorderWidth = wnd.Width - page.Width;
      iBorderHeight = wnd.Height - page.Height;
    
      iWinWidth = iWidth + iBorderWidth;
      iWinHeight = iHeight + iBorderHeight;
    
      Log.Message(aqString.Format("Set browser\'s viewport size to %ix%i (window size is %ix%i)", iWidth, iHeight, iWinWidth, iWinHeight),
          aqString.Format("%ix%i\n%ix%i", iWidth, iHeight, iWinWidth, iWinHeight));
    
      res = (iWinWidth > Sys.Desktop.Width) || (iWinHeight > Sys.Desktop.Height);
      if (res)
        Log.Warning(aqString.Format("Viewport size was not changed as it exceeds current size of the desktop (%ix%i)",
            Sys.Desktop.Width, Sys.Desktop.Height));
      else
      {
        if ((wnd.Width != iWinWidth) || (wnd.Height != iWinHeight))
          wnd.Position(0, 0, iWinWidth, iWinHeight);
      }
    
      return res;
    }
    //----------------------------------------------------------------------------
    
    // Returns BrowserWindow object that corresponds to the given page object
    // From: http://smartbear.com/forums/f75/t83264/how-to-match-a-page-object-to-its-browserwindow
    function BrowserWindowByPageGet(Page)
    {
      var title;
      var wnd;
    
    //  wnd = Utils.CreateStubObject(); // Utils is not accessible from Script Extension
      wnd = null;
      if (Page.Exists)
        if ("Page" == Page.ObjectType)
        {
          if ('edge' == Page.Parent.ObjectIdentifier)
            wnd = Page.Parent.BrowserWindow(0); // quick crutch
          else
          {
            title = Page.contentDocument.title;
            wnd = Page.Parent.FindChild("WndCaption", title + "*");
          }
        }
    
      return wnd;
    }
    //----------------------------------------------------------------------------
    

14 Replies

  • Lagencie's avatar
    Lagencie
    Frequent Contributor

    It works - even tho its kind of complicated, but well, better complicated than not :P

    Here the code in python if someone needs it:

    def Set_Browser_Size(page, iHeight, iWidth):
      wnd = BrowserWindowByPageGet(page)
      
      iBorderWidth = wnd.Width - page.Width
      iBorderHeight = wnd.Height - page.Height
    
      iWinWidth = iWidth + iBorderWidth
      iWinHeight = iHeight + iBorderHeight
    
      Log.Message(aqString.Format("Set browser\'s viewport size to %ix%i (window size is %ix%i)", iWidth, iHeight, iWinWidth, iWinHeight),
          aqString.Format("%ix%i\n%ix%i", iWidth, iHeight, iWinWidth, iWinHeight));
    
      res = (iWinWidth > Sys.Desktop.Width) | (iWinHeight > Sys.Desktop.Height)
      if (res):
        Log.Warning(aqString.Format("Viewport size was not changed as it exceeds current size of the desktop (%ix%i)",
            Sys.Desktop.Width, Sys.Desktop.Height))
      else:
        if ((wnd.Width != iWinWidth) | (wnd.Height != iWinHeight)):
          wnd.Position(0, 0, iWinWidth, iWinHeight)
      return res
      
    def BrowserWindowByPageGet(Page):
      if (Page.Exists):
        if ("Page" == Page.ObjectType):
          if ('edge' == Page.Parent.ObjectIdentifier):
            wnd = Page.Parent.BrowserWindow(0)
          else:
            title = Page.contentDocument.title
            wnd = Page.Parent.FindChild("WndCaption", title + "*")
      return wnd
    • royd's avatar
      royd
      Regular Contributor

      Hi Lagencie

       

      This is what I use and wonderfully for me (javascript) -

       

       

       

      Sys.Browser("*").BrowserWindow(0).Position(0, 0, 1400, 1080);

      Hope this works for you as well.

       

      • Lagencie's avatar
        Lagencie
        Frequent Contributor

        This can not work royd because this solutions sets the Browser size, so the content is set to whatever is left.

         

        You need to check the different borders, as in chrome it is around 30px, in firefox around 25px, in edge only 10px so if you use .Position it only makes the outer lines of the browser to 1400 width - but I needed the content to be at least 1400px.

         

         

        My problem right now is with Edge AlexKaras did you test Edge already with your provided code? For me the wnd.width - page.width says = 0 because the variables seem to be the same on MicrosoftEdge ... Not sure what I should do here, I made it now If Browser Edge => add 9,8px as this is the standard Edge Border size, which is fairly cheap and cheated, but works ... Do you have a better solution for this problem probably?

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Question: Are you trying to set it to a particular size? Or are you trying to maximize it?  Trying to determine the purpose for altering the browser window size because there may be other options than setting to a particular size.

    • Lagencie's avatar
      Lagencie
      Frequent Contributor

      We are testing a web application with ~20 breakpoints at different px width sizes.

       

      therefore I need to set it to specific sizes 15 exactly.

       

      The height I just took fixed on 1000 but the width changes depending on the test.

       

      So maximizing is no option, I even need to restore it before as it sometimes opens the browser maximized, which doesnt allow .Position

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        So far I ended-up with this code. Hopefully it will work for you as well...

        //----------------------------------------------------------------------------
        
        // Sets the size of browser's viewport
        function BrowserViewportSizeSet(page, iWidth, iHeight)
        {
          var wnd;
          var iBorderWidth, iBorderHeight;
          var iWinWidth, iWinHeight;
          var res;
        
          wnd = BrowserWindowByPageGet(page);
        
          iBorderWidth = wnd.Width - page.Width;
          iBorderHeight = wnd.Height - page.Height;
        
          iWinWidth = iWidth + iBorderWidth;
          iWinHeight = iHeight + iBorderHeight;
        
          Log.Message(aqString.Format("Set browser\'s viewport size to %ix%i (window size is %ix%i)", iWidth, iHeight, iWinWidth, iWinHeight),
              aqString.Format("%ix%i\n%ix%i", iWidth, iHeight, iWinWidth, iWinHeight));
        
          res = (iWinWidth > Sys.Desktop.Width) || (iWinHeight > Sys.Desktop.Height);
          if (res)
            Log.Warning(aqString.Format("Viewport size was not changed as it exceeds current size of the desktop (%ix%i)",
                Sys.Desktop.Width, Sys.Desktop.Height));
          else
          {
            if ((wnd.Width != iWinWidth) || (wnd.Height != iWinHeight))
              wnd.Position(0, 0, iWinWidth, iWinHeight);
          }
        
          return res;
        }
        //----------------------------------------------------------------------------
        
        // Returns BrowserWindow object that corresponds to the given page object
        // From: http://smartbear.com/forums/f75/t83264/how-to-match-a-page-object-to-its-browserwindow
        function BrowserWindowByPageGet(Page)
        {
          var title;
          var wnd;
        
        //  wnd = Utils.CreateStubObject(); // Utils is not accessible from Script Extension
          wnd = null;
          if (Page.Exists)
            if ("Page" == Page.ObjectType)
            {
              if ('edge' == Page.Parent.ObjectIdentifier)
                wnd = Page.Parent.BrowserWindow(0); // quick crutch
              else
              {
                title = Page.contentDocument.title;
                wnd = Page.Parent.FindChild("WndCaption", title + "*");
              }
            }
        
          return wnd;
        }
        //----------------------------------------------------------------------------
        
  • kaiiii's avatar
    kaiiii
    Regular Contributor

    Browsers.Item(bws).Run(url)

    Sys.Browser.BrowserWindow(0).Maximize

    • Lagencie's avatar
      Lagencie
      Frequent Contributor

      sadly i cant remove answers ... 

       


      kaiiii wrote:

      Browsers.Item(bws).Run(url)

      Sys.Browser.BrowserWindow(0).Maximize


      thx for not reading the question, still looking for an answer