Java+Selenium automated testing

Integration of Java+Selenium+TestNG automatic test framework

1. Simplified code

Encapsulates a class for locating elements, of type ElementLocation

package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
 * Call the same method every time you locate an element
 * Encapsulate each method for invocation only
public class ElementLocation {
     * Locating elements by id
     * @param id
     * @param elementId
    public  void finElementByID(String id, String elementId, WebDriver driver){
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     * Locating elements through xpath
     * @param xpath
     * @param text
    public void findElementByXpath(String xpath,String text,WebDriver driver){
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
/** * Clear the text box first and then locate the element through ID * @param id * @param text * @param text */ public void findElementByIdClearSendkeys(String id,String text,WebDriver driver){ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElement(; driver.findElement(; } /** * Clear the content of the text box before locating the element through xpath * @param xpath * @param text */ public void findElementByXpathClearSendkeys(String xpath,String text,WebDriver driver){ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElement(By.ByXPath.xpath(xpath)).clear(); driver.findElement(By.ByXPath.xpath(xpath)).sendKeys(text); } /** * Clear the text box first and then locate the element through cssSelector * @param cssSelector * @param text */ public void findElementByCssClearSendkeys(String cssSelector,String text,WebDriver driver){ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElement(By.ByCssSelector.cssSelector(cssSelector)).clear(); driver.findElement(By.ByCssSelector.cssSelector(cssSelector)).sendKeys(text); } /** * cssSelector Click events * @param cssSelector */ public void findElementByCssClick(String cssSelector,WebDriver driver){ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElement(By.ByCssSelector.cssSelector(cssSelector)).click(); } /** * xpath Click events * @param xpath */ public void findElementByXpathClick(String xpath,WebDriver driver){ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElement(By.ByXPath.xpath(xpath)).click(); } }

2. Integrating TestNG test framework

1. Java integrates the jar package used in TestNG. Download a jar package first;

2. Import the jar package into the project (IDEA is imported here)

The integration part takes login as an example:

package com.test;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Login  {
    WebDriver driver = null;
    //Call the class above to locate the element
    ElementLocation elementLocation =  new ElementLocation();
    //Run before all methods run
    public void before(){
        System.setProperty("", "E:\\selenium\\chromedriver.exe");
        driver = new ChromeDriver();
        String url = "";
     * Locate login interface elements
     * 1.Enter the correct mobile number
     * 2.Enter the correct password
     * 3.Login successfully
    public void test_login1(){
        System.out.println("Login successfully,Jump to home");
    }//Run after all methods have been run
    public void after(){
            Thread thread = new Thread();
        driver.quit();  }
catch (InterruptedException e){ e.printStackTrace(); } } }


1. It's still a little complicated to provide the method to call the location element, and it will be optimized later.

2. Integrate TestNG

1. It is convenient to select jar package. Now many of them do not use jar package files to integrate into maven directly. We will continue to optimize later.
2. TestNG provides a lot of comments. Currently, there are only three.
@ BeforeMethod: run this method only once before running other methods
@ AfterMethod: run this method only once after running other methods
@ Test: run the current method

Tags: Java Selenium Mobile Maven

Posted on Thu, 16 Jan 2020 08:03:30 -0800 by Nightslyr