Home > Blog > Two ways to Close a Browser in Selenium!

Two ways to Close a Browser in Selenium!

December 02 2021
Jolian

Do you want to know something about Selenium? Well, congratulations on finding it!

In this article you will learn:

  • What is selenium?
  • The functions and advantages of Selenium
  • How does Selenium relate to Python and WebDriver?
  • The difference between the close() and quit() methods

 

What is Selenium?

Selenium is a tool for Web application testing. Selenium tests run directly in the browser, just as real users do. Supported browsers include Internet Explorer (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, Edge, and more.selenium

  • AboutWeb automation

Web automated testing is web automated testing. You can use software to input data into web objects, click, and compare the pre-results with actual results, including test reports.

Web test automation includes interface test automation and UI test automation.

Automatic interface test: Verifies the interface level by sending HTTP or HSF requests to obtain the returned result and verify the service logic function on the link. Support online regression validation.

UI automation testing: The UI automation tools of the industry, such as WebDriver, Selenium, etc., are usually used to verify the functions and performance of website business logic by opening a browser and simulating user operations.

The Selenium components

1) Selenium IDE

A Firefox plugin that can record the user’s basic operations and generate test cases. These test cases can then be run and played back in the browser, and the test cases can be converted to automated scripts in other languages.

  • Selenium Remote Control (RC) :

