Technology Sharing | Selenium Installation for Web Automation

Acceptance testing of web applications often involves manual tasks such as opening a browser and performing an action described in a test case. But manually performed tasks are prone to human error and time consuming. So, by automating these tasks, you can eliminate the human element. Selenium can help us automate acceptance testing, making software more reliable and easier to maintain by building more rigorous tests.

Selenium supports automation of web browsers, it provides a set of test functions to support web automation testing. The function is very flexible and can complete functions such as interface element positioning, window jumping, and result comparison. Supports multiple browsers, multiple programming languages ​​(Java, C#, Python, Ruby, PHP, etc.), supports multiple operating systems (Windows, Linux, IOS, Android, etc.), open source and free. It mainly consists of three tools: WebDriver, IDE, Grid.

Selenium Architecture

Selenium scripting is completed on the client (client), and the script is sent to the Selenium server, and the Selenium server uses the browser driver (driver) to interact with the browser (browser).

Selenium core components

  • WebDriver uses the api provided by the browser to control the browser, just like the user is operating the browser, and it is not intrusive.

  • IDE is a Chrome and Firefox extension that can record user actions in the browser.

  • Grid is distributed for Selenium and you can run test cases across multiple browsers and operating systems.

Environmental preparation

The pip tool that comes with Python is installed

  • Python version
pip install selenium

复制代码
  • Java version
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
    <version>3.14.0</version>
</dependency>

复制代码
  • Python version

The prerequisite for installing Selenium is to have a Python development environment (PyCharm is recommended):

Selenium is a third-party library for Python, which can be installed using PyCharm's own method.

Menu bar File -> Settings to enter the configuration interface:

Search for Selenium -> Install Package:

Selenium supports a variety of browsers, you need to download the driver of the corresponding browser version, and set the browser driver location to the environment variable.

The download address of each browser driver: ceshiren.com/t/topic/327…

Write the code in the Python file. If the program can call up the corresponding browser, the installation is successful.

  • Java version

Dependencies are automatically loaded when using build tools such as Maven or Gradle.

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>4.0.0</version>
</dependency>

复制代码

Actual demonstration

#导入 Selenium 包
from selenium import webdriver

#创建一个 Chromdriver 的实例,Chrome()会从环境变量中寻找浏览器驱动
driver = webdriver.Chrome() 
# 打开 IE 浏览器
# driver = webdriver.Ie()  
# 打开 Firefox 浏览器
# driver = webdriver.Firefox()

复制代码
//导入 Selenium 包
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AiceTest {
    public static void main(String[] args) throws InterruptedException {
        //创建一个 Chromdriver 的实例
        WebDriver driver = new ChromeDriver();

    }
}

复制代码

This code will automatically create a Chrome process after running.


Finally, I would like to thank everyone who has read my article carefully. Watching the rise and attention of fans all the way, there is always a need for ritual exchanges. Although it is not a very valuable thing, if you can use it, you can take it directly.

These materials should be the most comprehensive and complete preparation warehouse for friends who do [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you! Everything should be done as early as possible, especially in the technology industry, and the technical foundation must be improved. I hope to be helpful…….


 

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/124250392