[Automated Testing] Java+Selenium automated testing environment construction

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

1. Introduction to Selenium

Selenium 1.0 consists of four parts: core, IDE, RC, and grid. Selenium 2.0 is a new tool formed after two experts met and communicated with each other by chance and decided to integrate their respective ideas of object-oriented structuring (OOPP) and ease of writing code. It is also This is what we refer to as Web Driver. Core is the core of selenium. Although it was encapsulated in the later stage, it only reduced the visibility. It is still the core of driving selenium; IDE is a firefox browser plug-in, mainly used for novices or people who are not familiar with coding to get started. When used, this plug-in allows recording a piece of web operation code in Firefox, exporting it, modifying it and running it in Eclipse, but testers do not recommend using this plug-in in actual project use, because it will only reduce the tester’s coding ability; Web Driver is the key core for web page testing and is also one of the current mainstream testing tools. Selenium can be used in conjunction with Junit to perform both unit testing and integration testing. It is both a code testing tool and a functional testing tool. Let’s get started step by step on how to use the tool.

2. Environment configuration

2.1 Install JDK

Also configure the jre path correctly and click Next.

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

2.2 Configure environment variables

Create JDK Home path

Additional configuration System environment variable of path

3.Usage of Selenium IDE

3.1 Environment preparation

1) Install firefox

If you do not use the default installation, you will need to add materials to the code in the future. Here we are based on the habits of ordinary users.

Installed in the system disk, the places where configuration needs to be modified will be explained later.

3.2 Using selenium IDE

1) Install Selenium IDE plug-in

Open the firefox browser, search for selenium IDE for add-ons, and click Install Now on the pop-up software installation page. After installation, restart the browser and the plug-in will take effect.

2) Other important functions of Selenium IDE

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

Change, let’s take a look:>

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 Preparation

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

Install TestNG, Help-Eclipse Marketplace

4.2 New project

File new project, select the java project, next step:

Enter the project name testngstudy?next?finish

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

4.3 Start coding

  • Java + Selenium implements login function

Note: When using pure Java, there is no need to introduce TestNG. You only need to introduce 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 browser*/
System.setProperty("webdriver.chrome.driver","C:\ProgramFiles\driver\chromedriver.exe") forces the webdriver path to be specified when the chromedriver cannot be found.
 
WebDriver driver =new ChromeDriver();
 
String url = "http://www.baidu.com";
 
driver.get(url);
 
driver.manage().window().maximize(); maximize the window to increase the stability of page return visits
 
driver.findElement(By.id("kw")).sendKeys("selenium");
 
driver.findElement(By.id("su")).click ();
 
try {
 
Thread.sleep(1000); Delay setting to avoid slow loading of web pages and failure to find page elements
 
} catch (InterruptedException e) {
 
// TODO Auto-generated catch block
e.printStackTrace();
}
/*The so-called assertion*/
Assert.assertTrue(driver.getTitle().contains(“selenium”));
driver.quit();
}
}

Study arrangements

As someone who has experienced it, 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, no one to answer questions, and giving up after a few days of study, here I will share with you some automation. The test learning resources hope to help you on your way forward. 【Guaranteed 100% Free】

How to obtain documents:

Join my software testing exchange group: 632880530 to get it for free~ (Academic exchanges with fellow leaders, and there will be live broadcasts every night to share technical knowledge points)

This document 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. I hope it can also help you!

All of the above can be shared. You only need to search the vx official account: Programmer Hugo to get it for free