Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要传递一个条件作为参数_Java_Selenium - Fatal编程技术网

Java 我需要传递一个条件作为参数

Java 我需要传递一个条件作为参数,java,selenium,Java,Selenium,基本上,我需要将一个条件作为参数传递到一个方法中,并不断验证该条件,直到它更改其值 下面我举了一个方法的例子,按下某个键盘键直到满足条件 public static void pressKeyUntilCondition(List<String> key, Predicate<YourObjectType> condition) { while(condition.test(yourValueToCheck)) { press(key);

基本上,我需要将一个条件作为参数传递到一个方法中,并不断验证该条件,直到它更改其值

下面我举了一个方法的例子,按下某个键盘键直到满足条件

 public static void pressKeyUntilCondition(List<String> key, Predicate<YourObjectType> condition) {
    while(condition.test(yourValueToCheck)) {
        press(key);
        timesPressed++;
    }
}
例如,我有一个案例,我试图阅读服务条款的页面,我需要按下“向下”,直到滚动条到达底部

public static void pressKeyUntilCondition(Keys key, boolean condition) {
    while(condition) {
        press(key);
        timesPressed++;
    }
}
编辑:
另外,解决问题的另一种方法是,我可以将一个方法传递到pressKeyUntilCondition()中,这样我就可以直接将布尔getHeightFromBottom()方法发送到While条件中。

您可能希望您的条件是可更改的(不是最终的)。 因此,考虑一些接口,包含可以调用的实际值(或检查需要做的)。 大概是这样的:

public static void pressKeyUntilCondition(Keys key, Reference<Boolean> reference) {
    while(reference.get()) {
        press(key);
        timesPressed++;
    }
}
private static final int BOTTOM_SCROLLBAR_POSITION = 777; // todo: change to your required position

private int getScrollBarPosition() {
    return 666; // todo: implement your scrollbar position logic
}

public void pressKeyUntilCondition(Keys key) {
    while (getScrollBarPosition() <= BOTTOM_SCROLLBAR_POSITION) { // using just an existing function
        press(key);
        timesPressed++;
    }
}
public static void按按键直到条件(按键,参考){
while(reference.get()){
按(键);
时间压缩++;
}
}
另一点需要注意的是,循环时不会消耗无休止的cpu,因此您可能只希望偶尔检查该值,或者您可能希望将线程置于睡眠状态,并且仅在条件已满时才将其唤醒


(因此,只有在不需要持续应用按键操作的情况下,此操作才有效)

您可以使用谓词来进行此操作。谓词是布尔值函数。 通过这种方式,您可以根据您的值进行测试,直到满足条件为止

 public static void pressKeyUntilCondition(List<String> key, Predicate<YourObjectType> condition) {
    while(condition.test(yourValueToCheck)) {
        press(key);
        timesPressed++;
    }
}
public static void按键直到条件(列表键,谓词条件){
while(condition.test(yourValueToCheck)){
按(键);
时间压缩++;
}
}

您可以将谓词传递给您的方法:

public static void pressKeyUntilCondition(Keys key, Predicate<Integer> condition) {
   while(condition.test(timesPressed)) {
      press(key);
      timesPressed++;
    }
}
public static void按KeyUntlCondition(按键、谓词条件){
while(条件测试(时间按下)){
按(键);
时间压缩++;
}
}
您通过的条件可以是,例如:

Predicate<Integer> predicate = integer -> integer == 3;
谓词=整数->整数==3;
如果需要评估更多“条件”,可以创建包含这两个字段的模型类,并从中创建谓词:

public class ConditionValues {
        private int timesPressed;
        private Keys key;
        //getters setters
}

Predicate<ConditionValues> predicate = values -> values.getMaxTimePressed() ==  3 && values.getKey() == Keys.DOWN;

public static void pressKeyUntilCondition(Keys key, Predicate<ConditionValues> condition) {
    ConditionValues conditionValues = new ConditionValues(Keys.DOWN, 0);
    while(condition.test(conditionValues)) {
        press(key);
        timesPressed++;
        conditionValues.setTimesPressed(timesPressed);
    }
公共类条件值{
私营机构;;
私钥;
//吸气剂二传手
}
谓词谓词=values->values.getMaxTimePressed()==3&&values.getKey()==Keys.DOWN;
public static void presskeyuntlcondition(按键、谓词条件){
ConditionValues ConditionValues=新的ConditionValues(Keys.DOWN,0);
while(条件测试(条件值)){
按(键);
时间压缩++;
conditionValues.setTimesPressed(timesPressed);
}

当然,这只是一个POC,因此您可以根据需要进行任何调整,例如将所需的键作为参数传递。

我没有得到确切的问题,但据我所知,您可以执行以下操作

如果要检查动态解决方案,可以只检查该方法,而不是静态
布尔值
条件。如下所示:

public static void pressKeyUntilCondition(Keys key, Reference<Boolean> reference) {
    while(reference.get()) {
        press(key);
        timesPressed++;
    }
}
private static final int BOTTOM_SCROLLBAR_POSITION = 777; // todo: change to your required position

private int getScrollBarPosition() {
    return 666; // todo: implement your scrollbar position logic
}

public void pressKeyUntilCondition(Keys key) {
    while (getScrollBarPosition() <= BOTTOM_SCROLLBAR_POSITION) { // using just an existing function
        press(key);
        timesPressed++;
    }
}

您应该创建一个setter方法,每次需要更改条件值时都会调用该方法。您可以根据此setter方法中的条件执行操作。我想知道您是在要求一些包含Selenium的解决方案,还是仅仅是关于如何编写一个好的Java代码?您应该将谓词作为参数传递,我如何传递参数对于该方法,我有“pressKeyUntilCondition(Keys.ARROW_DOWN,NavigationBar.getHeightFromBottom()”;但“getHeightFromBottom()”是布尔值,而不是谓词pressKeyUntilCondition方法应始终接收“Dynamic”“条件作为参数,否则该方法在其他情况下不会有用。当然。正如您所看到的,我的两个选项都依赖于
getScrollBarPosition()
中的动态逻辑(很抱歉,如果不清楚返回的常量必须替换为真实的滚动条逻辑)。以及如何将参数传递给该方法,我有“按KeyUntilCondition(Keys.ARROW_DOWN,NavigationBar.getHeightFromBottom());”但“getHeightFromBottom()”是布尔值,而不是谓词。