Java selenium验证下拉框内容

本文详细介绍了如何利用Java与Selenium自动化测试工具,对网页中的下拉框元素进行内容验证。通过示例代码展示如何选取下拉框选项,并检查所选选项是否正确显示,帮助开发者和测试人员提升Web应用的测试效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void validateLMInterfaceBarcodeToMachine(){
        String text = data.get(dataKey);
        System.out.println("input: "+text);
        String [] barcodeToMachine= text.split(";");
        String inputBarcode = barcodeToMachine[0];
        String inputType = barcodeToMachine[1];
        try {
            String barcode = driver.findElement(By.xpath(prop.getProperty("PanelTracking.LMInterface.validateBarcodeToMachine_xpath"))).getText();
            if (barcode.equals(inputBarcode)) {
                System.out.println("barcode to machine is displayed in LM interface configure");
                test.log(Status.PASS, "barcode to machine is displayed in LM interface configure");
            } else {
                System.out.println("barcode to machine is not displayed in LM interface configure");
                test.log(Status.FAIL, "barcode to machine is not displayed in LM interface configure");
            }
        }catch (Exception e){
            System.out.println("barcode to machine is not displayed in LM interface configure");
            test.log(Status.FAIL, "barcode to machine is not displayed in LM interface configure");
        }
        try{
            Select select= new Select(driver.findElement(By.xpath(prop.getProperty("PanelTracking.LMInterface.validateBarcodeToMachineType_xpath"))));//new一个select对象,里面有封装好的下拉框操作方法
            List<WebElement> webElements = select.getOptions();//获取下拉列表所有选项存入list
            List<String> values = new ArrayList<>();
            for(WebElement webElement:webElements){//获取各个选项里的字符串,并放入新建list里
                values.add(webElement.getText());
            }
            String list = values.stream().collect(Collectors.joining(","));//将list转换成用“,”隔开的字符串
            if (list.equals(inputType)) {
                System.out.println("barcode to machine choice is displayed in LM interface configure");
                test.log(Status.PASS,"barcode to machine choice is displayed in LM interface configure");
            }
            else {
                System.out.println("barcode to machine choice is not displayed in LM interface configure");
                test.log(Status.FAIL,"barcode to machine choice is not displayed in LM interface configure");
            }
        }catch (Exception e){
            System.out.println("barcode to machine choice is not displayed in LM interface configure");
            test.log(Status.FAIL,"barcode to machine choice is not displayed in LM interface configure");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值