java - 设置selenium webdriver的默认执行速度

标签 java user-interface selenium selenium-webdriver selenium-ide

我正在使用 webdriver 运行一些 GUI 测试。我直接从 selenium IDE 导出了一些测试。在此测试中,由于下拉菜单的加载,我不得不降低 IDE 的运行速度。我怎样才能减慢我在 Selenium webdriver 中的测试?我已经放了

 driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

而且一直在高速运转。我知道 sleep 选项,但这不是我要找的,我想更改 webdriver 的默认执行速度。这是我的代码:

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ProfileCheck {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private final StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost:8080";
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
}

@Test
public void testProfileCheck() throws Exception {
    System.out.println("Test if profiles have access to the screen");
    // Administrateur (has access)

    driver.get(baseUrl + "/Dashboard/index.jsp?lang=en");
    driver.findElement(By.cssSelector("input[name=combo_profile]")).click();
    driver.findElement(
            By.cssSelector(".x-boundlist-item:contains('Administrateur')"))
            .click();
    driver.get(baseUrl + "/Params/ClientCutOff/index.jsp?lang=en");
    try {
        assertTrue(driver.getTitle().matches("^[\\s\\S]*ClientCutOff$"));
    } catch (Error e) {
        verificationErrors.append(e.toString());
    }
    // Habilitation (no access)
    driver.get(baseUrl + "/Dashboard/index.jsp?lang=en");
    driver.findElement(By.cssSelector("input[name=combo_profile]")).click();
    driver.findElement(
            By.cssSelector(".x-boundlist-item:contains('Habilitation')"))
            .click();
    driver.get(baseUrl + "/Params/ClientCutOff/index.jsp?lang=en");
    try {
        assertFalse(driver.getTitle().matches("^[\\s\\S]*ClientCutOff$"));
    } catch (Error e) {
        verificationErrors.append(e.toString());
    }
}

@After
public void tearDown() throws Exception {

    try {
        Thread.sleep(5000);
        driver.quit();
    } catch (Exception e) {
    }
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
    }
}

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

private boolean isAlertPresent() {
    try {
        driver.switchTo().alert();
        return true;
    } catch (NoAlertPresentException e) {
        return false;
    }
}

private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
}

阅读了一些答案,但没有帮助

Decreasing the speed of Selenium Webdriver

https://sqa.stackexchange.com/questions/8451/how-can-i-reduce-the-execution-speed-in-webdriver-so-that-i-can-view-properly-wh

Selenium IDE - Set default speed to slow

最佳答案

不要使用sleep!!!

public static WebElement findElement(WebDriver driver, By selector, long timeOutInSeconds) {
    WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
    wait.until(ExpectedConditions.presenceOfElementLocated(selector));

    return findElement(driver, selector);
}

关于java - 设置selenium webdriver的默认执行速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32067341/

相关文章:

java - 如何修复 JSR-303 验证和 orientdb 的兼容性问题

java - 单独测试单一方法?

Xcode Instruments 给出此错误 "Could not start script, target application is not frontmost."

java - 如何在使用 Serenity BDD 的同时为 Chrome 设置日志记录首选项?

java - JFrame 窗口不显示

java - 检查预定义的字符串值 Hibernate Validator

python - 在 PySimpleGUI 中制作一个文本框

java swing cardLayout.show() 什么都不做

android - Appium - reset.sh 在 Mac 上的 "buildAndroidBootstrap"任务上失败并出现错误

node.js - 从 'then' 内的断言返回 promise