直接上源码
源码地址 https://github.com/int01/xinshangmeng_automationhttps://github.com/int01/xinshangmeng_automation
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.minhw</groupId>
<artifactId>iminhw-xinsahngmeng-qtp</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
</dependencies>
</project>
ExcelUtils.java
package com.minhw.utils;
import io.netty.util.internal.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @author
* @date 2022/12/3 22:13
* @Description: TODO
*/
public class ExcelUtils {
public static Map<String, String> readExcel(String path) {
// 订购的数据sheet位于excel的那个位置,从0开始
int sheetIndex = 2;
Map<String, String> map = new HashMap<>();
try {
// 获取文件输入流
InputStream inputStream = new FileInputStream(path);
// 定义一个org.apache.poi.ss.usermodel.Workbook的变量
Workbook workbook = null;
// 截取路径名 . 后面的后缀名,判断是xls还是xlsx。 魔法值!!!
if (path.substring(path.lastIndexOf(".") + 1).equals("xls")) {
workbook = new HSSFWorkbook(inputStream);
} else if (path.substring(path.lastIndexOf(".") + 1).equals("xlsx")) {
workbook = new XSSFWorkbook(inputStream);
}
Sheet sheet = workbook.getSheetAt(sheetIndex);
// sheet.getPhysicalNumberOfRows()获取总的行数
// 循环读取每一行
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
// 循环读取每一个格
Row row = sheet.getRow(i);
//第二个框为中文名称
Cell name = row.getCell(1);
name.setCellType(CellType.STRING);
//最后一个值为订单数据,如果为空则不处理。请将你的档位订购量放在最后一行
/** 如:
* 0198 云烟(软大重九) 1000 3 2 2
* 1101 中华(硬) 450 1 1 1 1 1
* 保留 "品牌编码 品牌名称 档位订货量", 档位订货量放最后一个位置,档位订货量为空则不处理
* 如品牌编码0198是当前档位无需订购的量,程序不会处理
* !!无需保留标题拦,只需要保留数据即可
*/
Cell value = row.getCell(row.getPhysicalNumberOfCells() - 1);
value.setCellType(CellType.STRING);
if (!StringUtil.isNullOrEmpty(value.getStringCellValue())) {
map.put(name.getStringCellValue().trim(), value.getStringCellValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
ExcelTest
import com.minhw.utils.ExcelUtils;
import org.junit.Test;
import java.util.Map;
/**
* @Author
* @DateTime 2022/12/30 11:54 星期五
* @Description: TODO
*/
public class ExcelTest {
/**
* excel文件的路径
*/
String path = "/Users/minhongwei/Desktop/2023第二期公示.xlsx";
@Test
public void getExcel() {
Map<String, String> excel = ExcelUtils.readExcel(path);
for (Map.Entry<String, String> entry : excel.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
XinshangmengTest 这个文件是最主要的文件,订货也在这里面
// Generated by Selenium IDE
import com.minhw.utils.ExcelUtils;
import org.junit.After;
import org.junit.Test;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;
import java.time.Duration;
import java.util.*;
public class XinshangmengTest {
private WebDriver driver;
private Map<String, Object> vars;
// private Map<String, String> excel;
/**
* excel文件的路径
*/
String path = "/Users/minhongwei/Desktop/2023第二期公示.xlsx";
/**
* 新商盟登录用户名和密码。请自行修改
*/
String username = "你的用户名";
String password = "你的密码";
JavascriptExecutor js;
@Before
public void setUp() {
// excel = ExcelUtils.readExcel();
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void xinshangmeng() throws InterruptedException {
Map<String, String> excel = ExcelUtils.readExcel(path);
driver.get("http://www.xinshangmeng.com/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.findElement(By.id("username")).click();
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("mcmm1")).click();
driver.findElement(By.id("mcmm1")).sendKeys(password);
driver.findElement(By.id("btn-login")).click();
driver.findElement(By.linkText("关闭")).click();
driver.findElement(By.linkText("卷烟订货")).click();
// By search = By.id("cgtIdNameSearch");
// driver.findElement(search).click();
// driver.findElement(search).sendKeys("0143");
// driver.findElement(By.id("confirmm")).click();
// System.out.printf(driver.findElement(By.cssSelector("#newul > li:nth-child(1)")).toString());
// List<WebElement> elementList = driver.findElements(By.cssSelector("#newul li"));
WebElement futureElement = driver.findElement(By.id("newul"));
List<WebElement> elementList = futureElement.findElements(By.tagName("li"));
//By.id("newul"));
System.out.println("获取到可选订单列表数量:" + elementList.size());
for (WebElement e : elementList) {
Thread.sleep(1000);
System.out.println(e.getAttribute("title"));
String title = e.getAttribute("title").trim();
if (excel.get(title) != null) {
System.out.println(e.getAttribute("data-cgt-code"));
String req_id = "req_qty_" + e.getAttribute("data-cgt-code").trim();
System.err.println("成功订购香烟,名:" + title + ", 订单数量: " + excel.get(title) + "。剩于定烟品种" + excel.size());
By search = By.id("cgtIdNameSearch");
driver.findElement(search).click();
driver.findElement(search).sendKeys(title);
driver.findElement(By.id("confirmm")).click();
Thread.sleep(1000);
driver.findElement(By.id(req_id)).click();
Thread.sleep(1000);
// driver.findElement(By.id(req_id)).clear();
// Thread.sleep(1000);
driver.findElement(By.id(req_id)).sendKeys(excel.get(title));
// 操作 完成删除对应值
excel.remove(title);
driver.findElement(search).clear();
}
// 如果对应的值空了,说明订货完成,直接推出循环
if (excel.size() < 1) {
break;
}
}
Thread.sleep(1000);
driver.findElement(By.id("accinfo")).click();
// 将得到的余额打印到控制台
System.err.println("余额:" + driver.findElement(By.cssSelector("#accerror > span")).getText());
Thread.sleep(1000);
driver.findElement(By.id("smt")).click();
Thread.sleep(1000);
driver.findElement(By.id("subbtn")).click();
Thread.sleep(3000);
System.err.println("提交结果" + driver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[1]/div[1]/p")).getText());
}
}