Forum Discussion

nastester's avatar
nastester
Regular Contributor
2 years ago
Solved

Change browser used in all tests

Is there a way to globally change the browser used in keyword tests rather than having to update each Navigate statement to use a different browser? 

 

  • I don't know what else to tell you.

     

    This is the code (javascript) :

    function startBrowser(){
      var localBrowser;
        
      switch (Project.Variables.browser){
        case "chrome":
        localBrowser = TestedApps.chrome;
        break;
          
        case "msedge":
        localBrowser = TestedApps.msedge;
        break;
          
        default:
        localBrowser = TestedApps.msedge;
      }
     
      localBrowser.Params.SimpleParams.CommandLineParameters = Project.Variables.url;
      localBrowser.Run();
    
      if(!browser.Exists) {
        return false;
      } else {
        return true;
      }
    }


    And I store the value of the browser as a simple string.

     

    In the name mapping I have just this : 


    I make sure only one tested browser is running at a time (otherwise, there may be a conflict).
    And that's it. Anytime I want to call the browser or one of its childs objects I use : Aliases.browser.something.

16 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I have my NameMapping set up so that the automation is browser agnostic.  Then, when I build my tests, I use the "Run Browser" activity and specify the browser based upon a parameter that I pass in.  Every action after that is recognized as occurring on that browser.  In otherwords, considering reworking your test case and mapping to be browser agnostic.  That way, if you want to run the test on a different browser, simply pass in a different value for the browser parameter.  -3 is Chrome, -6 is Edge, and so forth. see Run Browser and my screen shots below

     

     




    • nastester's avatar
      nastester
      Regular Contributor

      Thanks for the quick reply! 

      Interesting solution. Wouldn't you have to change the parameter values for browser in each test every time you wanted to switch though? 
      It doesn't seem like you can set that at the project level, just on the test case itself, unless I'm missing something.

      Or do you have some way, like a script, that updates all the parameters in every test?

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        The tests are called from a custom framework that we use that build an in-memory object list to indicate which test cases to execute.  We set a project variable at the start of things that is then passed through to all projects.

        But... this is by no means the only way of doing things.  I would say that the NameMapping change is vital no matter how you do it.  But how you determine what browser runs... that can be done any number of ways.  There's a "loop" option to run test cases on multiple browsers that will actually loop through all installed browsers on a machine and run the same test.  You can leverage that to make your tests more dynamic.  But generally, there are two principles that I suggest you take away:

        1) Make your mapping of your browser object generic
        2) Parameterize your "Run Browser" operation in some way to globally change your browser.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I use a INI file, which has specific parameters I can change before running the automation. I can change the database connection string, the test enviroment, username, password, and browser. It works very well for the web application that I automate.

     

    See Storages.INI Method

  • eykxas's avatar
    eykxas
    Regular Contributor

    In my project I use a global variable "browser"  (Project.Variables.browser) and a little method : startBrowser() with a switch case in it. I read the value of the variable then I launch the corresponding browser.

     

    So I can switch to any browser by changing the value of the variable.

    • nastester's avatar
      nastester
      Regular Contributor

      eykxas 

      This is interesting, I like this. 
      Would you be willing to give a little more info? 

      Are you storing that function as the value of the variable like as a string?

  • eykxas's avatar
    eykxas
    Regular Contributor

    I don't know what else to tell you.

     

    This is the code (javascript) :

    function startBrowser(){
      var localBrowser;
        
      switch (Project.Variables.browser){
        case "chrome":
        localBrowser = TestedApps.chrome;
        break;
          
        case "msedge":
        localBrowser = TestedApps.msedge;
        break;
          
        default:
        localBrowser = TestedApps.msedge;
      }
     
      localBrowser.Params.SimpleParams.CommandLineParameters = Project.Variables.url;
      localBrowser.Run();
    
      if(!browser.Exists) {
        return false;
      } else {
        return true;
      }
    }


    And I store the value of the browser as a simple string.

     

    In the name mapping I have just this : 


    I make sure only one tested browser is running at a time (otherwise, there may be a conflict).
    And that's it. Anytime I want to call the browser or one of its childs objects I use : Aliases.browser.something.

    • nastester's avatar
      nastester
      Regular Contributor

      eykxas  

      Thanks! 
      The variable piece was confusing me but that is very helpful. Appreciate it

    • nastester's avatar
      nastester
      Regular Contributor

      eykxas  one more thing, I'm assuming your TestedApps are pointing to the local .exe of each browser?

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    That was just an example - you have different options, so it's a matter of picking one that suit your needs. Note, if your executable file changes, you will have to update your tested app location. Whereas, using Browser you don't.

    • nastester's avatar
      nastester
      Regular Contributor

      rraghvani eykxas 

       

      I realized while setting this up that this approach is only helpful if you don't have Navigate statements within your tests somewhere that are using a specific browser. 

       

      Edit: Never mind, I can switch the Navigate statements to use "Current Browser" 

  • MW_Didata's avatar
    MW_Didata
    Regular Contributor

    I noticed this problem has not been marked as solved so here is how I do my multiple browser tests.

    I run my projects from batch files so first i do a DB restore.

    Then within the arguments I have a variable called browser_type.

    The argument is "/arg:pv:Browser_type=Edge" or "/arg:pv:Browser_type=Chrome" etc.

    Because TC does not allow vars to start with a number/special char, I then convert that to the correct value: Values for me are "-3" or "-6"

    After running through the tests I do an other DB restore and give the other arg to run in Chrome.

     

    By parametrized browser navigation the browser is set at the start and carries through all tests.

     

     

    By using this way I can quickly add another browser if needed by adding into the batch script and set the browser to another value.

     

    Hope this helps with your problem!

     

    EDIT: if you use the solution be sure all browsers are set to the parameter.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You can simplify the code like so,

    function launchBrowser()
    {
        // Open browser
        Browsers.Item(Project.Variables.browser).Run(Project.Variables.url);
    
        // Obtains the browser process
        var browser = Sys.Browser(Project.Variables.browser);
    
        // Obtains the page currently opened in the browser
        var page = browser.Page("*");
    }

    Project.Variables.browser must be one of "iexplore", "firefox", "chrome" or "edge".

    Project.Variables.url e.g. "https://smartbear.com/" 

     

    You can perform additional tests, such as closing existing browser before opening a new browser. If Project.Variables.browser is not the correct string, log error message. Or if Project.Variables.url string is not given, log error message.

     

    You can launch your project via the command line and pass in the project variables. 

  • eykxas's avatar
    eykxas
    Regular Contributor

    Yes it works fine, but with your way it takes more time to execute. About 5 sec more. Because TC run the browser (loading the home page of the browser) then navigate to the page. In my case, the browser already launches with the right webpage.

     

    And if I use the CommandLineParameters before call the Run method, then it does exactly the same thing. So its just a replacement of the switch case by the enum of TC.