In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to accelerate Selenium". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to accelerate Selenium.
How to execute Selenium test cases faster?
Testers can speed up Selenium test cases in a variety of ways. You can consider using explicit waits, Web locators, preferring different browsers, optimizing the Selenium infrastructure, and other best practices to enhance software performance. Because there are so many updates in the final product, the process of maintaining Selenium test cases becomes tedious. Therefore, we cannot ignore the performance of Selenium test cases; instead, we should focus on accelerating them from the initial phase. The key tasks of Selenium test cases in any given scenario are:
Open the URL in the test using Selenium Webdriver (local / remote)
Use the relevant web page locator to locate web page elements
Execute an assertion on the positioning Web element on the page under test
Release resources used by WebDriver
Let's focus on some methods to understand how to speed up selenium testing.
Parallel testing in Selenium Automation
This is one of the easiest ways to speed up Selenium test cases. Parallel testing allows you to perform multiple tests simultaneously on different device browser combinations and operating system configurations, immediately covering the entire test suite. If you have an internal Selenium Grid infrastructure, you can always look at the benefits of Selenium Grid 4 and see what it provides to speed up Selenium test cases. Let's assume that you have ten tests to run. If you run them on different devices, all ten tests can be completed in 10 seconds instead of 100 seconds. You can select this method at the class and method level. Grouping, parameterization, and cloud-based options for test scenarios will further enhance the process.
a. Group testing:
Multiple test methods and test files in the test suite make it difficult to implement. If we group test scenarios according to the type of functionality under test, we can easily manage any emerging complexity.
b. Replace Selenium 3 with Selenium 4:
With the release of Selenium 4, Selenium has made significant improvements. It is equipped with optimized Selenium Grid, standardized Selenium Webdriver World wide Web Consortium (W3C), enhanced Selenium 4 IDE, and introduces Chrome Web Tools and related locators. These improvements can significantly speed up Selenium testing. If we compare Selenium 3 with 4, we will find that the former uses the JSON wired protocol for interaction between browsers and test code, which results in an additional burden of encrypting and decoding multiple API requests through the W3C. However, the latter uses the WebDriver W3C protocol to speed up the interaction between Web browsers and test code. New Selenium 4 relative locators, such as-'above',' below', 'to_left_of',' to_right_of', 'near', accelerate Selenium test cases and improve their overall stability. Also
c. Cloud-based selenium grid:
Whenever you want to test large Web applications that must run many parallel tests across multiple browser-operating system-device combinations, you will need a cloud-based Selenium Grid to execute and accelerate Selenium test cases. The following is an illustration of Selenium Grid.
Select the relevant site locator
The Web locator is an integral part of any Selenium test scenario. After locating the network elements, a suitable network locator is needed to play a further role. Among the many options available, a faster network locator is always recommended. Of all the network locators, the ID locator is the fastest in the Selenium WebDriver. Let's briefly discuss some of the most commonly used network locators:
A. ID locator: it runs fastest using the document.getElementById () javascript command associated with all browsers. In cases where many unique elements persist, this command generates the first unique match. The HTML element works only if it has an ID attribute that is unique to each element on the page. In terms of execution speed, after ID, Name, CSS Selector and XPath are the fastest respectively.
b. Name selector: use the name selector Web locator when there is no ID in WebElement.
C. CSS Selector: if the WebElement does not have an ID or NAME attribute, it is appropriate to choose CSS Selector Web locator in this case. CSS is usually no different in most common Web browsers, and uses the CSS selector in Selenium to ensure better performance of the CSS engine. The advantages of using this Web locator are faster element recognition, reduced browser incompatibility, and reduced test execution. Compared with XPath, CSS locator is preferred in the case of traditional Web browsers such as Internet Explorer to provide better clarity.
D. XPath: XPath Selector is the most flexible Web locator, but it is the slowest of the four fastest locators, because you have to span each layer of the path to select a specific Web element and move from one browser to another. Using the XPath locator should not be the primary option, but should only be used if this is the only option left.
Use a small number of Web locators: keeping the number of Web locators to a minimum can improve the readability of test scripts, thereby reducing the execution time of Selenium scripts.
Explicit Waits: the Explicitwait command for automated testing will eliminate any deceleration and allow you to execute wait conditions, such as Element visible, Element clickable, and Element is an optional page Web element, which is not possible in the case of implicit wait in Selenium. For example, the ToBeClickable method generates a WebElement when the identified element is clickable. Once the conditions are met, the explicit wait retreats. This means that the element is returned as a result and does not wait for the entire duration. The following code snippet shows the WebElement of ID = element positioned within 6 seconds. After its location, explicitly wait for the exit and return the desired WebElement.
Better performance is demonstrated using test scripts that explicitly wait.
Create atomic scripts: create independent test cases to make Selenium testing efficient by simplifying complex scenarios. Frameworks like TestNG support explicit test dependencies between test methods, while atomic tests can easily detect faults, which reduces test time and maintenance work, minimizes test dependencies, and accelerates Selenium testing.
Disable images on the page to speed up page loading: after creating a Selenium instance, you can use the driver.get () method to open the page under test. Many web pages are rich in content and composed of many images, resulting in slower page loading. However, you can speed up page loading by using browser-related settings to disable image loading.
The following snapshot shows:
How to use Selenium scripts in Chrome to disable page loading to speed up Selenium test cases and page loading.
How to use Selenium scripts in Firefox to disable page loading to speed up Selenium testing:
In this case, image loading is controlled in the Amazon e-commerce site, where the Firefox preference is set to 2 and permissions.default.image is used.
Parameterized data-driven testing: let's take a look at how to use Parameterization to accelerate selenium testing. When it comes to testing against a large number of datasets and running the same test on different test inputs. Parameterization proved to be a good choice. Most automation frameworks support parameterization well, such as TestNG (Selenium Java), JUnit, NUnit (C #), PyTest (Selenium Python), and so on.
Use headless browsers / drivers: headless browsers allow us to perform browser user interface (UI) tests without a browser graphical user interface (GUI). It also helps to improve the efficiency of cross-browser tests that run in the background. If you don't want to know about UI interactions received through test scripts, you don't need this best practice. Some common headless browsers are HtmlUnit, Splash, PhantomJS, and so on. View the performance of the Selenium browser test in the context of the PhantomJS driver.
At this point, I believe you have a deeper understanding of "how to accelerate Selenium". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.