Java中循环的使用(一)

2020-05-19  本文已影响0人  墨色尘埃

如图所示,当“添加行”处有很多的数据时,点击了“提交”按钮之后,等待跳出弹出框的时间就会很长,不知道什么时候能跳出来。弹出框的弹出时间随着数据的多少而变化,所以这里要用到循环

提交大批量数据.png 弹出框.png

定义一个变量num,num的值由记录条数决定

        // put数据到列表的时候,因为数据量很大,所以这里休眠时间根据数据动态变化
        int num = 20;
        if (matchOrders != null && matchOrders.size() > 0) {
            int total = matchOrders.size();  //总条数
            int i = total / 10;  //商
            int length = String.valueOf(i).length(); //商的长度

            if (length == 1 && (2 <= i && i <= 5)) {  //数据量在20~59
                num = 100;
            } else if (length == 1 && (6 <= i && i <= 9)) {  //数据量在60~99
                num = 200;
            } else if (length == 2 && (10 <= i && i <= 19)) {  //数据量在100~199,休眠200*300
                num = 300;
            } else if (length == 2 && (20 <= i && i <= 29)) {  //数据量在200~299
                num = 400;
            } else if (length == 2 && (30 <= i && i <= 39)) {  //数据量在300~399
                num = 600;
            } else if (length == 2 && (40 <= i && i <= 49)) {  //数据量在400~499
                num = 800;
            } else if (length == 2 && (50 <= i && i <= 59)) {  //数据量在500~599
                num = 1000;
            }
        }

再对num进行for循环,每次停顿0.3秒后去校验状态,看弹出框是否有弹出来

        boolean isValid = false;
        for (int i = 0; i < num; i++) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //调用校验状态
            String script1 = "return angular.element(document.querySelector('[ng-controller=ticketManageController]'))" +
                    ".scope().view.showTaskChooseUserDialog";
            Object o = driver.executeScript(script1);

            if (o != null && o.equals(true)) {
                isValid = true;
                System.out.println("Object:" + o + "__num=" + num + "__i=" + i + "__i * 300=" + i * 300);
                logger.error("提交之后跳出弹出框", "Object:" + o + "/num=" + num + "/i=" + i + "/i*300=" + i * 300);
                break;
            }
        }
        if (!isValid) {
            throw new BusinessException("记录提交失败,祥云校验失败");
        }

完整的submit方法

    public void submit(List<LinkedHashMap> matchOrders, ChromeDriver driver, String mmzyjCookie) throws BusinessException,
            InterruptedException {

        //添加mm系统cookie
        driver.manage().addCookie(new Cookie("CurrentOrg_", "3202100010", cookiemmjsUrl, "/mm_zyj", null, true));
        driver.manage().addCookie(new Cookie("JSESSIONID", mmzyjCookie.substring(mmzyjCookie.indexOf("=") + 1, mmzyjCookie
                .length()), cookiemmjsUrl, "/mm_zyj", null, true));

        //点“提交按钮”
        String script = "angular.element(document.querySelector('[ng-controller=ticketManageController]')).scope()" +
                ".subData()";
        driver.executeScript(script);


        // put数据到列表的时候,因为数据量很大,所以这里休眠时间根据数据动态变化
        int num = 20;
        if (matchOrders != null && matchOrders.size() > 0) {
            int total = matchOrders.size();  //总条数
            int i = total / 10;  //商
            int length = String.valueOf(i).length(); //商的长度

            if (length == 1 && (2 <= i && i <= 5)) {  //数据量在20~59
                num = 100;
            } else if (length == 1 && (6 <= i && i <= 9)) {  //数据量在60~99
                num = 200;
            } else if (length == 2 && (10 <= i && i <= 19)) {  //数据量在100~199,休眠200*300
                num = 300;
            } else if (length == 2 && (20 <= i && i <= 29)) {  //数据量在200~299
                num = 400;
            } else if (length == 2 && (30 <= i && i <= 39)) {  //数据量在300~399
                num = 600;
            } else if (length == 2 && (40 <= i && i <= 49)) {  //数据量在400~499
                num = 800;
            } else if (length == 2 && (50 <= i && i <= 59)) {  //数据量在500~599
                num = 1000;
            }
        }

        boolean isValid = false;
        for (int i = 0; i < num; i++) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //调用校验状态
            String script1 = "return angular.element(document.querySelector('[ng-controller=ticketManageController]'))" +
                    ".scope().view.showTaskChooseUserDialog";
            Object o = driver.executeScript(script1);
            System.out.println("Object:" + o + "__num=" + num + "__i=" + i + "__i * 300=" + i * 300);

            if (o != null && o.equals(true)) {
                isValid = true;
                break;
            }
        }

        if (!isValid) {
            throw new BusinessException("记录提交失败,祥云校验失败");
        }

        // TODO: 2019/12/17 postman测试用时候,注释
        boolean isSelected = false;
        for (int i = 0; i < 10; i++) {
            WebElement element = null;
            try {
                Thread.sleep(300);
                element = driver.findElementById("usertask1_0510048312");
            } catch (Exception ex) {

            }
            if (element != null) {
                //调用一下js选中下一个节点的候选人
                driver.executeScript("document.getElementById('usertask1_0510048312').checked=true");
                isSelected = true;
                break;
            }
        }

        if (!isSelected) {
            throw new BusinessException("记录提交失败,未选中提交人");
        }


//        最后一步提交
        WebElement element2 = driver.findElement(By.cssSelector("button[ng-show='nodes']"));
        element2.click();

    }

上一篇 下一篇

猜你喜欢

热点阅读