[Automated testing] Java+Selenium automated testing environment construction

This book mainly introduces the process of building a Selenium automated testing environment and implementing code writing based on Java.

1. Introduction to Selenium

Selenium 1.0 includes four parts: core, IDE, RC, and grid. Selenium 2.0 is a new tool formed after two big cows met and communicated with each other and decided to integrate object-oriented structure (OOPP) and their ideas for easy coding. This is what we refer to as the Web Driver. Core is the core of selenium. Although it is encapsulated in the later stage, it only reduces the visibility. It is still the core driving selenium; IDE is a firefox browser plug-in, which is mainly used for beginners or those who are not familiar with coding to get started This plug-in allows to record a piece of web operation code in firefox, and then modify and run it in eclipse after exporting. However, it is not recommended for testers to use this plug-in in the actual use of the project, because it will only reduce the coding ability of testers; Web Driver is the key core to carry out web page testing, and it is also one of the mainstream testing tools at present. Selenium can be used in conjunction with Junit, both for unit testing and integration testing. It is not only a code testing tool, but also a functional testing tool. Below we will introduce the use of the tool step by step.

2. Environment configuration

2.1 Install JDK

Also correctly configure the jre path and click Next

Input: java –version to verify the correctness of jdk installation

2.2 Configure environment variables

Create JDK Home path

Append the system environment variable of the configuration path

3. Use of Selenium IDE

3.1 Environment preparation

1) Install firefox

If you don’t use the default installation, you will need to add material to the code in the future~, here we use the habits of ordinary users, not

It is installed in the system disk, and the places that need to modify the configuration will be explained later.

3.2 Using selenium IDE

1) Install the Selenium IDE plugin

Open the firefox browser, search for selenium IDE for the add-on, click Install Now on the pop-up software installation page, restart the browser after installation, and the add-on will work.

2) Other important features of Selenium IDE

As mentioned at the beginning of this article, another important function of Selenium IDE is to convert the script

Let's see it together :>

Selenium IDE can convert HTML scripts into scripts in C#, JAVA and other languages, which provides great convenience for us to write WebDriver test cases in the future.

4. WebDriver instance

4.1 Preparations

Open Eclipse, first select the encoding format as UTF-8 under windowspreferences, as shown in the figure below:

Install TestNG, Help-Eclipse Marketplace

4.2 New project

File new project, select java project, next step:

Enter the project name testngstudynextfinish

Create a selenium script, select the TestNG class, and click Next

4.3 Start coding

  • Java+Selenium implements login function

Explanation: It is not necessary to introduce TestNG when using pure Java, but only need to import the "selenium-server-standalone-2.21.0.jar" package.

File name: TestBaidu0325.java

File code:

package com.study;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class  TestBaidu0325 {
public static void main(String[] args) {
/*google浏览器*/
System.setProperty("webdriver.chrome.driver","C:\\ProgramFiles\\driver\\chromedriver.exe")找不到chromedriver时,强制制定webdriver路径。

WebDriver driver =new ChromeDriver();

String url = "http://www.baidu.com";

driver.get(url);

driver.manage().window().maximize();窗口最大化,加大页面回访的稳定性

driver.findElement(By.id("kw")).sendKeys("selenium");

driver.findElement(By.id("su")). click ();

try {

Thread.sleep(1000);延时设置,避免网页加载慢找不到页面元素

} catch (InterruptedException e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
/*所谓的断言*/
Assert.assertTrue(driver.getTitle().contains(“selenium”));
driver.quit();
}
}

 

study arrangement

As someone who has been here, I also hope that everyone will avoid some detours. If you don’t want to experience the feeling of not being able to find information when learning, no one answering questions, and giving up after a few days of persistence, here I will share with you some learning about automated testing. Resources, I hope to help you on the way forward. 【Guaranteed 100% Free】

How to get the video file:

This document and video material should be the most comprehensive and complete preparation warehouse for friends who want to engage in [software testing] . This warehouse has also accompanied me through the most difficult journey, and I hope it can help you too! All of the above can be shared, and you can receive it yourself by clicking the small card below.

Guess you like

Origin blog.csdn.net/weixin_57794111/article/details/130366023