首页 > 解决方案 > 如何比较同一方法的两个变量值

问题描述

同一类有两种方法。我想通过使用断言来比较两个变量的值。

这是两种方法

//方法一

public void Edit_window() throws InterruptedException {

        collection_title.clear();

//        driver.wait(2000);
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
         Date date = new Date();
        collection_title.sendKeys(dateFormat.format(date));


    }

//方法二

public void name_collection()
    {
        String updated_title=col_title.getText() ;

        System.out.println("the title eof the collectio is " + updated_title) ;

        System.out.println("the value is" + date) ;
    }

因此,如果您可以看到有一个变量“日期”,它包含当前日期。我想要该变量值并与方法 2 中定义的变量值“updated_title”进行比较。请输入任何内容!

标签: javaselenium

解决方案


您基本上可以将方法的返回类型更改为字符串类型,然后添加断言。下面的示例:

public String Edit_window() throws InterruptedException {

        collection_title.clear();

//        driver.wait(2000);
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
         Date date = new Date();
        collection_title.sendKeys(dateFormat.format(date));
return dateFormat.format(date);
    }


public String name_collection()
    {
        String updated_title=col_title.getText() ;

        System.out.println("the title eof the collectio is " + updated_title) ;

        System.out.println("the value is" + date) ;
return date;
    }

//然后对于断言你基本上可以:Assert.asserEquals(classObject.Edit_window(), classObject.name_collection);


推荐阅读