Close-Browser-1

In Testing, it is most important to understand how to interact with the browser. In this blog, we will be looking for ways to close browser. It is extremely easy to close a browser with a robot framework. Here, we will learn about how to close a browser using the selenium library. 

Generally closing the browser is the last step of any automation testing framework so it is usually situated in tear down method. 

Also, when we want to close a single browser window we tend to use ‘close browser’ method of the robot framework and if want to quit entire browser window we use ‘close all browser’ 

Let us discuss the following- 

  • Project setup for close browser 
  • Perform the action for closing a browser 
  • Verify for the same 

Project setup for Close a Browser:

Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/

After this, we can check our libraries by pip list command, or we can check manually also 

File > settings > project: robot-automation > python interpreter 

Close or quit browser project setup

After verifying the prerequisite part create a file closeBrowser.robot in the TestCases directory. 

— go to the new file > give a name of with .robot >Enter 

project file
New file

Keywords: 

  • Close Browser Closes single current browser window 
  • Close all browsers – Closes all browser windows 

TestCase:

Step1: Go to any website, in our example, we will be opening Google and Facebook 

Step2: Go to  https://www.google.com/ 

Step3: Click on the + icon of the same page and open one more web page that is, https://www.facebook.com/ 

Step4: Close the browser 

Step5: The above step will close done just ‘facebook.com’ but you would still be able to access the older tab which contains ‘google.com’ 

Step6: Close all browsers (this will quit the entire browser) 

***Settings ***
Library    SeleniumLibrary
***Variables ***
${browser}    chrome
${url1}     https://www.google.com/
${url2}    http://www.facebook.com/
***Test Cases ***
MyTestCases
      open browser    ${url1}    ${browser}
      sleep    2
      open browser    ${url2}     ${browser}
      sleep    2
      close browser       # to close only latest browser(facebook.com) 
      close all browsers
*** Keywords *** 

***Settings*** – To work with robot framework keywords we need to import the selenium library in the settings section because all these keywords are built in the selenium library- 

                                     Library SeleniumLibrary 

Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section. 

***Variables***– This section contains the defined variables that are used commonly, here we declared variables of browser and URL so we can reuse these variables.  

${url}  ${browser} 

In the same way, we can create more variables like this according to the requirement of the project. 

***Test Cases*** 

Close or quit browser code

Code Explanation: 

In the first step, we open 2 different URLs  

  1. https://www.google.com/ 
  2. https://www.facebook.com/ 

In the next step, we must close our current browser, so we use keywords 

                              Close browser 

This will only close the Facebook URL but still, we would be having our previous URL which is google.com which is running, so to quit the entire browser we would be using  

                          Close all browser 

This ends our Test.

Execution: 

To execute the Test case we need to use the below command. 

Close or quit browser execution command

After execution, we should get the result as a pass 

Execution result

Report: 

Close or quit browser log
Close or quit browser report