Create a UI automation project using selenium

Table of contents

1. Introduction to selenium

Advantages of Selenium:

Selenium components:

Selenium RC workflow

2. Use Selenium RC to create automation programs

2.1 install selenium

2.2 Install browser driver

2.3 Create a project


1. Introduction to selenium

Selenium is a tool for web application testing . Selenium tests run directly in the browser, just like real users. Supported browsers include IE (7, 8, 9, 10, 11), Mozilla Firefox , Safari , Google  Chrome , Opera , Edge, etc. The main functions of this tool include: Test compatibility with browsers-test your application to see if it can work well on different browsers and operating systems. Test System Functionality - Create regression tests to verify software functionality and user requirements. It supports automatic recording of actions and automatic generation of test scripts in different languages ​​such as  .Net , Java , and Perl .

Combining Selenium RC scripts and JUnit unit tests can cover not only functional tests, but also data or background Java class tests, thus forming a complete web application testing solution.

The biggest benefits of using Selenium over other testing tools are:

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

Advantages of Selenium:

By writing Selenium test scripts that mimic user actions, applications can be tested from the perspective of the end user. By running tests in different browsers, it is easier to spot browser incompatibilities. Selenium has "three mores", that is, multi-platform, multi-language, and multi-browser.

At its core, Selenium, also known as the browser bot , is written in JavaScript. This enables test scripts to be run in supported browsers. The browser bot is responsible for executing the commands it receives from the test script, which is either written in a table layout in HTML or in one of the supported programming languages.

Selenium 2.0 is available for the following browsers [2]   :

  • Google Chrome

  • Internet Explorer 7, 8, 9, 10, 11

  • Firefox

  • Safari

  • Opera

  • Edge

  • HtmlUnit

  • phantomjs

  • Android

  • iOS

Selenium components:

  • Selenium IDE: A Firefox plug-in that can record basic user operations and generate test cases. These test cases can then be run for playback in the browser, and the test cases can be converted into automation scripts in other languages.

  • Selenium Remote Control (RC) : supports multiple platforms (Windows, Linux, Solaris) and multiple browsers (IE, Firefox, Opera, Safari), and can be used in multiple languages ​​(Java, Ruby, Python, Perl, PHP, C# ) to write test cases.

  • Selenium Grid: Allows Selenium-RC to scale for large test case sets or test case sets that need to be run in different environments.

Selenium RC workflow

  • 1. The test case establishes a connection with the selenium-RC server through an Http request
  • 2. Selenium RC Server drives a browser, loads Selenium Core into the browser page, and sets the proxy of the browser to Http Proxy of Selenium Server
  • 3. The execution use case sends an Http request to Selenium Server, Selenium Server parses the request, and then sends JS commands through Http Proxy to notify Selenium Core to perform operations on the browser and inject JS code
  • 4. Selenium Core executes the received instructions and operates
  • 5. When the browser receives a new request, it sends an http request
  • 6. After Selenium Server receives the Http request sent by the browser, it reorganizes the Http request and obtains the corresponding Web page
  • 7. Http Proxy in Selenium Server returns the received page to the browser

2. Use Selenium RC to create automation programs

Now take Mac, Chrome + java + idea environment as an example to introduce the process of using selenium rc

2.1 install selenium

Open terminal -> pip installation (installation command: pip3 install selenium)

preview

2.2 Install browser driver

2.2.1 View browser version

Because different browser versions need to use different drivers, so first check the browser version to download the corresponding driver

2.2.2 Find the corresponding relationship between Chrome and ChromeDriver

For versions above Chrome73, the version of ChromeDriver is consistent with the version of Chrome; for versions below Chrome73, you can refer to the following relationship table to find the version of ChromeDriver.

2.2.3 Download the corresponding ChromeDriver

ChromeDriver download address: http://chromedriver.storage.googleapis.com/index.html   Select the corresponding version to download, decompress the downloaded file, and copy chromedriver.exe to /usr/local/bin/ (because the Mac directory is Hidden, so it can be opened by shortcut key command+shift+g)

preview

2.2.4 ChromeDriver environment configuration

Add chromedriver.exe to environment variables

  • Open the command line terminal and enter vim ~/.bash_profile
  • Add a line: export PATH=$PATH:/usr/local/bin/chromedriver, save and exit
  • Execute source ~/.bash_profile to make the modification take effect

Check the ChromeDriver version: chromedriver –version.

2.3 Create a project

2.3.1 First open the idea, select create new project, select to create a Java project, and then select next all the way to complete the project creation

2.3.2 Import the selenium jar package required by the project

Download the selenium-server-standalone-3.4.0.jar package

Import the local jar package just downloaded, the import method can refer to  https://blog.csdn.net/weixin_42614570/article/details/100162630

2.3.3 Create a java class

Running the code will open the Chrome browser and enter the Baidu homepage, which means that selenium runs successfully

package com.selenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTest {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
    }
}

 

 

 

 

Guess you like

Origin blog.csdn.net/jierxiaoyao/article/details/114097940