0

嗨,我正在尝试在硒测试中使用 webdriver。我想检查它是如何工作的。我给了 5 秒作为 webdriver 等待的最长时间。我的页面加载时间超过 7 秒,但我仍然没有从 webdriver 等待中得到任何超时异常。我也在给我的控制台输出。请告诉我为什么我没有收到超时异常?

public class MainClass {

private static ChromeDriver driver;


public static void main(String[] args) {



            executeTest();

}

private static void executeTest( )  {

    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

    driver = new ChromeDriver();
    // driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

    driver.get("http://myUrl");

    Long loadtime = (Long) ((JavascriptExecutor) driver)
            .executeScript("return performance.timing.loadEventEnd - performance.timing.navigationStart;");

    System.out.println("loadding time " + Loadtime);

    WebDriverWait wait = new WebDriverWait(driver, 5);

    Boolean sign_in = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("signinbutton")))
            .isDisplayed();

    if (sign_in == true) {
        driver.findElement(By.id("signinbutton")).click();
    } else {
        System.out.println("Oops! Couldn't locate sign_in element!");
    }

    Boolean user_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username_id")))
            .isDisplayed();
    // user_name.sendKeys("ANAND@RIL");
    if (user_name == true) {
        driver.findElement(By.id("username_id")).sendKeys("ANAND@RIL");
    } else {
        System.out.println("Oops! Couldn't locate user_name element!");
    }

    Boolean password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password_id")))
            .isDisplayed();

    if (password == true) {
        driver.findElement(By.id("password_id")).sendKeys("ANAND");
    } else {
        System.out.println("Oops! Couldn't locate password element!");
    }

    WebElement login = 
    wait.until(ExpectedConditions.elementToBeClickable(By.id("textButton")));
    login.click();

}
}

控制台输出:

尝试 1:

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 42020
Only local connections are allowed.
Apr 18, 2018 12:07:35 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loading time 7872**

尝试 2:

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 38325
Only local connections are allowed.
Apr 18, 2018 12:07:46 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loadding time 6632**

尝试 3:

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 34619
Only local connections are allowed.
Apr 18, 2018 12:07:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
**Loadding time 7522**

尝试 4:

Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 48000
Only local connections are allowed.
Apr 18, 2018 12:08:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
4

2 回答 2

1

您需要考虑以下几个事实:

  • pageLoadTimeout()pageLoadTimeout()设置在抛出异常/错误之前等待页面加载完全的时间。如果超时为负数,则页面加载可能是不确定的。

    在您的代码中,您已配置pageLoadTimeout()40秒。根据日志,您的页面加载时间是7872 ms6632 ms7522 ms,因此您看不到异常/错误。

    如果不配置,根据GeckoDriverpageLoadTimeout()的当前实现,默认值"pageLoad":300000被考虑。

    您可以在 Selenium中的pageLoadTimeout 中找到有关pageLoadTimeout()的详细讨论not working

  • WebDriverWait():WebDriverWait()Fl​​uentWait的特化,它使用 WebDriver 实例并与ExpectedConditions类一起工作,该类被定义为在继续执行代码之前等待某个条件发生。

    在您的代码中,您已配置WebDriverWait5秒,这适用于您用作的所有ExpectedConditions :

     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("signinbutton")));
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username_id")));
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password_id")));
     wait.until(ExpectedConditions.elementToBeClickable(By.id("textButton")));
    

    所有预期条件都在5秒的时间跨度内实现。所以你也看不到任何异常/错误。

    您可以在用显式等待替换隐式等待 (selenium webdriver & java)中找到关于WebDriverWait / ExplicitWait的详细讨论

于 2018-04-18T07:47:59.177 回答
1

我已经用 try-catch 修改了该方法。请尝试以下操作:

public static void main(String[] args) throws Exception {
try{
    executeTest();
    } catch (Exception e) {
        System.out.println(e);
        throw e;
    }
}
private static void executeTest( ) throws Exception {
try{

    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

    driver = new ChromeDriver();
    // driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

    WebDriverWait wait = new WebDriverWait(driver, 5);

    driver.get("http://myUrl");

    Boolean sign_in = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("signinbutton")))
            .isDisplayed();

    Long loadtime = (Long) ((JavascriptExecutor) driver)
            .executeScript("return performance.timing.loadEventEnd - performance.timing.navigationStart;");

    System.out.println("loadding time " + Loadtime);

    if (sign_in == true) {
        driver.findElement(By.id("signinbutton")).click();
    } else {
        System.out.println("Oops! Couldn't locate sign_in element!");
    }

    Boolean user_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username_id")))
            .isDisplayed();
    // user_name.sendKeys("ANAND@RIL");
    if (user_name == true) {
        driver.findElement(By.id("username_id")).sendKeys("ANAND@RIL");
    } else {
        System.out.println("Oops! Couldn't locate user_name element!");
    }

    Boolean password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password_id")))
            .isDisplayed();

    if (password == true) {
        driver.findElement(By.id("password_id")).sendKeys("ANAND");
    } else {
        System.out.println("Oops! Couldn't locate password element!");
    }

    WebElement login = 
    wait.until(ExpectedConditions.elementToBeClickable(By.id("textButton")));
    login.click();
    }
    catch(Exception e) {
        System.out.println(e);
        throw e;
    }
}
于 2018-04-18T07:03:06.657 回答