有没有可能有一个流利的等待两个条件

时间:2016-04-12 17:08:24

标签: java selenium

在Java Selenium Framework中,FluentWait是否可以检查两种不同的预期条件?它可能是一个或另一个。

为了提供更多上下文,我需要检查一个网站的标题,但根据具体情况,它可以重定向到两个网站之一(想象你无法登录并被发送到登录页面,但是如果您确实登录,则重定向到主页。

对于这种情况,隐性等待会更好吗?因为该网站似乎很多次加载速度相当缓慢,我不得不不断增加它的等待时间。

2 个答案:

答案 0 :(得分:3)

您可以操纵ExpectedConditions以形成合适的复杂条件。有or(...)urlContains(...)方法可以满足您的需求。像这样:

/*Lets say that we have the following array initialized,
  k is the maximum value and n is the length of the array*/
int A[] = {4,5,1,3,7};
    k = 7;
    n = 5;

void Counting_sort(int A[], int k, int n)
{   
    /*C is the count array and B is the sorted array*/
    int i, j;
    int B[n], C[k+1];
    for(i = 0; i <=  k; i++)
        C[i] = 0;

    for(j = 0; j < n; j++)
        C[ A[j] ] = C[ A[j] ] + 1;

    for(i = 1; i < k+1; i++)
        C[i] = C[i] + C[i-1];

    for(j = 0; j < n; j++) {
        B[ C [ A [j] ] ] = A[j];
        C[ A[j] ] = C[ A[j]] - 1;
    }
    //so in this loop I must always traverse from 1 to <= n and leave the Index 0?
    for (i = 1; i <= n; i++)
        cout << B[i] << endl;
}

答案 1 :(得分:2)

您可以使用lambda表达式创建自己的等待条件。此示例等待标题以“abcd”开头或包含“1234”:

WebDriverWait wait = new WebDriverWait(driver, 20);

String page_title = wait.until((WebDriver wd)->{
    String title = wd.getTitle();
    return title.startsWith("abcd") || title.contains("1234") ? title : null;
});

System.out.println("Page title is: " + page_title);