设置Selenium网络驱动程序的默认执行速度



我正在用webdriver运行一些GUI测试。我直接从selenium IDE导出了一些测试。在这个测试中,我不得不降低IDE的速度来运行它,因为加载了一个下拉。我如何在Selenium webdriver中减慢测试速度?我已经把

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

并保持高速运行。我知道睡眠选项,但这不是我要找的,我想改变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;
    }
}
}

阅读一些答案,没有帮助

降低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 -设置默认速度为慢

不要用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);
}

曾经有一个"setSpeed()"方法用于Selenium WebDriver的Java绑定。但它已被弃用,因为它对浏览器自动化没有意义。

相反,如果驱动程序比加载你的元素或类似的东西"快",你应该总是让智能地使用等待来处理这些情况。

总而言之,在WebDriver本身中,目前没有选项可以故意减慢的执行速度。除了实现Thread.sleep() 之外,目前没有其他方法显式地减慢您的步骤。

但是还有一些其他的选择,你可能会探索:

例如,如果你想减慢点击元素的速度,你可以编写自己的方法来点击元素:

public void clickElement(WebElement element) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    element.click();
}

如果您想要减慢findElement方法的速度,您可以编写另一个辅助方法:

public void findElement(By by) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    driver.findElement(by);
}

你甚至可以从一个WebDriver类中编写你自己的扩展类,如下所示:

public class MyFirefoxDriver extends FirefoxDriver {
@Override
public WebElement findElement(By by) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return by.findElement((SearchContext) this);
}

你可以为所有你想减慢的执行编写这些方法

在正常情况下,当它查找元素时,Selenium会一直尝试查找元素,直到您设置了"隐式等待"的值。这意味着,如果元素在此之前被找到,它将继续执行测试。我不是Java专家,但是在您提供的代码中,我无法识别正在寻找下拉项的任何位。由于下拉菜单可以在加载其中的实际项目之前加载很久,我敢打赌,这就是您的问题所在:它正在寻找下拉菜单本身,找到它,停止等待并开始尝试执行其余的测试,但由于尚未加载项目而失败。因此,解决方案是查找您要选择的特定项目。不幸的是,我对Java不够精通,无法为您提供实际的代码解决方案。

作为题外话,根据我的经验,当从Selenium IDE导出到其他东西时,您最终得到的测试几乎与您期望的完全不同,但并非完全不同。他们可以做您期望他们做的事情,但是如果您自己编写测试代码,他们通常会走捷径,或者至少采用不同的方法。

public void beforeAll(ExtensionContext)抛出IOException {

  // Set webdriver execution speed
    final EventFiringWebDriver slowDriver = new EventFiringWebDriver(driver);
    final ListenerThatWaitsBeforeAnyAction locatorsListener = new ListenerThatWaitsBeforeAnyAction(Duration.ofMillis(500));
    slowDriver.register(locatorsListener);
    WebDriverRunner.setWebDriver(slowDriver);

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

@Override
public void afterAll(final ExtensionContext context) throws Exception {
    LOGGER.info("Call afterAll function...");
    if (Objects.nonNull(WebDriverRunner.getWebDriver())) {
        WebDriverRunner.getWebDriver().quit();
    }
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxBaseWebDriverListener扩展WebDriverEventListener {

@Override
void beforeAlertAccept(WebDriver driver);
@Override
void afterAlertAccept(WebDriver driver);
@Override
void afterAlertDismiss(WebDriver driver);
@Override
void beforeAlertDismiss(WebDriver driver);

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

公共类ListenerThatWaitsBeforeAnyAction实现BaseWebDriverListener{

final private Duration duration;
public ListenerThatWaitsBeforeAnyAction(final Duration duration)
{
    this.duration = duration;
}
@Override
public void beforeAlertAccept(final WebDriver driver)
{
    sleep(duration.toMillis());
}

最新更新