接口数据对比测试程序

该程序用于自动化比较线上数据和接口返回的数据是否一致。通过设置代理并使用Firefox浏览器模拟访问网页,获取线上数据,然后与接口返回的数据进行对比,确保两者在数量和内容上的匹配。程序首先读取txt文件中的问题,然后分别从线上和接口获取数据,最后对比两者结果,若发现不一致则记录下来。
摘要由CSDN通过智能技术生成

线上和接口数据进行对比,排除格式影响,数据是否一致。

package zhishu;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test11 {
    private static final String URLOnline = "http://www.**********";
    private static final String URLInterface = "http://192.168.201.119:9111/dataquery?q=";
    
    private static final String IP = "172.20.201.254";
    private static final int PORT = 3128;
    private static Proxy proxy;
    
    static WebDriver driver;

    private static void getProxy() throws Exception {
        // 设置代理 地址和密码
        InetSocketAddress socketAddress = new InetSocketAddress(
                InetAddress.getByName(IP), PORT);
        proxy = new Proxy(Proxy.Type.HTTP, socketAddress);

        // 设置代理的密码验证
        Authenticator auth = new Authenticator() {
            private PasswordAuthentication pa = new PasswordAuthentication(
                    "hexin", "hx300033".toCharArray());

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return pa;
            }
        };
        Authenticator.setDefault(auth);

    }

    //获取线上数据,装在StringBuffer result
    private static String getLineOnline(String w) {
        StringBuffer result = new StringBuffer();
        String line = null;
        try {
            //打开新的网页            
            driver.get(URLOnline + w + "&my=1");
            
            //如果页面没有结果
            if(!driver.findElement(By.xpath("//*[@id='boxTitle' and contains(text(),'选出')]")).isDisplayed())
            {
                return null;
            }
            //获取下面 第二个http的url
            WebElement elementURL = driver.findElement(By.xpath("//html/body/p[2]"));
            String urlOpen = elementURL.getText();
            urlOpen = urlOpen.substring(urlOpen.indexOf("http"));
            
            // 打开连接
            URL url = new URL(urlOpen);
            URLConnection conn = url.openConnection(proxy);

            // 读取内容
            InputStreamReader isr = new InputStreamReader(conn.getInputStream());
            BufferedReader br = new BufferedReader(isr);
            while ((line = br.readLine()) !=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值