Supports multiple platforms (Windows, Linux, Solaris) and multiple browsers (IE, Firefox, Opera, Safari), and can write test cases in multiple languages (Java, Ruby, Python, Perl, PHP, C#).

  • Selenium Grid:

Selenium-RC allows you to extend for large test cases or test cases that need to be run in different environments.

The version of Selenium

The core part of the Selenium1.0 (Selenium RC or Remote Control) tool is implemented based on the JavaScript codebase.

Selenium 1.0 automated test execution steps are as follows:

  1. Testers write test scripts based on Selenium supported programming languages
  2. Testers execute test procedures
  3. The test script sends an HTTP request to the Remote Control Server (RC) to access the website
  4. Upon receiving the request, RC visits the site under test, retrieves the data content of the page, inserts Selenium Core’s JavaScript codebase into the page, and returns it to the browser where the tester is performing the test
  5. The test script calls Selenium Core inside the browser to execute the test code logic, and finally records the test results to complete the test.

Selenium2.0 appears (Selenium2.0 = Selenium + WebDriver).

In October 2016, Selenium3.0 was introduced to separate the core API from the client driver and remove the less-used Selenium RC functionality.

 

The function of Selenium

The main functions of Selenium include testing browser compatibility and system functionality.

Test compatibility: Test the application to see if it works well across browsers and operating systems.

Test system functionality: Create regression tests to verify software functionality and user requirements.

The bottom layer of the framework uses JavaScript to simulate the operation of the browser by real users. When the test script is executed, the browser automatically clicks, enters, opens, and validates the script code, just as a real user would, testing the application from the end user’s perspective.

Makes it possible to automate browser compatibility testing, although there are still subtle differences between browsers.

Simple to use, you can use Java, Python, and other languages to write use case scripts.

The advantage of Selenium

Selenium tests run directly in the browser, just as real users do. Selenium tests run in Internet Explorer, Chrome, and Firefox on Windows, Linux, and Macintosh. No other testing tool can cover so many platforms. There are many other benefits to using Selenium and running tests in a browser.

Selenium is completely open-source and has no restrictions for business users. It supports distribution and has a mature community and learning documentation. Here are the main benefits:

By writing Selenium test scripts that mimic the user’s actions, you can test the application from the end user’s perspective. By running tests in different browsers, it is easier to find browser incompatibilities. The core of Selenium, also known as the Browser Bot, is written in JavaScript. This allows the test script to run in a supported browser. The Browser Bot is responsible for executing the commands received from the test scripts, which are either written in HTML table layouts or in one of the supported programming languages.

Selenium works in the following browsers:

  • Google Chrome
  • Internet Explorer 7, 8, 9, 10, 11
  • Firefox
  • Safari
  • Opera
  • Microsoft Edge
  • HtmlUnit
  • PhantomJS
  • Android
  • iOS

 

How does Selenium relate to Python and WebDriver?relationship

Relationship between Selenium and Python

Selenium is also a tool for Web application testing. ThoughtWorks is an acceptance testing tool written specifically for Web applications.

Python is an object-oriented, literal computer programming language. Is also a powerful and perfect universal language, has more than ten years of development history, mature and stable. Python has the richest and most powerful class library of any scripting language, enough to support most everyday applications. Python syntax is simple and clear, and it has a rich and powerful class library. Often nicknamed the Glue language, it makes it easy to link together modules made in other languages (especially C/C++).

Selenium can use the Python language to call Selenium RC for testing.

Relationship between Selenium and WebDriver

With the continuous development of Internet technology and the security limitations of browsers on the JavaScript language, the development of Selenium has encountered many difficult difficulties. Due to its own implementation mechanisms, Selenium is unable to break through the browser sandbox, making testing requirements for many test scenarios difficult to implement.

In 2006, Google engineer Simon Stewart started WebDriver, a project that lets test tools call built-in methods provided by browsers and the operating system itself to bypass the sandbox limitations of the JavaScript environment.

In 2008, Selenium and WebDriver merged, and Selenium2.0 emerged (Selenium2.0 = Selenium + WebDriver).

 

The difference between the close() and quit() methods

In some cases, we need to open more content than a browser with multiple tabs. To close these meetings quit() and close() methods, Selenium is used. But there is a difference.

Close: Used to close the current window. When many open Windows are open, use close to close some Windows.

Quit: Exit the driver and close all associated Windows. End the process and close all Windows. The close() method is used to close one of the Windows opened during the execution of the use case.close vs quit

Code test:

# -*- coding: utf-8 -*-

from selenium import webdriver

from time import sleep

driver = webdriver.Firefox()

driver.get(‘http://sahitest.com/demo/index.htm’)

Print driver.current_window_handle # Check the current window handle

driver.find_element_by_link_text(‘Window Open Test’).click()   # Open new Window1

driver.find_element_by_link_text(‘Window Open Test With Title’).click()  # Open new Window2

print driver. Window_handles  # View all window handles

driver.close()

print driver.window_handles  # If you have any window handles, you can see that the first two Windows are still in place

driver.quit()  # see that all Windows are closed

 

Results:

{b030dd54-3cbd-4d7b-800a-2ff296f03f5b}

[u'{b030dd54-3cbd-4d7b-800a-2ff296f03f5b}’, u'{7fdacf2e-0c34-4f0d-9a7a-ae34f3af932c}’, u'{f2d79121-8cc2-47ea-bd7d-2035e305ba2f}’]

[u'{7fdacf2e-0c34-4f0d-9a7a-ae34f3af932c}’, u'{f2d79121-8cc2-47ea-bd7d-2035e305ba2f}’]

selenium close browser

The close () method

# Importing webdriver from selenium

from selenium import webdriver

# Here Chrome will be used

driver = webdriver.Chrome()

# URL of website

url = “https://www.geeksforgeeks.org/”

# Opening the website

driver.get(url)

# Closes the current window

driver.close()

 

The quit () method

# Importing webdriver from selenium

from selenium import webdriver

# Here Chrome will be used

driver = webdriver.Chrome()

# URL of website

url = “https://www.geeksforgeeks.org/”

# Opening the website

driver.get(url)

# All windows related to driver instance will quit

driver.quit()

Also read
Ready To Get Started?

Don't waste another minute.Do more with ClonBrowser and start saving more time.

Register for free
准备好开始了吗?

不再浪费一分钟。使用ClonBrowser做更多的事情,并开始节省更多的时间。

免费注册
準備好開始了嗎?

不再浪費一分鐘。使用ClonBrowser做更多的事情,並開始節省更多的時間。

免費注冊
готовы начать? 

не трать ни минуты.  Используй ClonBrowser, чтобы сделать больше и начать экономить больше времени.

Подпишитесь бесплатно
¿Estás listo para empezar?

No pierdas ni un minuto.Haga más con clonbrowser y comience a ahorrar más tiempo.

Registrate gratis
© 2019-2021 ClonBrowser